Datetime problems between c# and sql


I'm currently working on an SQL DB where one of the fields(Start_Time) in my shift table is a datetime datatype.

When I bind the values in my Start_Time field to a combobox in my front end it retrieves the values in the following format :   2006/03/22 03:40:21 PM


However if I run a normal select * query on my Shift Table  the same value as above is returned in the following format :  2006-03-22 15:40:21.000

What I'm trying to do is to use the selectd value in my combobox as a parameter in my storedprocedures but I am getting the [ 2006/03/22 03:40:21 PM]
value and not the [2006-03-22 15:40:21.000 ] value that my DB requires ! Please help!!
It`s driving me nuts!!
ChardiPAsked:
Who is Participating?
 
dkloeckCommented:
when you write your date in the combobox it becomes a DateTime,
you can just set the format of the DateTime

here is the msdn example on formats for DateTime:

DateTime july28 = new DateTime(1979, 7, 28, 5, 23, 15, 16);

string[] july28Formats = july28.GetDateTimeFormats();

// Print out july28 in all DateTime formats using the
// default culture.
foreach (string format in july28Formats) {
    System.Console.WriteLine(format);
}

IFormatProvider culture =
    new System.Globalization.CultureInfo("fr-FR", true);
// Get the short date formats using the "fr-FR" culture.
string [] frenchJuly28Formats =
            july28.GetDateTimeFormats('d', culture);

// Print out july28 in various formats using "fr-FR" culture.
System.Console.WriteLine("Starting fr-FR formats");
foreach (string format in frenchJuly28Formats) {
    System.Console.WriteLine(format);
}

0
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
see here for how to use parameterized queries (including datetime) which is the best way:
http://www.codeproject.com/cs/database/SqlSprocUtil.asp
0
 
ChardiPAuthor Commented:
Hi Thanks for the response but my problem is not the stored procedure .
The problem is that the field returns two different types of formats for my datetime field
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AGBrownCommented:
You can also use DateTime.ParseExact to parse your datetime.

The thing is that when you run the select query on your table, you are only seeing the string representation of the value. As dkloeck says, you need to take it from a string value into a datetime, and pass a datetime into your stored procedure to ensure that you get the correct comparison.

Andy
0
 
mrichmonCommented:
If your C# is sending the data to the stored procedure using the DateTime parameter and then the stored procedure is accepting it as a DateTime, then it should convert between the two.  I have not had problems with this.

However, you could always format your date into the parts as shown by dkloeck or into a string and then do a cast and convert in SQL to ensure consistent formatting.
0
 
mrichmonCommented:
I think a split between these three comments:
dkloeck & AGBrown & mrichmon
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.