|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
adding items to a listbox that are null problemI'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 dr.Item("SecondTier")))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"), Show quoteHide quote > > 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 Show quoteHide quote "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 build the check in the constructor of your Menu class, it's just
what you prefer Greetz, Peter -- Show quoteHide quoteProgramming 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. > > > > > > > > > > > > > > > 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 Show quoteHide quote "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. > > > > > > > > > > > > > > > > > > > > > > > > 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. Show quoteHide quote In article <#ZIgSMkUGHA.***@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. >> > >> > >> > >> >> > > Thanks for the help everybody.
I'm gonna try these different approaches out. Much appreciated! Show quoteHide quote "Renze de Waal" <re***@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.***@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. > >> > > >> > > >> > > >> > >> > > > >
Derived TextBox
Financial.Rate Cannot calculate rate using the arguments provided which is better Re: Newbie needs help temporarily pausing loop Problem with - Edit Item of Collections.Generic.List(Of SearchAreaListItem) Abstract/Base Class Best Practices - Help Shell to DOS How to read S.M.A.R.T. attributes in vb ? Instance of derived class does not show public methods or properties that are not in the inherited c Help VS 2005 Dead in Water for VB project |
|||||||||||||||||||||||