By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,571 Members | 1,659 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,571 IT Pros & Developers. It's quick & easy.

adding items to a listbox that are null problem

P: n/a
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.

Mar 28 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
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:OQ**************@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.

Mar 28 '06 #2

P: n/a
thanks...how would I optimally do that with the Add approach I
posted...adding an object to the listbox.

thanks

"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:OB**************@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:OQ**************@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.


Mar 28 '06 #3

P: n/a
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:#Z*************@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" <pp*****@nospam.hotmail.com> wrote in message
news:OB**************@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:OQ**************@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.



Mar 28 '06 #4

P: n/a
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


"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:%2****************@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:#Z*************@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" <pp*****@nospam.hotmail.com> wrote in message
news:OB**************@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:OQ**************@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.
>
>
>



Mar 28 '06 #5

P: n/a
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 <#Z*************@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" <pp*****@nospam.hotmail.com> wrote in message
news:OB**************@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:OQ**************@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.
>
>
>



Mar 28 '06 #6

P: n/a
Thanks for the help everybody.

I'm gonna try these different approaches out.

Much appreciated!
"Renze de Waal" <re***@dewaal.speedlinq.nl> wrote in message
news:e0**********@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 <#Z*************@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" <pp*****@nospam.hotmail.com> wrote in message
news:OB**************@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:OQ**************@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.
>
>
>


Mar 28 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.