Handling Null values in a FormView

I have a binary field represented by a check box in a FormView control.  This field is NULL in a number of records.  I’m getting an error:

Conversion from type ‘DBNull’ to type ‘Boolean’ is not valid.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Conversion from type ‘DBNull’ to type ‘Boolean’ is not valid.

Source Error:

Line 874:                        <asp:TableCell>Renew Ad: </asp:TableCell>
Line 875:                        <asp:TableCell>
Line 876:                            <asp:CheckBox ID="ReNewAdCheckBox" runat="server" Checked=’<%# Bind("ReNewAd") %>’
Line 877:                            Enabled="false" /><br />
Line 878:                        </asp:TableCell>

Is there a way to handle this?

Diane 

You can try this:

C#:

<asp:CheckBox
ID="ReNewAdCheckBox"
runat="server"
Checked=’<%# (Eval("checked")==DBNull.Value ? false : Eval("checked"))%>
/>

VB.NET:

<asp:CheckBox
ID="ReNewAdCheckBox"
runat="server"
Checked=’<%# IIF (CONVERT.ToString(Eval("checked"))="",false, Eval("checked"))%>
/>

Thank you.  The  checkbox code is

<asp:CheckBox ID="ReNewAdCheckBox" runat="server" Checked=’<%# Bind("ReNewAd") %>’ /> </asp:TableCell>

How do I change that?  It the below close?

<asp:CheckBox
ID="ReNewAdCheckBox"
runat="server"
Checked=’<%# IIF
<%# Bind("ReNewAd") %>="",false, Eval("checked"))%>
/>

 

Diane 

Assume the ReNewAd is a bit field in your database:(I tested in SQL Server) 

VB.NET:

 

<asp:CheckBox
ID="ReNewAdCheckBox"
runat
="server" Checked=’<%# IIF (CONVERT.ToString(Eval("ReNewAd"))="",false, Eval("ReNewAd"))%>
/>

That’s a great article Allen, thank you.  It doesn’t help me here, because I’m using a form view, which doesn’t give me the same options.  But I’ve bookmarked the article.  There’s a lot of great stuff in there.

Limno, I’m using a bound check box:

<asp:CheckBox ID="ReNewAdCheckBox" runat="server" Checked=’<%# Bind("ReNewAd") %>’ />

Is this what the code should look like?

<asp:CheckBox ID="ReNewAdCheckBox" runat="server" Checked=’<%# IIF (CONVERT.ToString(Bind("ReNewAd") )="",false, Eval("checked"))%>’ /> 

Diane 

I tried the line I last posted, it didn’t work.  I tried putting it into a functions, that didn’t work.  In both cases i got a Bind not declared error.  Apparently the double binding is what’s making this so difficult.  I’ve done a Google search, but I can
either find how to handle nulls in a DetailsView control, or how to use FormsView with no mention of checkbox nulls.  I’m stumped!

Diane 

This is the code:

<asp:CheckBox
ID="ReNewAdCheckBox"
runat
="server" Checked=’<%# IIF (CONVERT.ToString(Eval("ReNewAd"))="",false,
Eval("ReNewAd"))%>

/>

This line tells you that if the value returned is DBNull, then use false as the value here, otherwise get the value from your table (either true or false)

FYI:  Assume this ReNewAd is a bit column in your table(Sql Server), you can make it a required field which would save you a lot of time later on.

Does the above code work?  I’m trying to do the same thing as you’ve done above.  I’m able to display the value in a checkbox, but when it comes to binding the value & updating the database, that doesn’t work.

No, it doesn’t work if you need to Bind to the database.

I got mine to work like this:

<asp:CheckBox ID="ReNewAdCheckBox" runat="server" Checked='<%# Convert.ToString(Eval("ReNewAd")) == "1") ? true : false %>'

Leave a Reply