Groups | Blog | Home
all groups > vb.net > march 2006 >

vb.net : adding items to a listbox that are null problem


mrmagoo
3/27/2006 11:45:34 PM
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.


Peter Proost
3/28/2006 12:00:00 AM
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]

Peter Proost
3/28/2006 12:00:00 AM
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]

mrmagoo
3/28/2006 12:39:09 AM
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]

mrmagoo
3/28/2006 7:20:44 AM
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]

mrmagoo
3/28/2006 1:42:13 PM
Thanks for the help everybody.

I'm gonna try these different approaches out.

Much appreciated!


[quoted text, click to view]

renze NO[at]SPAM dewaal.speedlinq.nl
3/28/2006 9:59:17 PM
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]
AddThis Social Bookmark Button