all groups > vb.net > march 2006 >
vb.net :
adding items to a listbox that are null problem
I'm trying to add an object to a listbox. I'm using a DataReader from a resultset from a stored procedure. It works except when there are NULL in any of the rows. Then I get an error: An unhandled exception of type 'System.InvalidCastException' occurred in microsoft.visualbasic.dll Additional information: Cast from type 'DBNull' to type 'String' is not valid. Me.lstMenu.Items.Add(New Menu(dr.Item("FirstTier"), dr.Item("SecondTier"))) How do I handle nulls? FirstTier is a string, SecondTier is a Long. If I remove all nulls in the table, this works, but I need to keep the nulls, so I need a solution.. Thanks. Any help is appreciated.
Hi, you can handle them in you select statement or you can build in a check in .net something like if not dr.Item("FirstTier") Is DBNull.Value then ... Hth Greetz, Peter -- Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. (Rich Cook) "mrmagoo" <-> schreef in bericht news:OQBlYujUGHA.5900@tk2msftngp13.phx.gbl... [quoted text, click to view] > I'm trying to add an object to a listbox. I'm using a DataReader from a > resultset from a stored procedure. > > It works except when there are NULL in any of the rows. Then I get an error: > > An unhandled exception of type 'System.InvalidCastException' occurred in > microsoft.visualbasic.dll > Additional information: Cast from type 'DBNull' to type 'String' is not > valid. > > Me.lstMenu.Items.Add(New Menu(dr.Item("FirstTier"), dr.Item("SecondTier"))) > > How do I handle nulls? FirstTier is a string, SecondTier is a Long. > > If I remove all nulls in the table, this works, but I need to keep the > nulls, so I need a solution.. > > Thanks. Any help is appreciated. > > >
You could build the check in the constructor of your Menu class, it's just what you prefer Greetz, Peter -- Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. (Rich Cook) "mrmagoo" <-> schreef in bericht news:#ZIgSMkUGHA.776@TK2MSFTNGP09.phx.gbl... [quoted text, click to view] > thanks...how would I optimally do that with the Add approach I > posted...adding an object to the listbox. > > thanks > > "Peter Proost" <pproost@nospam.hotmail.com> wrote in message > news:OBJc2$jUGHA.6084@TK2MSFTNGP14.phx.gbl... > > Hi, you can handle them in you select statement or you can build in a > check > > in .net something like > > > > if not dr.Item("FirstTier") Is DBNull.Value then ... > > > > Hth > > > > Greetz, Peter > > > > -- > > Programming today is a race between software engineers striving to build > > bigger and better idiot-proof programs, and the Universe trying to produce > > bigger and better idiots. So far, the Universe is winning. (Rich Cook) > > > > "mrmagoo" <-> schreef in bericht > > news:OQBlYujUGHA.5900@tk2msftngp13.phx.gbl... > > > I'm trying to add an object to a listbox. I'm using a DataReader from a > > > resultset from a stored procedure. > > > > > > It works except when there are NULL in any of the rows. Then I get an > > error: > > > > > > An unhandled exception of type 'System.InvalidCastException' occurred in > > > microsoft.visualbasic.dll > > > Additional information: Cast from type 'DBNull' to type 'String' is not > > > valid. > > > > > > Me.lstMenu.Items.Add(New Menu(dr.Item("FirstTier"), > > dr.Item("SecondTier"))) > > > > > > How do I handle nulls? FirstTier is a string, SecondTier is a Long. > > > > > > If I remove all nulls in the table, this works, but I need to keep the > > > nulls, so I need a solution.. > > > > > > Thanks. Any help is appreciated. > > > > > > > > > > > > > > >
thanks...how would I optimally do that with the Add approach I posted...adding an object to the listbox. thanks [quoted text, click to view] "Peter Proost" <pproost@nospam.hotmail.com> wrote in message news:OBJc2$jUGHA.6084@TK2MSFTNGP14.phx.gbl... > Hi, you can handle them in you select statement or you can build in a check > in .net something like > > if not dr.Item("FirstTier") Is DBNull.Value then ... > > Hth > > Greetz, Peter > > -- > Programming today is a race between software engineers striving to build > bigger and better idiot-proof programs, and the Universe trying to produce > bigger and better idiots. So far, the Universe is winning. (Rich Cook) > > "mrmagoo" <-> schreef in bericht > news:OQBlYujUGHA.5900@tk2msftngp13.phx.gbl... > > I'm trying to add an object to a listbox. I'm using a DataReader from a > > resultset from a stored procedure. > > > > It works except when there are NULL in any of the rows. Then I get an > error: > > > > An unhandled exception of type 'System.InvalidCastException' occurred in > > microsoft.visualbasic.dll > > Additional information: Cast from type 'DBNull' to type 'String' is not > > valid. > > > > Me.lstMenu.Items.Add(New Menu(dr.Item("FirstTier"), > dr.Item("SecondTier"))) > > > > How do I handle nulls? FirstTier is a string, SecondTier is a Long. > > > > If I remove all nulls in the table, this works, but I need to keep the > > nulls, so I need a solution.. > > > > Thanks. Any help is appreciated. > > > > > > > >
Ok...but how do I do it? Sub New(ByVal FirstTier As String, _ ByVal SecondTier As Long) If there's a null, it errors out even before the constructor begins. There's a NULL being passed as a parameter value to the constructor, which is expecting a string. There's where the error is occurring. How do I implement your suggestion? thanks [quoted text, click to view] "Peter Proost" <pproost@nospam.hotmail.com> wrote in message news:%23lD2JxkUGHA.1160@TK2MSFTNGP09.phx.gbl... > You could build the check in the constructor of your Menu class, it's just > what you prefer > > Greetz, > > Peter > > -- > Programming today is a race between software engineers striving to build > bigger and better idiot-proof programs, and the Universe trying to produce > bigger and better idiots. So far, the Universe is winning. (Rich Cook) > > "mrmagoo" <-> schreef in bericht > news:#ZIgSMkUGHA.776@TK2MSFTNGP09.phx.gbl... > > thanks...how would I optimally do that with the Add approach I > > posted...adding an object to the listbox. > > > > thanks > > > > "Peter Proost" <pproost@nospam.hotmail.com> wrote in message > > news:OBJc2$jUGHA.6084@TK2MSFTNGP14.phx.gbl... > > > Hi, you can handle them in you select statement or you can build in a > > check > > > in .net something like > > > > > > if not dr.Item("FirstTier") Is DBNull.Value then ... > > > > > > Hth > > > > > > Greetz, Peter > > > > > > -- > > > Programming today is a race between software engineers striving to build > > > bigger and better idiot-proof programs, and the Universe trying to > produce > > > bigger and better idiots. So far, the Universe is winning. (Rich Cook) > > > > > > "mrmagoo" <-> schreef in bericht > > > news:OQBlYujUGHA.5900@tk2msftngp13.phx.gbl... > > > > I'm trying to add an object to a listbox. I'm using a DataReader from > a > > > > resultset from a stored procedure. > > > > > > > > It works except when there are NULL in any of the rows. Then I get an > > > error: > > > > > > > > An unhandled exception of type 'System.InvalidCastException' occurred > in > > > > microsoft.visualbasic.dll > > > > Additional information: Cast from type 'DBNull' to type 'String' is > not > > > > valid. > > > > > > > > Me.lstMenu.Items.Add(New Menu(dr.Item("FirstTier"), > > > dr.Item("SecondTier"))) > > > > > > > > How do I handle nulls? FirstTier is a string, SecondTier is a Long. > > > > > > > > If I remove all nulls in the table, this works, but I need to keep the > > > > nulls, so I need a solution.. > > > > > > > > Thanks. Any help is appreciated. > > > > > > > > > > > > > > > > > > > > > > > >
Thanks for the help everybody. I'm gonna try these different approaches out. Much appreciated! [quoted text, click to view] "Renze de Waal" <renze@dewaal.speedlinq.nl> wrote in message news:e0c837$7t0$1@emma.aioe.org... > You could create a function that takes a parameter with type object and > returns a string. Something like > > private function ReplaceNull(byval objValue as Object) as string > If objValue is DBNull.Value Then > ReplaceNull = "" > Else > ReplaceNull = CType(objValue,String) > End If > end function > > Then change your call to > > Me.lstMenu.Items.Add(New Menu(ReplaceNull(dr.Item("FirstTier")), > ReplaceNull(dr.Item("SecondTier")))) > > Best regards, > > Renze de Waal. > > In article <#ZIgSMkUGHA.776@TK2MSFTNGP09.phx.gbl>, "mrmagoo" <-> wrote: > >thanks...how would I optimally do that with the Add approach I > >posted...adding an object to the listbox. > > > >thanks > > > >"Peter Proost" <pproost@nospam.hotmail.com> wrote in message > >news:OBJc2$jUGHA.6084@TK2MSFTNGP14.phx.gbl... > >> Hi, you can handle them in you select statement or you can build in a > >check > >> in .net something like > >> > >> if not dr.Item("FirstTier") Is DBNull.Value then ... > >> > >> Hth > >> > >> Greetz, Peter > >> > >> -- > >> Programming today is a race between software engineers striving to build > >> bigger and better idiot-proof programs, and the Universe trying to produce > >> bigger and better idiots. So far, the Universe is winning. (Rich Cook) > >> > >> "mrmagoo" <-> schreef in bericht > >> news:OQBlYujUGHA.5900@tk2msftngp13.phx.gbl... > >> > I'm trying to add an object to a listbox. I'm using a DataReader from a > >> > resultset from a stored procedure. > >> > > >> > It works except when there are NULL in any of the rows. Then I get an > >> error: > >> > > >> > An unhandled exception of type 'System.InvalidCastException' occurred in > >> > microsoft.visualbasic.dll > >> > Additional information: Cast from type 'DBNull' to type 'String' is not > >> > valid. > >> > > >> > Me.lstMenu.Items.Add(New Menu(dr.Item("FirstTier"), > >> dr.Item("SecondTier"))) > >> > > >> > How do I handle nulls? FirstTier is a string, SecondTier is a Long. > >> > > >> > If I remove all nulls in the table, this works, but I need to keep the > >> > nulls, so I need a solution.. > >> > > >> > Thanks. Any help is appreciated. > >> > > >> > > >> > > >> > >> > > > >
You could create a function that takes a parameter with type object and returns a string. Something like private function ReplaceNull(byval objValue as Object) as string If objValue is DBNull.Value Then ReplaceNull = "" Else ReplaceNull = CType(objValue,String) End If end function Then change your call to Me.lstMenu.Items.Add(New Menu(ReplaceNull(dr.Item("FirstTier")), ReplaceNull(dr.Item("SecondTier")))) Best regards, Renze de Waal. [quoted text, click to view] In article <#ZIgSMkUGHA.776@TK2MSFTNGP09.phx.gbl>, "mrmagoo" <-> wrote: >thanks...how would I optimally do that with the Add approach I >posted...adding an object to the listbox. > >thanks > >"Peter Proost" <pproost@nospam.hotmail.com> wrote in message >news:OBJc2$jUGHA.6084@TK2MSFTNGP14.phx.gbl... >> Hi, you can handle them in you select statement or you can build in a >check >> in .net something like >> >> if not dr.Item("FirstTier") Is DBNull.Value then ... >> >> Hth >> >> Greetz, Peter >> >> -- >> Programming today is a race between software engineers striving to build >> bigger and better idiot-proof programs, and the Universe trying to produce >> bigger and better idiots. So far, the Universe is winning. (Rich Cook) >> >> "mrmagoo" <-> schreef in bericht >> news:OQBlYujUGHA.5900@tk2msftngp13.phx.gbl... >> > I'm trying to add an object to a listbox. I'm using a DataReader from a >> > resultset from a stored procedure. >> > >> > It works except when there are NULL in any of the rows. Then I get an >> error: >> > >> > An unhandled exception of type 'System.InvalidCastException' occurred in >> > microsoft.visualbasic.dll >> > Additional information: Cast from type 'DBNull' to type 'String' is not >> > valid. >> > >> > Me.lstMenu.Items.Add(New Menu(dr.Item("FirstTier"), >> dr.Item("SecondTier"))) >> > >> > How do I handle nulls? FirstTier is a string, SecondTier is a Long. >> > >> > If I remove all nulls in the table, this works, but I need to keep the >> > nulls, so I need a solution.. >> > >> > Thanks. Any help is appreciated. >> > >> > >> > >> >> >
Don't see what you're looking for? Try a search.
|
|
|