Home All Groups Group Topic Archive Search About

adding items to a listbox that are null problem

Author
28 Mar 2006 7:45 AM
mrmagoo
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.

Author
28 Mar 2006 8:17 AM
Peter Proost
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")))
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.
>
>
>
Author
28 Mar 2006 8:39 AM
mrmagoo
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.
> >
> >
> >
>
>
Author
28 Mar 2006 9:45 AM
Peter Proost
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)

Show quoteHide quote
"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.
> > >
> > >
> > >
> >
> >
>
>
Author
28 Mar 2006 3:20 PM
mrmagoo
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.
> > > >
> > > >
> > > >
> > >
> > >
> >
> >
>
>
Author
28 Mar 2006 9:59 PM
Renze de Waal
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.
>> >
>> >
>> >
>>
>>
>
>
Author
28 Mar 2006 9:42 PM
mrmagoo
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.
> >> >
> >> >
> >> >
> >>
> >>
> >
> >