ASP.NET Passing Inconsistent DateTime Format to SQL Server [duplicate] - asp.net

This question already has answers here:
RDLC - "String Not Recognized As A Valid DateTime" Error
(2 answers)
Closed 2 years ago.
I know this is an age-old question but I'm not exactly new to it and I haven't found an answer which helps me so far!
I've got an ASP.NET site which displays data from an SQL Server 2005 database. At the top of the page are 'Date From' and 'Date To' textboxes which are used to filter the data shown beneath.
The data is displayed using an SqlDataSource, and the two dates are passed as parameters to a Stored Procedure. The textboxes display the dates and accept input in UK date format (dd/MM/yyyy) and it works fine.
Now I've added a new page with exactly the same setup, displaying slightly different data. In the backend I created a new Stored Procedure by copying and pasting my original one, it's almost identical. And yet on this page I get errors with my dates because they're being read as MM/dd/yyyy, meaning that today's date, for example, 15th August 2011, is passed as 15/08/2011 and isn't a valid date.
I've checked over everything and I can't understand why this should work on one page and not another, especially when I've basically just copied all of the original code and tweaked it slightly. Can anyone suggest anything I can check that I might not have thought of?

The text boxes are strings, which are in the thread local which is set from the browser (langauges).
Convert them to aa DateTime object first, using a local aware transformation, then write the dateTime object to the server. NEVER (!) deal with strings to sql server unless you format them in ISO independent form (2011-08-17 23:52:11).
But in general, ASp.NET will show dates, times, numbers in the local langauge of the browser. Either turn that off, or deal with it. It is nice for the user. So, check locales - user, server process. What is the thread current locale?

