|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
date format in parametercmdUpdate.Parameters.Add ("@invoicedate", SqlDbType.NVarChar, 40).Value = dr["invoice date"].ToString(); IT is updating a field in a SQL2K database. The database field is of type char and not date. The data in dr["invoice date"] looks like this: 12/29/2004 But when the field is updated the database looks like this. 2/4/2004 12:00:00 AM . How do I get it formated correctly. Thanks Paul Why do you use NVarChar instead of DateTime?
You'll avoid this kind of problems.. -- Show quoteHide quoteMiha Markic [MVP C#] - RightHand .NET consulting & development SLODUG - Slovene Developer Users Group www.rthand.com <Paulb1us@newsgroup.nospam> wrote in message news:E9972E39-7E45-4751-A50F-D654D46C6DF6@microsoft.com... >I have SQL parameter that looks like this: > > cmdUpdate.Parameters.Add > ("@invoicedate", SqlDbType.NVarChar, 40).Value = dr["invoice > date"].ToString(); > > IT is updating a field in a SQL2K database. > The database field is of type char and not date. > > The data in dr["invoice date"] looks like this: > > 12/29/2004 > > But when the field is updated the database looks like this. > > 2/4/2004 12:00:00 AM . > > How do I get it formated correctly. > > Thanks > > Paul > > > > Check the type of dr["invoice date"] locally and make sure it's not a
DateTime - if it is I think ShortDateString() instead of ToString() should work. BTW, I totally agree with Miha here - you really don't get any benefit by using NVarchar for dates and in all likelihood- you'll need to sort on them sometime in the future and you'll just have to cast everything again. -- Show quoteHide quoteW.G. Ryan MVP (Windows Embedded) TiBA Solutions www.tibasolutions.com | www.devbuzz.com | www.knowdotnet.com <Paulb1us@newsgroup.nospam> wrote in message news:E9972E39-7E45-4751-A50F-D654D46C6DF6@microsoft.com... > I have SQL parameter that looks like this: > > cmdUpdate.Parameters.Add > ("@invoicedate", SqlDbType.NVarChar, 40).Value = dr["invoice > date"].ToString(); > > IT is updating a field in a SQL2K database. > The database field is of type char and not date. > > The data in dr["invoice date"] looks like this: > > 12/29/2004 > > But when the field is updated the database looks like this. > > 2/4/2004 12:00:00 AM . > > How do I get it formated correctly. > > Thanks > > Paul > > > > Hi Paul,
I agree with William to use DateTime.ToShortDateString method to achieve this. We can first cast the field value to a DateTime object and then call ToShortDateString like: DateTime dt = (DateTime)dr["invoice date"]; cmdUpdate.Parameters.Add("@invoicedate", SqlDbType.NVarChar, 40).Value = dt.ToShortDateString(); Of course using a DateTime field in database is a better choice as Miha suggested. Kevin Yu ======= "This posting is provided "AS IS" with no warranties, and confers no rights." Thank you Kevin,
That works great. I agree it should be DateTime Filed in the DB, all of my other date fields are just that. But in this paticular instance the origianl design of the schema won't allow it. Show quoteHide quote "Kevin Yu [MSFT]" wrote: > Hi Paul, > > I agree with William to use DateTime.ToShortDateString method to achieve > this. We can first cast the field value to a DateTime object and then call > ToShortDateString like: > > DateTime dt = (DateTime)dr["invoice date"]; > cmdUpdate.Parameters.Add("@invoicedate", SqlDbType.NVarChar, 40).Value = > dt.ToShortDateString(); > > Of course using a DateTime field in database is a better choice as Miha > suggested. > > Kevin Yu > ======= > "This posting is provided "AS IS" with no warranties, and confers no > rights." > >
Other interesting topics
Data reader already open?
Question about SQLDataReader SqlDataReader.GetChar(n) failed because of Specified method unsupp OleDbParameter doesn't update value correctly Question on how I can use ForeignKeyConstraint What is the Best way to fetch the most recent updated row in a database Cant get Stored Procedure connection string right?? Dataset created in Code cannot be used in Crystal Reports designer RowState Confused about MissingSchemaAction.AddWithKey |
|||||||||||||||||||||||