Date of creation is older than previous day until 6am - c#

I have app where is datagridview with data from sql database - and a want to modify every row. I have button for this and if i will click there will appear new Form and there I can modify every record, but I want to that user can edit only records up to the previous day until 6 am , later records user cannot modify.
if (DateTime.Parse(labelDate.Text) < DateTime.Now.AddDays(-2))
{
labelNotice.Text = "Cannot modify records older than 2 days"
button1.Enabled = false;
}
My problem is that i dont know how to make date-time limit for this. I have labelDate where I have date of creation and I need to compare with actual date and time and I need to app will say to user that if labelDate is older than previous day until 6am you cannot modify nothing. Now I have only compare date of creation and if its 2 days older button1 will be disabled.
But I need date and time to compare.

Related

Allow only onward date and block backward date

How I will restrict a user to enter only max date of the previous entered date?
I want to enter only onward date and block backward date in SQL and c#.net in windowform?
In the blow image textbox "Auto Most Recent Date (Last Date)" is backward date and textbox "Enter Next Date" is onward date.
You clearly know how to query a database, so run a query like:
SELECT MAX(somedate) FROM courtcases WHERE casenumber = #whatever
And use the output of it on your date time picker
nextDateDateTimePicker.MinDate = <the DateTime you queried>
If the min date is a day later, use AddDays(1) on the date you queried
(Not that that looks like a standard win forms datetimepicker but I’m sure whatever you’re using will have a similar facility)
I don’t think you need to go to the extent of protecting your insert sql against hacking to ensure the user hasn’t modified the ui in some way and put an illegal date in

C# winforms datetime picker maxdate

