|
dev
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Convertin dbnull to stringHello,
I am trying to insert a new datarow. Everything works fine except for three columns that have datetime data types. I keep getting this error message "System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <> in 'columnname' Column. Expected type is DateTime." Is there any way of converting a dbnull to string using vb.net? Hi,
DbNull is a special value and not an empty string. In a case of DateTime datatype you cannot save empty string into this field, because it is not a character data type. You have to store Null or valid date. Why do you need to store an empty string? It is actually a great advantage to use Nulls instead of empty strings, because you know for sure that field does not hold a value in this case Show quoteHide quote "40forty" <40fo***@discussions.microsoft.com> wrote in message news:DC3D8AB8-946E-4FEB-AFEE-A0138CEF9619@microsoft.com... > Hello, > > I am trying to insert a new datarow. Everything works fine except for > three > columns that have datetime data types. I keep getting this error message > "System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store > <> > in 'columnname' Column. Expected type is DateTime." > > Is there any way of converting a dbnull to string using vb.net? I am sorry, here is my code:
dr = DS.Tables("UserAccount").NewRow dr.Item("UserName") = txtUsrName.Text dr.Item("EmailAddress") = txtEmailID.Text dr.Item("Name") = txtDscript.Text dr.Item("Directory") = txtDefaultDir.Text dr.Item("Account") = txtAccount.Text dr.Item("UIC") = txtUIC.Text dr.Item("ChargeBack") = txtChargeBack.Text dr.Item("DateCreated") = txtDtCreated.Text dr.Item("DateDeleted") = txtDtDeleted.Text ---Here is where I get the error Additional information: System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider) at System.Convert.ToDateTime(String value, IFormatProvider provider) at System.String.System.IConvertible.ToDateTime(IFormatProvider provider) at System.Convert.ToDateTime(Object value) at System.Data.Common.DateTimeStorage.Set(Int32 record, Object value) at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <> in DateDeleted Column. Expected type is DateTime. How would I be able to convert the dbnull to a string? Show quoteHide quote "Val Mazur (MVP)" wrote: > Hi, > > DbNull is a special value and not an empty string. In a case of DateTime > datatype you cannot save empty string into this field, because it is not a > character data type. You have to store Null or valid date. Why do you need > to store an empty string? It is actually a great advantage to use Nulls > instead of empty strings, because you know for sure that field does not hold > a value in this case > -- > Val Mazur > Microsoft MVP > > http://xport.mvps.org > > > > "40forty" <40fo***@discussions.microsoft.com> wrote in message > news:DC3D8AB8-946E-4FEB-AFEE-A0138CEF9619@microsoft.com... > > Hello, > > > > I am trying to insert a new datarow. Everything works fine except for > > three > > columns that have datetime data types. I keep getting this error message > > "System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store > > <> > > in 'columnname' Column. Expected type is DateTime." > > > > Is there any way of converting a dbnull to string using vb.net? > > > I think what you should do is to assign dbnull value to
row item when date value is empty. It's like: If txtDtDeleted.Text.Trim.Equals("") Then dr.Item("DateDeleted") = DBNull.Value Else dr.Item("DateDeleted") = txtDtDeleted.Text EndIf HTH Elton Wang elton_w***@hotmail.com >-----Original Message----- Here is where I get >I am sorry, here is my code: > >dr = DS.Tables("UserAccount").NewRow > dr.Item("UserName") = txtUsrName.Text > dr.Item("EmailAddress") = txtEmailID.Text > dr.Item("Name") = txtDscript.Text > dr.Item("Directory") = txtDefaultDir.Text > dr.Item("Account") = txtAccount.Text > dr.Item("UIC") = txtUIC.Text > dr.Item("ChargeBack") = txtChargeBack.Text > dr.Item("DateCreated") = txtDtCreated.Text > > dr.Item("DateDeleted") = txtDtDeleted.Text --- >the error was not recognized as > >Additional information: System.FormatException: String Show quoteHide quote >a valid DateTime. record, Object value)> at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, >DateTimeStyles styles) > at System.DateTime.Parse(String s, IFormatProvider provider, >DateTimeStyles styles) > at System.DateTime.Parse(String s, IFormatProvider provider) > at System.Convert.ToDateTime(String value, IFormatProvider provider) > at System.String.System.IConvertible.ToDateTime (IFormatProvider provider) > at System.Convert.ToDateTime(Object value) > at System.Data.Common.DateTimeStorage.Set(Int32 > at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't >store <> in DateDeleted Column. Expected type is case of DateTime DateTime. > > >How would I be able to convert the dbnull to a string? > >"Val Mazur (MVP)" wrote: > >> Hi, >> >> DbNull is a special value and not an empty string. In a >> datatype you cannot save empty string into this field, because it is not a >> character data type. You have to store Null or valid date. Why do you need >> to store an empty string? It is actually a great advantage to use Nulls >> instead of empty strings, because you know for sure that field does not hold >> a value in this case A0138CEF9***@microsoft.com...>> -- >> Val Mazur >> Microsoft MVP >> >> http://xport.mvps.org >> >> >> >> "40forty" <40fo***@discussions.microsoft.com> wrote in message >> news:DC3D8AB8-946E-4FEB-AFEE- >> > Hello, works fine except for >> > >> > I am trying to insert a new datarow. Everything >> > three getting this error message>> > columns that have datetime data types. I keep >> > "System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store >> > <> using vb.net? >> > in 'columnname' Column. Expected type is DateTime." >> > >> > Is there any way of converting a dbnull to string Show quoteHide quote >> >> >> >. > Elton wang, it worked, but I think I may be mis understood. I will try this
again, here is the code: dr = DS.Tables("UserAccount").NewRow dr.Item("UserName") = usrN dr.Item("EmailAddress") = emlAdd dr.Item("Name") = nm dr.Item("Directory") = dirt dr.Item("Account") = acc dr.Item("UIC") = uc dr.Item("ChargeBack") = cbk dr.Item("DateCreated") = dcr Get the error dr.Item("DateDeleted") = ddl Get the error dr.Item("VSADMINID") = vsminid dr.Item("VSADMINDelete") = vsminde Get the error dr.Item("VSADMINCreate") = vsmindc Get the error DS.Tables("UserAccount").Rows.Add(dr) Everything works...so long as the user fills out these required text boxes. I f you leave the textboxes blank, you get the error messege Additional information: System.FormatException: String was not recognized as a valid DateTime. at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider, DateTimeStyles styles) at System.DateTime.Parse(String s, IFormatProvider provider) at System.Convert.ToDateTime(String value, IFormatProvider provider) at System.String.System.IConvertible.ToDateTime(IFormatProvider provider) at System.Convert.ToDateTime(Object value) at System.Data.Common.DateTimeStorage.Set(Int32 record, Object value) at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't store <> in DateDeleted Column. Expected type is DateTime. I am trying to get the textboxes to insert whether I input data or leave it blank. Can anyone help me? Show quoteHide quote "Elton Wang" wrote: > I think what you should do is to assign dbnull value to > row item when date value is empty. It's like: > > If txtDtDeleted.Text.Trim.Equals("") Then > dr.Item("DateDeleted") = DBNull.Value > Else > dr.Item("DateDeleted") = txtDtDeleted.Text > EndIf > > HTH > > Elton Wang > elton_w***@hotmail.com > > >-----Original Message----- > >I am sorry, here is my code: > > > >dr = DS.Tables("UserAccount").NewRow > > dr.Item("UserName") = txtUsrName.Text > > dr.Item("EmailAddress") = txtEmailID.Text > > dr.Item("Name") = txtDscript.Text > > dr.Item("Directory") = txtDefaultDir.Text > > dr.Item("Account") = txtAccount.Text > > dr.Item("UIC") = txtUIC.Text > > dr.Item("ChargeBack") = txtChargeBack.Text > > dr.Item("DateCreated") = txtDtCreated.Text > > > > dr.Item("DateDeleted") = txtDtDeleted.Text --- > Here is where I get > >the error > > > >Additional information: System.FormatException: String > was not recognized as > >a valid DateTime. > > at System.DateTimeParse.Parse(String s, > DateTimeFormatInfo dtfi, > >DateTimeStyles styles) > > at System.DateTime.Parse(String s, IFormatProvider > provider, > >DateTimeStyles styles) > > at System.DateTime.Parse(String s, IFormatProvider > provider) > > at System.Convert.ToDateTime(String value, > IFormatProvider provider) > > at System.String.System.IConvertible.ToDateTime > (IFormatProvider provider) > > at System.Convert.ToDateTime(Object value) > > at System.Data.Common.DateTimeStorage.Set(Int32 > record, Object value) > > at System.Data.DataColumn.set_Item(Int32 record, > Object value)Couldn't > >store <> in DateDeleted Column. Expected type is > DateTime. > > > > > >How would I be able to convert the dbnull to a string? > > > >"Val Mazur (MVP)" wrote: > > > >> Hi, > >> > >> DbNull is a special value and not an empty string. In a > case of DateTime > >> datatype you cannot save empty string into this field, > because it is not a > >> character data type. You have to store Null or valid > date. Why do you need > >> to store an empty string? It is actually a great > advantage to use Nulls > >> instead of empty strings, because you know for sure > that field does not hold > >> a value in this case > >> -- > >> Val Mazur > >> Microsoft MVP > >> > >> http://xport.mvps.org > >> > >> > >> > >> "40forty" <40fo***@discussions.microsoft.com> wrote in > message > >> news:DC3D8AB8-946E-4FEB-AFEE- > A0138CEF9***@microsoft.com... > >> > Hello, > >> > > >> > I am trying to insert a new datarow. Everything > works fine except for > >> > three > >> > columns that have datetime data types. I keep > getting this error message > >> > "System.Data.DataColumn.set_Item(Int32 record, Object > value)Couldn't store > >> > <> > >> > in 'columnname' Column. Expected type is DateTime." > >> > > >> > Is there any way of converting a dbnull to string > using vb.net? > >> > >> > >> > >. > > > Hi As Elton suggested before assigning of the value to the DateTime field,
check if it is an empty string or not or you could check if it is a date and if it is not, then assign Null, like if isdate(dcr) then dr.Item("DateCreated") = dcr else dr.Item("DateCreated")=DbNull.Value Endif Show quoteHide quote "40forty" <40fo***@discussions.microsoft.com> wrote in message news:1CF061A9-0CB3-4BD0-9A56-0769ECAB60B0@microsoft.com... > Elton wang, it worked, but I think I may be mis understood. I will try > this > again, here is the code: > > dr = DS.Tables("UserAccount").NewRow > dr.Item("UserName") = usrN > dr.Item("EmailAddress") = emlAdd > dr.Item("Name") = nm > dr.Item("Directory") = dirt > dr.Item("Account") = acc > dr.Item("UIC") = uc > dr.Item("ChargeBack") = cbk > dr.Item("DateCreated") = dcr Get the error > dr.Item("DateDeleted") = ddl Get the error > dr.Item("VSADMINID") = vsminid > dr.Item("VSADMINDelete") = vsminde Get the error > dr.Item("VSADMINCreate") = vsmindc Get the error > > DS.Tables("UserAccount").Rows.Add(dr) > Everything works...so long as the user fills out these required text > boxes. > I f you leave the textboxes blank, you get the error messege > > Additional information: System.FormatException: String was not recognized > as > a valid DateTime. > at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, > DateTimeStyles styles) > at System.DateTime.Parse(String s, IFormatProvider provider, > DateTimeStyles styles) > at System.DateTime.Parse(String s, IFormatProvider provider) > at System.Convert.ToDateTime(String value, IFormatProvider provider) > at System.String.System.IConvertible.ToDateTime(IFormatProvider > provider) > at System.Convert.ToDateTime(Object value) > at System.Data.Common.DateTimeStorage.Set(Int32 record, Object value) > at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't > store <> in DateDeleted Column. Expected type is DateTime. > > I am trying to get the textboxes to insert whether I input data or leave > it > blank. Can anyone help me? > "Elton Wang" wrote: > >> I think what you should do is to assign dbnull value to >> row item when date value is empty. It's like: >> >> If txtDtDeleted.Text.Trim.Equals("") Then >> dr.Item("DateDeleted") = DBNull.Value >> Else >> dr.Item("DateDeleted") = txtDtDeleted.Text >> EndIf >> >> HTH >> >> Elton Wang >> elton_w***@hotmail.com >> >> >-----Original Message----- >> >I am sorry, here is my code: >> > >> >dr = DS.Tables("UserAccount").NewRow >> > dr.Item("UserName") = txtUsrName.Text >> > dr.Item("EmailAddress") = txtEmailID.Text >> > dr.Item("Name") = txtDscript.Text >> > dr.Item("Directory") = txtDefaultDir.Text >> > dr.Item("Account") = txtAccount.Text >> > dr.Item("UIC") = txtUIC.Text >> > dr.Item("ChargeBack") = txtChargeBack.Text >> > dr.Item("DateCreated") = txtDtCreated.Text >> > >> > dr.Item("DateDeleted") = txtDtDeleted.Text --- >> Here is where I get >> >the error >> > >> >Additional information: System.FormatException: String >> was not recognized as >> >a valid DateTime. >> > at System.DateTimeParse.Parse(String s, >> DateTimeFormatInfo dtfi, >> >DateTimeStyles styles) >> > at System.DateTime.Parse(String s, IFormatProvider >> provider, >> >DateTimeStyles styles) >> > at System.DateTime.Parse(String s, IFormatProvider >> provider) >> > at System.Convert.ToDateTime(String value, >> IFormatProvider provider) >> > at System.String.System.IConvertible.ToDateTime >> (IFormatProvider provider) >> > at System.Convert.ToDateTime(Object value) >> > at System.Data.Common.DateTimeStorage.Set(Int32 >> record, Object value) >> > at System.Data.DataColumn.set_Item(Int32 record, >> Object value)Couldn't >> >store <> in DateDeleted Column. Expected type is >> DateTime. >> > >> > >> >How would I be able to convert the dbnull to a string? >> > >> >"Val Mazur (MVP)" wrote: >> > >> >> Hi, >> >> >> >> DbNull is a special value and not an empty string. In a >> case of DateTime >> >> datatype you cannot save empty string into this field, >> because it is not a >> >> character data type. You have to store Null or valid >> date. Why do you need >> >> to store an empty string? It is actually a great >> advantage to use Nulls >> >> instead of empty strings, because you know for sure >> that field does not hold >> >> a value in this case >> >> -- >> >> Val Mazur >> >> Microsoft MVP >> >> >> >> http://xport.mvps.org >> >> >> >> >> >> >> >> "40forty" <40fo***@discussions.microsoft.com> wrote in >> message >> >> news:DC3D8AB8-946E-4FEB-AFEE- >> A0138CEF9***@microsoft.com... >> >> > Hello, >> >> > >> >> > I am trying to insert a new datarow. Everything >> works fine except for >> >> > three >> >> > columns that have datetime data types. I keep >> getting this error message >> >> > "System.Data.DataColumn.set_Item(Int32 record, Object >> value)Couldn't store >> >> > <> >> >> > in 'columnname' Column. Expected type is DateTime." >> >> > >> >> > Is there any way of converting a dbnull to string >> using vb.net? >> >> >> >> >> >> >> >. >> > >> Val,
Thanks that did the trick. ________________________________________________________________ Show quoteHide quote "Val Mazur (MVP)" wrote: > Hi As Elton suggested before assigning of the value to the DateTime field, > check if it is an empty string or not or you could check if it is a date and > if it is not, then assign Null, like > > if isdate(dcr) then > dr.Item("DateCreated") = dcr > else > dr.Item("DateCreated")=DbNull.Value > Endif > > -- > Val Mazur > Microsoft MVP > > http://xport.mvps.org > > > > "40forty" <40fo***@discussions.microsoft.com> wrote in message > news:1CF061A9-0CB3-4BD0-9A56-0769ECAB60B0@microsoft.com... > > Elton wang, it worked, but I think I may be mis understood. I will try > > this > > again, here is the code: > > > > dr = DS.Tables("UserAccount").NewRow > > dr.Item("UserName") = usrN > > dr.Item("EmailAddress") = emlAdd > > dr.Item("Name") = nm > > dr.Item("Directory") = dirt > > dr.Item("Account") = acc > > dr.Item("UIC") = uc > > dr.Item("ChargeBack") = cbk > > dr.Item("DateCreated") = dcr Get the error > > dr.Item("DateDeleted") = ddl Get the error > > dr.Item("VSADMINID") = vsminid > > dr.Item("VSADMINDelete") = vsminde Get the error > > dr.Item("VSADMINCreate") = vsmindc Get the error > > > > DS.Tables("UserAccount").Rows.Add(dr) > > Everything works...so long as the user fills out these required text > > boxes. > > I f you leave the textboxes blank, you get the error messege > > > > Additional information: System.FormatException: String was not recognized > > as > > a valid DateTime. > > at System.DateTimeParse.Parse(String s, DateTimeFormatInfo dtfi, > > DateTimeStyles styles) > > at System.DateTime.Parse(String s, IFormatProvider provider, > > DateTimeStyles styles) > > at System.DateTime.Parse(String s, IFormatProvider provider) > > at System.Convert.ToDateTime(String value, IFormatProvider provider) > > at System.String.System.IConvertible.ToDateTime(IFormatProvider > > provider) > > at System.Convert.ToDateTime(Object value) > > at System.Data.Common.DateTimeStorage.Set(Int32 record, Object value) > > at System.Data.DataColumn.set_Item(Int32 record, Object value)Couldn't > > store <> in DateDeleted Column. Expected type is DateTime. > > > > I am trying to get the textboxes to insert whether I input data or leave > > it > > blank. Can anyone help me? > > "Elton Wang" wrote: > > > >> I think what you should do is to assign dbnull value to > >> row item when date value is empty. It's like: > >> > >> If txtDtDeleted.Text.Trim.Equals("") Then > >> dr.Item("DateDeleted") = DBNull.Value > >> Else > >> dr.Item("DateDeleted") = txtDtDeleted.Text > >> EndIf > >> > >> HTH > >> > >> Elton Wang > >> elton_w***@hotmail.com > >> > >> >-----Original Message----- > >> >I am sorry, here is my code: > >> > > >> >dr = DS.Tables("UserAccount").NewRow > >> > dr.Item("UserName") = txtUsrName.Text > >> > dr.Item("EmailAddress") = txtEmailID.Text > >> > dr.Item("Name") = txtDscript.Text > >> > dr.Item("Directory") = txtDefaultDir.Text > >> > dr.Item("Account") = txtAccount.Text > >> > dr.Item("UIC") = txtUIC.Text > >> > dr.Item("ChargeBack") = txtChargeBack.Text > >> > dr.Item("DateCreated") = txtDtCreated.Text > >> > > >> > dr.Item("DateDeleted") = txtDtDeleted.Text --- > >> Here is where I get > >> >the error > >> > > >> >Additional information: System.FormatException: String > >> was not recognized as > >> >a valid DateTime. > >> > at System.DateTimeParse.Parse(String s, > >> DateTimeFormatInfo dtfi, > >> >DateTimeStyles styles) > >> > at System.DateTime.Parse(String s, IFormatProvider > >> provider, > >> >DateTimeStyles styles) > >> > at System.DateTime.Parse(String s, IFormatProvider > >> provider) > >> > at System.Convert.ToDateTime(String value, > >> IFormatProvider provider) > >> > at System.String.System.IConvertible.ToDateTime > >> (IFormatProvider provider) > >> > at System.Convert.ToDateTime(Object value) > >> > at System.Data.Common.DateTimeStorage.Set(Int32 > >> record, Object value) > >> > at System.Data.DataColumn.set_Item(Int32 record, > >> Object value)Couldn't > >> >store <> in DateDeleted Column. Expected type is > >> DateTime. > >> > > >> > > >> >How would I be able to convert the dbnull to a string? > >> > > >> >"Val Mazur (MVP)" wrote: > >> > > >> >> Hi, > >> >> > >> >> DbNull is a special value and not an empty string. In a > >> case of DateTime > >> >> datatype you cannot save empty string into this field, > >> because it is not a > >> >> character data type. You have to store Null or valid > >> date. Why do you need > >> >> to store an empty string? It is actually a great > >> advantage to use Nulls > >> >> instead of empty strings, because you know for sure > >> that field does not hold > >> >> a value in this case > >> >> -- > >> >> Val Mazur > >> >> Microsoft MVP > >> >> > >> >> http://xport.mvps.org > >> >> > >> >> > >> >> > >> >> "40forty" <40fo***@discussions.microsoft.com> wrote in > >> message > >> >> news:DC3D8AB8-946E-4FEB-AFEE- > >> A0138CEF9***@microsoft.com... > >> >> > Hello, > >> >> > > >> >> > I am trying to insert a new datarow. Everything > >> works fine except for > >> >> > three > >> >> > columns that have datetime data types. I keep > >> getting this error message > >> >> > "System.Data.DataColumn.set_Item(Int32 record, Object > >> value)Couldn't store > >> >> > <> > >> >> > in 'columnname' Column. Expected type is DateTime." > >> >> > > >> >> > Is there any way of converting a dbnull to string > >> using vb.net? > >> >> > >> >> > >> >> > >> >. > >> > > >> > > >
Other interesting topics
disconnected typed dataset
DataAdapter Update Does Nothing ADO error "There is already an open DataReader associated with this Connection" How to look at parameter string SQLHelper.ExecuteReader - Connection Close Oracle Data Providers (Connection Pooling & Transactions) Importing Excel data to Access Filling multiple tables Simple asynchronous method Deleting from a dataview |
|||||||||||||||||||||||