You need to use the SQL convert in stored procedure like:
convert(varchar, #yourdateParameter, 103) for format dd/mm/yyyy
or
convert(varchar, #yourdateParameter, 101) for format mm/dd/yyyy
Hope this helps.

Related

MS Access - change date format of local database only

I'm based in the UK and I'm developing an MS Access 2016 database for an American client.
How can I change the language settings for this database only and not Access in general so, for example in table design view field format options for date appear appear as mm/dd/yyyy, mm/dd/yy etc rather than dd/mm/yyyy, dd/mm/yy?
I know I can format the data post-hoc using SQL or VBA but I'd prefer to set the behaviour as default at the design stage to limit the potential for error.
Many thanks.
Update, here's the options currently presented for the field format:
You can set the Format property of table fields and textboxes to: mm/dd/yyyy
But that will not change the behaviour of, say, DateValue which still will read a date string like "6/7/2016" as 2016-07-06 using your Windows settings.
So the real answer is that you can not. And neither should you need it - if you don't apply custom formats, your database will display dates as m/d/yyyy when reaching your American client.
If you want to see your application in action, install a virtual machine with US settings, and test your application in this environment.

Convert multiple gridview date fields into one format

I'm spinning my wheels right now and what I'm pretty sure is an easy answer, I just can't see at the moment. What I'm attempting to do take two sets of converted dates in a database and I want to present them in a grid view as one single date format.
Right now the two date formats are as follows:
YYYYMMDD
mon dd yyyy hh:miAM/PM
I want to convert them all into one format. For giggles, lets just convert them all to 112 (YYYYMMDD).
Now I want to present this all in the same SELECT statement that populates my gridview with other columns of data of various forms lots of text, data entry, long characters, etc.
The thing I haven't been able to wrap my head around is how to put this into a SELECT statement that includes all the rest of the information that populates the gridview.
So a normal SELECT statement (into all nvarchar(max) fields...the table is formed this way because we deal with lots of non standard data importing like Unix) would look like this (this is just a sample database):
SELECT [record_number], [price], [product], [description], [date_of_order], [customer_id] [comments]from sample_database
When presenting the 'date_of_order' field, I figured I need to use a CAST or CONVERT with the proper conversion numbers. However, after looking online I'm scratching my head on how to do this as most of the somewhat relevant information is focused around converting with the GETDATE option instead of leveraging the date that's already written to the database (the information is entered on a different part of the website).
I'm just looking for a pointer or a suggestion in the right direction to use a SELECT statement in my gridview (along with all of the other fields) that can convert any/all of the entries into one common date format. I know I'm missing something simple here but I don't know what.
I should note that I don't want to change them in the Database just when presenting them in the Gridview.
Use the following Convert SQL Function around all of the date fields you require, be warned that this can cause errors if the value is not a correctly formatted date.
CONVERT(datetime, [OldDate], 112)
Found here.
Also look here for more information on the Convert Function

How do I get the date AND TIME to show up in a Crystal report?

I'm working with Crystal Reports for Visual Studio 2010. The data is in a SQL Server 2005 database. The field is a datetime field, within the database/table. It has both date and time in it. But my report refuses to see that field as anything other than a date field. Huh??? According to this article on C# Corner, there should be a Date, a Time and a Date And Time tab when customizing the display. No there isn't; the only data type tab I see, when I try to customize it, is Date.
So, how do I get the date and time to show up from a datetime field returned by the stored procedure, when CR for VS 2010 only wants to treat it as a Date field???
We finally found the solution. The report I'm working with is really old. It was started by someone over 10 years ago, using who knows what version of Crystal Reports was around then. My colleague decided to start a whole new report and drag the field first from the table, and then from the stored procedure that's used for the report, onto the new report. Well, the data type was now DateTime! The best we can figure is the original .rpt file must not have had a DateTime data type (that field in the table has been a DateTime for 15+ years). We even discovered that creating a new report based upon this existing report, doesn't resolve the issue; it still sees the field as a Date data type only. So we've got to create a new report and start over from stratch. Frankly, pretty awful to do, just for 1 field in the report, but oh well.
This shows you how to set CR to set how it interprets DateTime as either Date or DateTime:
SAP Community Network Issue

How to get date from datetime in crystal report?

I have the datetime format in my sqldatabase was "2012-07-21 00:00:00.000". I need the date(21) only. Am trying to convert but it was showing database error for this particular field.
My Code is:
DatePart('d',CDate({tblFiles.Filed_Date}));
It's a problem with the connection of your report to the database. Make sure to set the data source correctly. You may start by verifying the report.
Okay I had this problem in the past, which is dealt with some bits of conversions, here is an example to how I dealt with this issue, here I did not have date but I had time in my database, you need to convert it to text and replace the number of zeros you see and display x as Date(x); and then right click the field and format it to just get the data, by right clicking it. hope this helps.
LOCAL STRINGVAR x := REPLACE(totext({DETAILED_FOR_EMPLOYEE.LOGIN_TIME}), '.0000000', '')

Data Binding DateTime to a label->text converts the value to local time

I am working in C++ .net. I have a custom control that shows the contents of a dataBase with a bindingsource in between. I use labels in the control to bind the text property to the data in the dataset through a bindingsource with the following code:
mycontrol->Controls[i]->DataBindings->Add((gcnew System::Windows::Forms::Binding(L"Text",
this->bindingSourceRelRev_Data, mycolumn,true, System::Windows::Forms::DataSourceUpdateMode::OnValidation,
nullptr, L"t")));
Please, note that the code is an excerpt of a loop that iterates on all the controls, therefore the control[i], which would be the label control, and the variable mycolumn, which would be the column of the dataset to be binded to. It works fine apart of an unwanted behaviour that I haven't yet found the way to change.
The sofware runs in different parts of the world but the records must be in local time. The database stores the time in localtime as a DateTime value but the dataBinding interprets it as UTC and shows the time in the label with the timezone difference applied. For example, if the data is 24/06/2012 16:40, it will show 24/06/2012 22:40 in a 4 hours difference time zone.
I need it to show just the value as it was stored, without changes.
I can think of different ways to go arround, but non elegant:
-capture the text update event of the label and un-do the change...
-Store the date as String in the DB...
-fill manually the labels...
-...
If I bind without formating, it will not change the value, but then I have the raw datetime string on the label...
Please, any magic I am missing in the binding so that it will not assume it has to change the time zone?
Thank you very much in advance!
Adan
well, it seems that the problem was not actually on the binding but in the dataset itself. The columns' property DateTimeMode was set as undefinedlocal and this was serializing the dataimput and converting it to the local time of the machine viewing the data. Just changed the property to undefined and it works now as I wanted, just taking the date from the data unmodified.
I was confused by the documentation on the format provider for DateTime, which states that it serializes the date. Perhaps I missunderstood it... It actually doesn't.
Thank you very much anyway!

Resources