how to cast DBNull to int?

J

js

Hi, I used a dropdownlist in an ASP.
I try to assign a db field to the SelectedValue. but got this error:
Exception Details: System.InvalidCastException: Object cannot be cast from
DBNull to other types.

<asp:DropDownList id=edit_Province runat="server" SelectedValue='<%#
Convert.ToInt32(DataBinder.Eval(Container.DataItem, "CityID")) %>'>

How to fix then? Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

js,

You can't, really. DBNull indicates that the value is a null value in
the DB, which indicates the absence of a value. You might want to check
that the query is returning valid data. If it is, you need to find a value
to use when the item in the list is null.

Basically, you can't convert a null to a value. If anything, you will
have to put a switch statement around the Eval if the value is null, and
replace it with the value to convert to.

Hope this helps.
 
J

js

Thanks Nicholas.
There is no any build in functioni like (nulltoempty, nulltozero) ?

how to use switch, like this:
<asp:DropDownList id=edit_Province runat="server" SelectedValue=' <%#
DataBinder.Eval(Container.DataItem, "CityID")==null?0:
DataBinder.Eval(Container.DataItem, "CityID")%>'>



Nicholas Paldino said:
js,

You can't, really. DBNull indicates that the value is a null value in
the DB, which indicates the absence of a value. You might want to check
that the query is returning valid data. If it is, you need to find a value
to use when the item in the list is null.

Basically, you can't convert a null to a value. If anything, you will
have to put a switch statement around the Eval if the value is null, and
replace it with the value to convert to.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

js said:
Hi, I used a dropdownlist in an ASP.
I try to assign a db field to the SelectedValue. but got this error:
Exception Details: System.InvalidCastException: Object cannot be cast from
DBNull to other types.

<asp:DropDownList id=edit_Province runat="server" SelectedValue='<%#
Convert.ToInt32(DataBinder.Eval(Container.DataItem, "CityID")) %>'>

How to fix then? Thanks.
 
N

