Checkbox = null, Specified cast is not valid.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

<edititemtemplate>
<asp:CheckBox ID="giChkBox_bln_removed" runat="server" Checked='<%#
Bind("bln_removed")%>'></asp:CheckBox>
</edititemtemplate>

The above line gives an error (Specified cast is not valid.) if the field
bln_removed is null. Without changing the SQL select statement how can we fix
this?
 
<edititemtemplate>
<asp:CheckBox ID="giChkBox_bln_removed" runat="server" Checked='<%#
Bind("bln_removed")%>'></asp:CheckBox>
</edititemtemplate>

The above line gives an error (Specified cast is not valid.) if the field
bln_removed is null. Without changing the SQL select statement how can we fix
this?

In C#:

<%# Bind("bln_removed")==DBNull.Value ? "" : Bind("bln_removed") %>

In VB.NET:

<%# IIf(IsDBNull(Bind("bln_removed")), "", Bind("bln_removed")) %>
 
Thanks for the reply.

I tried the C# code that you have given. It is still giving the same error.
 
Thanks for the reply.

I tried the C# code that you have given. It is still giving the same error.

Ah, I think it's not because of null, but because of "" in my code

<%# Bind("bln_removed")==DBNull.Value ? false : Bind("bln_removed") %>
 
Alexey,

I'm still getting the same error message.
However, I tried the following code. It works. But the data is not updated
when I save the GridView control.

Checked='<%#
(DataBinder.Eval(Container.DataItem,"bln_core_asset").ToString().TrimStart().TrimEnd()=="True"?true:false) %>'

How can we make the data saved back to the database.
 
Alexey,
I removed the excess spaces in between the characters. And it works now!!!

Checked=<%#
Bind("bln_core_asset")==DBNull.Value?false:Bind("bln_core_asset") %>

Thank you very very much for the help.
 
Alexey,
I removed the excess spaces in between the characters. And it works now!!!

Checked=<%#
Bind("bln_core_asset")==DBNull.Value?false:Bind("bln_core_asset") %>

Thank you very very much for the help.
--
test









- Show quoted text -

Glad, that it works. The Checked property's value must be boolean,
meaning it expects a value of either true or false and not spaces, or
other extra characters...
 
Alexey,

The error started appearing again. To my surprise, it was working before.

<edititemtemplate>
<asp:CheckBox ID="geChkBox_bln_core_asset" runat="server"
Checked=<%#Bind("bln_core_asset")==DBNull.Value?false:Bind("bln_core_asset")
%>></asp:CheckBox>
</edititemtemplate>
 
Alexey,

The error started appearing again. To my surprise, it was working before.

<edititemtemplate>
<asp:CheckBox ID="geChkBox_bln_core_asset" runat="server"
Checked=<%#Bind("bln_core_asset")==DBNull.Value?false:Bind("bln_core_asset"­)
%>></asp:CheckBox>
</edititemtemplate>

--
test






- Show quoted text -

Are you sure that it occurs exactly here? The code looks good.

Try to delete

Checked=<%#Bind("bln_core_asset")==DBNull.Value?
false:Bind("bln_core_asset"­)

to see if it helps
 
Yes, it appears at the same place.
--
test


Alexey Smirnov said:
Are you sure that it occurs exactly here? The code looks good.

Try to delete

Checked=<%#Bind("bln_core_asset")==DBNull.Value?
false:Bind("bln_core_asset"-)

to see if it helps
 
Yes, it appears at the same place.
--
test







- Show quoted text -

wait... it's wrong, sure...

Checked=<%# ( Bind("bln_core_asset")==DBNull.Value ? false :
Bind("bln_core_asset"­) )%>

it will not work without ( )
 
It, (), is giving compilation errors
--
test


Alexey Smirnov said:
wait... it's wrong, sure...

Checked=<%# ( Bind("bln_core_asset")==DBNull.Value ? false :
Bind("bln_core_asset"-) )%>

it will not work without ( )
 
It, (), is giving compilation errors
--
test








- Show quoted text -

Look at the earlier posts, checked was with a value with ' ' around

Checked='<%# ( Bind("bln_core_asset")==DBNull.Value ? false :
Bind("bln_core_asset"­) )%>'
 
Hi rkbnair,

As for the problem here, it seems the "Bind(..)" expression is the cause.
"Bind" is a bi-direction databinding keyword and which only support the
following usage

<%# Bind("columnname or propertyname") %>

You can not use it as an normal expression(just like what you can use on
"Eval" keyword).

Therefore, for the case here, I suggest you consider the following means:

1. provide a default value(false) for your datatable so as to avoid letting
the ASP.NET databinding to handle DBNull Value

2. Manually use code to do update(rather than use "Bind" keyword), you can
still use "Eval" to display the value.

How do you think?

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hello Steven,

I think I should handle the null value in the query itself.

I used isnull(bln_core_asset,0),?,?,etc. in the query statement and it works
fine.

Thanks for the help.
 
Back
Top