The max date accepted by Datetime data type in SQL Server and C# seems to be 31/12/9999 23:59:59. I have tried to assign a data value (01/01/9999 00:00:00) from database to a datetime picker. It failed saying
DateTimePicker does not support dates after 31/12/9998 00:00:00.
Parameter name: MaxDate
Now my question is when both the datatypes are compatible (datetime), why does these is restriction in Datetimepicker. This seems to be by design. Can any let me know why this restriction and how this feature in design is going to help?
Thank you
In normal operations the date time picker has to be able to display dates after the selected date. This means that if you selected the max date (31st Dec 9999) then theoretically it would have to display 1st Jan 10,000 - which it can't.
Therefore the picker itself restricts the initial value to be some arbitrary value before the max date so subsequent dates can be shown.
I don't see that this is a problem. When, in a real world application, is the user going to want to set a date 7000+ years in the future?
If your application has unset dates then perhaps you need to consider storing them as null in the database (i.e. make the column nullable) so that it's presented as a blank on the UI. This may actually make it easier for the user to spot dates that haven't been set.
It's as simple as that DateTimePicker checks against
DateTimePicker.MaximumDateTime
which is defined as december 31 9998
here's the relevant portion from the referencesource
public DateTime Value {
set {
bool valueChanged = !DateTime.Equals(this.Value, value);
// Check for value set here; if we've not set the value yet, it'll be Now, so the second
// part of the test will fail.
// So, if userHasSetValue isn't set, we don't care if the value is still the same - and we'll
// update anyway.
if (!userHasSetValue || valueChanged) {
if ((value < MinDate) || (value > MaxDate)) {
throw new ArgumentOutOfRangeException("Value", SR.GetString(SR.InvalidBoundArgument, "Value", FormatDateTime(value), "'MinDate'", "'MaxDate'"));
}
}
And MaxDate checks DateTimePicker.MaximumDateTime
http://referencesource.microsoft.com/#System.Windows.Forms/winforms/Managed/System/WinForms/DateTimePicker.cs,040fca665238ae30

Loading data from SQL database into a list in C#

I'm working on an application that displays (highlighted) the dates on which a certain event will occur.
In the database that I have to use there is a table that contains date intervals or collections of dates that have special meaning (like collection of holidays, school breaks...). When an event is defined, the user defines on which day(s) that event occurs (if it occurs periodically), but the user can also define that that particular event does or does not occur on special interval.
For instance - there can be an event that occurs every Sunday, if that days is not a holiday.
Now, I've solved the periodical part by reading data from the database for that event (determining on which day does that event occur) and then filling a list with dates that will be highlighted (for graphical representation I use MotnhCalendar and its BoldedDates array):
for (DateTime day = start; day < end;day = day.AddDays(1))
{
if (Sunday && day.DayOfWeek == DayOfWeek.Sunday)
{
MarkedDates.Add(day);
}
monthCalendar1.BoldedDates = MarkedDates.ToArray();
}
Now, for this code to work properly, I need to skip the dates that belong to a special interval (in this case if that date is a holiday). It could be easily done by adding another condition into the if clause:
!SpecialDates.Contains(day)
The problem is that I don't know how to fill the list with these special dates from the database. I can't simply "hard code" it, because these special date collections/intervals can be changed at any time. So, my question is - how can I, by using SQL and C# commands, read data from database and save it in the list.
Thanks
As it looks like your using SQL Server 2008 R2 I would recommend using an ORM tool like ADO.NET Entity Framework to access your database - best going with the latest release EF 5.
There are tons of tutorials online on how to get up & running with it - a good one being Creating Model Classes with the Entity Framework.
To give you an idea of how simple it makes it, here is an example of the minimal amount of code you would need to achieve what it is your looking to do:
using (var db = new MyDbContext())
{
var specialDates = db.SpecialDatesTable.ToList();
}

How to insert only day and month or month and year or any combination in a date field sql server

I have a date of birth field in the database which is of datatype date. I have three dropdowns for the same in my aspx page. One dropdown for day,one for month and one for year. If the user selects only day and month or month and year or any combination. How to insert that into the date field into the database.
If the user selects only the day and month or the month and the year or any combination as such. How to insert this into the database?
Thanks,
Two options:
Keep day, month and year in separate (nullable) fields in your database
Keep a DateTime field in the database, populate it with "dummy" data when it's not provided, and have a separate field to indicate what information was provided
The first sounds more sensible to me - it's not like you can really do many sensible date-related queries with "19th of some month in 1976", and it means your data accurately represents what the user specified.
Don't allow user to submit answer until he selects all the field. That's the standard way. you can use validator for it.
Or else try this(i am not sure about this)
1. Keep date field null.
And/Or
2.If (txtDate.Text = "") Then
cmd.Parameters("#Date").Value = sqldatenull
0000 isn't a valid year; firstly I'd revisit the requirements for the input form to decide what is optional and possible add validation.
If it is only the year that is optional then you could simply store 0001 as the year.
dt = new DateTime(Math.Max(1,dob_year), dob_month, dob_day);
If day and/or month are optional then a DateTime isn't the right data type because you cannot store null values so having separate fields for day, month, year is the only option.
A Date will always need all a day, month and a year because it refers to an actual day in the past, present or future.
All dates have to be in the range 01/01/0001 to 31/12/9999, so there is no way of storing the year 0000.
For your problem there are two options:
Store the day the, the month and the year in three separate fields.
Store the date in the usual way, setting the unspecified parts of the date to some default values. Then use a second field with a tinyint to store three bit flags which tell you which parts of the date are valid and which aren't

Date UTC issue with PST/EST

I am trying to save date value to mysql DB, DB date column has UTCdatetime value, whenever the user selects time , it subtracts from the PST/EST/Mountain time, it works when i select PST & subtract -8 hrs, whenever i set MST- moutain time, -7, the date column doesn't save.
Any thoughts?
I'm not sure if I understood the question correctly, but if what you need is to store UTC dates, you don't need to manually add or subtract hours, there's a method for that:
var utcToday = DateTime.Today.ToUniversalTime();

Categories

Resources