Checkbox = null, Specified cast is not valid.

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?
 
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?

In C#:

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

In VB.NET:

<%# IIf(IsDBNull(Bind("bln_removed")), "", Bind("bln_removed")) %>
 
G

Guest

Thanks for the reply.

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

Guest

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") %>
 
G

Guest

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.
 
G

Guest

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.
 
G

Guest

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...
 
G

Guest

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>
 
G

Guest

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
 
G

Guest

Yes, it appears at the same place.
--
test


Anon User 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
 
G

Guest

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 ( )
 
G

Guest

It, (), is giving compilation errors
--
test


Anon User said:
wait... it's wrong, sure...

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

it will not work without ( )
 
G

Guest

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"­) )%>'
 
S

Steven Cheng[MSFT]

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.
 
G

Guest

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.
 

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

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top