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
Related
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.
I want to add posted date of product to database. I am using DateTime.UtcNow but it shows wrong date if date is wrong on computer. How can I solve this problem?
I have "prodpostDate" column in the "product" table and its type is nvarchar.
DateTime aDate = DateTime.UtcNow;
item.prodpostDate= aDate.ToString("dddd, dd MMMM yyyy");
_context.Products.Add(item);
The date is not wrong. The date is exactly what it should be. When you call Now functions, it pulls the date and time from the computer that executes the code. If the date is wrong on the computer, you need to update it/change the timezone.
Expanding on the comment below:
If you want the date and time regardless of a users local settings, you cannot get the date time from their local machine which is what happens if you call a Now function from code ran on the client. You need to make a call to a different source to get it. If you are using an API, you could make a call to it from your client to get the current date and time. Even better than that, if you end up sending a request to the server for an update, just don't send a date time and let the server get it and populate it. Or if you are doing a database update, let the database get the date time on update/insert.
I have a weird problem with sql server 2008. I am trying to save date with dd/MM/yyyy format into sql server 2008, But after inserting date it automatically get converted into MM/dd/yyyy.
Note : my computer clock format is dd/MM/yyyy
My report viewer date text box properties
enter image description here
Date from my database table
enter image description here
my c# code
lbldate.Text = DateTime.Today.ToShortDateString();
date on report
05/04/2017
SQL Server is the Data Layer and as such there is no formatting available; it stores a date as a 4 byte number which is relative to days with 0 = 01/01/1900.
The Application Layer DateTime type is generally an ODBC Canonical representation which basically looks like a class with integer properties for each component (year, month, date, hours, minutes, seconds, milliseconds).
The Presentation Layer is what you actually see, and that is where you should be concerned. When your application calls the ToShortDateString() method, it is calling the display format from the threads current culture, which may or may not reflect the systems settings for Region & Language or Date & Time.
Solution number one is to set the threads current culture, but this would just go to that particular cultures standard display
Thread.CurrentThread.CurrentCulture = new CultureInfo("fr-FR");
Solution number 2 is to just use a custom DateTime format string
lbldate.Text = DateTime.Today.ToString("dd/MM/yyyy");
I would not say this is a "problem" so to speak. This is how SQL handles dates. Your computer clock format is not relevant. To change the format, use CONVERT in your queries. Ex:
SELECT CONVERT(varchar, GETDATE(), 103)
Results: 04/05/2017
SELECT CONVERT(varchar, GETDATE(), 101)
Results: 05/04/2017
The codepage arguments are listed here: https://learn.microsoft.com/en-us/sql/t-sql/functions/cast-and-convert-transact-sql
edit per your new update: Your C# should look something like this:
DateTime.Now.ToString("dd/mm/yyyy")
What you're seeing is how the query tool presents results and has nothing to do with how Sql Server stored the data. Sql Server actually stores dates in a binary (not readable) format.
This is a good thing. Let Sql Server store dates (and other data) how it wants. You only need to worry about how you show the data to users after you retrieve it, and most of the time the best place to worry about that formatting isn't even in the server at all, but in your client language.
I am having hard time to store date information into the datetime column of SQL Server.
I get the input from the user for three columns:
Creation Date
Preparation Date
Next Preparation Date
I use calendarextender and format the date as "yyyy/MM/dd". When all the fields have date, they are stored in the DB as for instance, 16-10-2016 (dd-MM-yyyy).
At this point I have two issues:
These columns are optional, when some of them are empty my code does not work (I assume because datetime cannot be null). To overcome this, I am using the following code snippet but still does not work.
DateTime? creationDate= null;
if (creationDateTextbox.Text != null && creationDateTextbox.Text != "")
{
creationDate= Convert.ToDateTime(creationDateTextbox.Text);
}
When I fetch the dates from DB, they are shown as 10/16/2016 (MM-dd-yyyy) which is different how I formatted it. I would like to show it in the format user enters them.
Dates do not have a format while stored in a database. It is actually usually just a very large long that counts the number of milliseconds from a set starting date.
If you want to store the format you need to stop storing it as dates and instead just treat the text as text in the database, however if you do this you won't get the advantage of sorting or filtering by a date range because it will just be seen as text.
Date time doesn't have any format You can format is as a string, suppose your DateTime type database field dt which contain date as 10/16/2016 (MM-dd-yyyy) then you can convert it
string s = dt.ToString("yyyy/MM/dd");
The answer to one of your questions is here: MSDN
You can use data annotations to format the dates that you get from your SQL DB. I'm assuming that you're using EF6; if not, you can change the field to a varchar in SSMS, and store the date as a String.
And the second, I'm unclear about, but if what you want is for your SQL DB column to be optional, you can use the Optional data annotation for that.
I wanted to save only date in my database. There is a table which holds dates in database and the column which holds date in type of "Date". Now I want to store date from UI,so I placed WPF DatePicker in UI which allows to select date, but whenever I try to get the data from datepicker it shows the date and time.But I want just the dates to be stored in database.
This is the thing i am doing. It is demo code by the way. Can t upload original code. But this explains the thing. you can see in the message box, it shows 14-10-2015 00:00:00 , i want this zeros to be removed.
The dateTime Picker has a property DisplayDate of Type DateTime. This type contains date and time information.
Just use picker.DisplayDate.Date this returns a DateTime value with the TimeOfDay component set to 00:00.00
Edit
Usually you use an SQL Statement to insert or update values in the database. You should use a parametrized SQL statement with an parameter of type DateTime. The SQL API will take care of the conversion form DateTime (.Net type) to your SQL Date type and strip all time information away. It is a good idea to set the time component to 00:00:00 however to avoid any strange "roundings".
Use ToShortDateString() at the end something like:
var date = datePicker.SelectedDate.Value.Date.ToShortDateString();
MessageBox.Show(date.ToString());