From: C on 29 Mar 2010 12:53 sqlParameterLastUpdatedDate = new SqlParameter(); sqlParameterLastUpdatedDate.ParameterName = "@lastUpdatedDate"; sqlParameterLastUpdatedDate.SqlDbType = SqlDbType.DateTime; sqlParameterLastUpdatedDate.Value = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")); sqlParameterLastUpdatedDate.Direction = ParameterDirection.Input; When I add this to parameter to my command object I get "Cannot conver String to DateTime" beause I am assigning a string to the Value. I need my date in the above format. How can I get this format without having it as a string? Any help appreciated.
From: Alberto Poblacion on 29 Mar 2010 13:26 "C" <C(a)discussions.microsoft.com> wrote in message news:177F0D49-8ECD-424D-BA4C-52AC0E2F8540(a)microsoft.com... > sqlParameterLastUpdatedDate.Value = > DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")); This is terribly silly. Why would you cnvert the DateTime into a String, just to parse the string back into a DateTime? > When I add this to parameter to my command object I get "Cannot conver > String to DateTime" beause I am assigning a string to the Value. No. You get the error in the "Parse" method because it doesn't know how to interpret the string that you passed. > I need my > date in the above format. That doesn't make sense. If the parameter has its type set to SqlDbType.DateTime, then it needs a DateTime object, which only contains binary data. It does not have any format at all. If the parameter has its type correctly set to match the field in the database, then the field is also a DATETIME, which also stores a binary value with no formatting. Therefore, there is no place at all in this code where a format would be needed. Just assign the value like this: sqlParameterLastUpdatedDate.Value = DateTime.Now;
From: Andy O'Neill on 29 Mar 2010 14:13 "C" <C(a)discussions.microsoft.com> wrote in message news:177F0D49-8ECD-424D-BA4C-52AC0E2F8540(a)microsoft.com... > sqlParameterLastUpdatedDate = new SqlParameter(); > sqlParameterLastUpdatedDate.ParameterName = > "@lastUpdatedDate"; > sqlParameterLastUpdatedDate.SqlDbType = > SqlDbType.DateTime; > sqlParameterLastUpdatedDate.Value = > DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")); > sqlParameterLastUpdatedDate.Direction = > ParameterDirection.Input; > > When I add this to parameter to my command object I get "Cannot conver > String to DateTime" beause I am assigning a string to the Value. I need my > date in the above format. How can I get this format without having it as a > string? > > Any help appreciated. When you display a datetime variable, you can format it however you like. When you try and update it in the database you should be giving it a datetime variable rather than string. So worry about how you want it formatted when you display it, only.
From: Mr. Arnold on 29 Mar 2010 14:24 "C" wrote: > sqlParameterLastUpdatedDate = new SqlParameter(); > sqlParameterLastUpdatedDate.ParameterName = > "@lastUpdatedDate"; > sqlParameterLastUpdatedDate.SqlDbType = > SqlDbType.DateTime; > sqlParameterLastUpdatedDate.Value = > DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff")); > sqlParameterLastUpdatedDate.Direction = > ParameterDirection.Input; > > When I add this to parameter to my command object I get "Cannot conver > String to DateTime" beause I am assigning a string to the Value. I need my > date in the above format. How can I get this format without having it as a > string? > > Any help appreciated. private DateTime dt = new DateTime(); dt = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss:fff"); SqlDbType.DateTime; sqlParameterLastUpdatedDate.Value = dt but on the other hand SqlDbType.DateTime; sqlParameterLastUpdatedDate.Value = DateTime.Now; Yor worry about the formating of ('yyyy-mm-dd') when you get the data.
|
Pages: 1 Prev: Adding a method in InitializeComponent() using codedom from userc Next: correction |