Nicholas Paldino [.NET/C# MVP]

js,

The switch is a little lengthy, but that should be correct. You could
write your own function that would take the DataBinder, as well as the field
name and table name, and it should reduce the code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


js said:
Thanks Nicholas.
There is no any build in functioni like (nulltoempty, nulltozero) ?

how to use switch, like this:
<asp:DropDownList id=edit_Province runat="server" SelectedValue=' <%#
DataBinder.Eval(Container.DataItem, "CityID")==null?0:
DataBinder.Eval(Container.DataItem, "CityID")%>'>



message news:[email protected]...
js,

You can't, really. DBNull indicates that the value is a null value in
the DB, which indicates the absence of a value. You might want to check
that the query is returning valid data. If it is, you need to find a value
to use when the item in the list is null.

Basically, you can't convert a null to a value. If anything, you will
have to put a switch statement around the Eval if the value is null, and
replace it with the value to convert to.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

js said:
Hi, I used a dropdownlist in an ASP.
I try to assign a db field to the SelectedValue. but got this error:
Exception Details: System.InvalidCastException: Object cannot be cast from
DBNull to other types.

<asp:DropDownList id=edit_Province runat="server" SelectedValue='<%#
Convert.ToInt32(DataBinder.Eval(Container.DataItem, "CityID")) %>'>

How to fix then? Thanks.
 
J

js

Thanks for the help.
Still have two questions:
1. the statement got an error, do you know why?
Exception Details: System.ArgumentOutOfRangeException: Specified argument
was out of the range of valid values.

protected int GetCityValue(int ID); //? hwo to accept the ID value but can
possible be null?












Nicholas Paldino said:
js,

The switch is a little lengthy, but that should be correct. You could
write your own function that would take the DataBinder, as well as the field
name and table name, and it should reduce the code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


js said:
Thanks Nicholas.
There is no any build in functioni like (nulltoempty, nulltozero) ?

how to use switch, like this:
<asp:DropDownList id=edit_Province runat="server" SelectedValue=' <%#
DataBinder.Eval(Container.DataItem, "CityID")==null?0:
DataBinder.Eval(Container.DataItem, "CityID")%>'>



message news:[email protected]...
value
in
the DB, which indicates the absence of a value. You might want to check
that the query is returning valid data. If it is, you need to find a value
to use when the item in the list is null.

Basically, you can't convert a null to a value. If anything, you will
have to put a switch statement around the Eval if the value is null, and
replace it with the value to convert to.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi, I used a dropdownlist in an ASP.
I try to assign a db field to the SelectedValue. but got this error:
Exception Details: System.InvalidCastException: Object cannot be
cast
from
DBNull to other types.

<asp:DropDownList id=edit_Province runat="server" SelectedValue='<%#
Convert.ToInt32(DataBinder.Eval(Container.DataItem, "CityID")) %>'>

How to fix then? Thanks.
 
A

Adam W Root

you need to check for a typeof(System.DBNull) like:

<%# myval.GetType() == typeof(System.DBNull) ? 0 : myval %>

a C# null value is different than a DBNull value.

js said:
Thanks for the help.
Still have two questions:
1. the statement got an error, do you know why?
Exception Details: System.ArgumentOutOfRangeException: Specified argument
was out of the range of valid values.

protected int GetCityValue(int ID); //? hwo to accept the ID value but can
possible be null?












message news:[email protected]...
js,

The switch is a little lengthy, but that should be correct. You could
write your own function that would take the DataBinder, as well as the field
name and table name, and it should reduce the code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


js said:
Thanks Nicholas.
There is no any build in functioni like (nulltoempty, nulltozero) ?

how to use switch, like this:
<asp:DropDownList id=edit_Province runat="server" SelectedValue=' <%#
DataBinder.Eval(Container.DataItem, "CityID")==null?0:
DataBinder.Eval(Container.DataItem, "CityID")%>'>



"Nicholas Paldino [.NET/C# MVP]" <[email protected]>
wrote
in
message js,

You can't, really. DBNull indicates that the value is a null
value
in
the DB, which indicates the absence of a value. You might want to check
that the query is returning valid data. If it is, you need to find a
value
to use when the item in the list is null.

Basically, you can't convert a null to a value. If anything,
you
will
have to put a switch statement around the Eval if the value is null, and
replace it with the value to convert to.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi, I used a dropdownlist in an ASP.
I try to assign a db field to the SelectedValue. but got this error:
Exception Details: System.InvalidCastException: Object cannot be cast
from
DBNull to other types.

<asp:DropDownList id=edit_Province runat="server" SelectedValue='<%#
Convert.ToInt32(DataBinder.Eval(Container.DataItem, "CityID")) %>'>

How to fix then? Thanks.
 
J

Jon Skeet [C# MVP]

Adam W Root said:
you need to check for a typeof(System.DBNull) like:

<%# myval.GetType() == typeof(System.DBNull) ? 0 : myval %>

That's a pretty ugly way of checking, IMO. I'd use

myval == DBNull.Value myself.
a C# null value is different than a DBNull value.

Yup.
 
B

Brad Williams

Anything wrong with this?

<%# myval == System.DBNull.Value ? 0 : myval %>


Adam W Root said:
you need to check for a typeof(System.DBNull) like:

<%# myval.GetType() == typeof(System.DBNull) ? 0 : myval %>

a C# null value is different than a DBNull value.

js said:
Thanks for the help.
Still have two questions:
1. the statement got an error, do you know why?
Exception Details: System.ArgumentOutOfRangeException: Specified argument
was out of the range of valid values.
<asp:DropDownList id=edit_Province runat="server" SelectedValue=' <%#
(DataBinder.Eval(Container.DataItem, "CityID")==null)?0:
DataBinder.Eval(Container.DataItem, "CityID")%>'>
2. I will try to write my own function, but how to handle null value?
<asp:DropDownList id=edit_Province runat="server" SelectedValue=' <%#
GetCityValue(DataBinder.Eval(Container.DataItem, "CityID"))%>'>

protected int GetCityValue(int ID); //? hwo to accept the ID value but can
possible be null?












message news:[email protected]...
js,

The switch is a little lengthy, but that should be correct. You could
write your own function that would take the DataBinder, as well as the field
name and table name, and it should reduce the code.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)


Thanks Nicholas.
There is no any build in functioni like (nulltoempty, nulltozero) ?

how to use switch, like this:
<asp:DropDownList id=edit_Province runat="server" SelectedValue=' <%#
DataBinder.Eval(Container.DataItem, "CityID")==null?0:
DataBinder.Eval(Container.DataItem, "CityID")%>'>



in
message js,

You can't, really. DBNull indicates that the value is a null value
in
the DB, which indicates the absence of a value. You might want to check
that the query is returning valid data. If it is, you need to
find
a
value
to use when the item in the list is null.

Basically, you can't convert a null to a value. If anything, you
will
have to put a switch statement around the Eval if the value is
null,
and
replace it with the value to convert to.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Hi, I used a dropdownlist in an ASP.
I try to assign a db field to the SelectedValue. but got this error:
Exception Details: System.InvalidCastException: Object cannot be cast
from
DBNull to other types.

<asp:DropDownList id=edit_Province runat="server" SelectedValue='<%#
Convert.ToInt32(DataBinder.Eval(Container.DataItem, "CityID")) %>'>

How to fix then? Thanks.
 
A

Adam W Root

The beauty of .NET is everyone can have their own style of programming ;-)

I will admit your way probably has it's performance advantages.

Adam
 
J

js

how to get "myval"?

Can I do this:
<%# myval=DataBinder.Eval(Container.DataItem, "CityID"); myval.GetType() ==
typeof(System.DBNull) ? 0 : myval %>
 
G

Guest

and readability. I mean intuitively, one might not understand right away why testing for DBNull through type equality. afterall, that's not how you'd test an integer

----- Adam W Root wrote: ----

The beauty of .NET is everyone can have their own style of programming ;-

I will admit your way probably has it's performance advantages

Ada
 
J

Jon Skeet [C# MVP]

Daniel Jin said:
and readability. I mean intuitively, one might not understand right
away why testing for DBNull through type equality. afterall, that's
not how you'd test an integer.

There's another way I thought of after posting, which is arguably more
readable again (and actually is more similar to the previous way):

"myVal is DBNull"

At that stage I'd say it's a matter of taste.
 
M

Mike Swaim

js said:
Hi, I used a dropdownlist in an ASP.
I try to assign a db field to the SelectedValue. but got this error:
Exception Details: System.InvalidCastException: Object cannot be cast
from DBNull to other types.

<asp:DropDownList id=edit_Province runat="server" SelectedValue='<%#
Convert.ToInt32(DataBinder.Eval(Container.DataItem, "CityID")) %>'>

How to fix then? Thanks.

Is this data coming from a database? If so, why not fix your source query
to return 0 or -1 instead of null?
 
G

Guest

What about the followin

if (!reader.IsDBNull(reader.GetOrdinal("MyVar")

mvar = reader.GetValue(reader.GetOrdinal("MyVar"))


Another thing to consider, if you are using Sql, might be to use the types declared in System.Data.SqlTypes ??? They should automatically do for you what you're trying to achieve manually

Hope that helps..

Martin.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top