Link to home
Start Free TrialLog in
Avatar of Abdurahman Almatrodi
Abdurahman AlmatrodiFlag for Saudi Arabia

asked on

DateTime in MS SQL 2000 With C#


I've an application developed in C# connecting to MS SQL 2000. And, some user have dd/mm/yyyy hh:mm AM format, others have mm/dd/yyyy. I got error when I try to insert an new record or work with date.

What is the shortest way to solve this issue?
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

use SqlCommand and SqlParameter.
in c# part, ensure you can parse the user input into a c# date, from there it's dead easy:
    SqlCommand cmd = new SqlCommand("SELECT * FROM table_name WHERE dateTimeField > @SomeDate");
    cmd.Parameters.AddWithValue("@SomeDate", DateTime.Today);

Open in new window

Avatar of Abdurahman Almatrodi

ASKER


I am using such as:

String.Format("INSERT INTO MyTable (RecDate) VALUES ({0}) WHERE RecID = {1}", '19/08/2009', 21)

The error is coming from SQL 2000 itself.


ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial

Thanks. It works well.

May you guide me to a link that explain this matter.

I would start with this link, for the cast/convert function:
http://msdn.microsoft.com/en-us/library/ms187928.aspx
Dear Angel

What I need to know is how String.Format change the date.

Thanks