p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 2.0 Basics (http://p2p.wrox.com/forumdisplay.php?f=136)
-   -   Conversion from type 'DBNull' to type 'Boolean' is not valid. (http://p2p.wrox.com/showthread.php?t=76749)

alcsew October 24th, 2009 11:31 AM

Conversion from type 'DBNull' to type 'Boolean' is not valid.
 
Hi,

This is my second time trying to post this to this site so I appologize if its a repeat as the the first one didn't appear to go through.

I'm new to asp.net and am getting the error posted below. I have a formview and it contains fields OT and PT, which are set in the DB as allowing nulls. From what I've found googling this error, I will need codebehind but do not know what to write or what event to post in under.

Any help would be greatly apprecited.

Thanks,
Leesha
__________________________________________________ ____________

Conversion from type 'DBNull' to type 'Boolean' is not valid.

Source Error:


Line 402: <asp:Label ID="GradeLabel" runat="server" Text='<%# Bind("Grade") %>'></asp:Label><br />
Line 403: OT:
Line 404: <asp:CheckBox ID="OTCheckBox" runat="server" Checked='<%# Bind("OT") %>' Enabled="false" /><br />
Line 405: PT:
Line 406: <asp:CheckBox ID="PTCheckBox" runat="server" Checked='<%# Bind("PT") %>' Enabled="false" /><br />

alliancejhall October 24th, 2009 09:42 PM

It's due to null...
 
If you have a field in your db that has no value (null) you code doesn't know what to do with it. Since the check setting requires a boolean (True False) it can't interpret a null. One way around this is to set a default when adding a row to the database. If there is no value chosen OT set it to True and same for PT...

For the rows you already have in the DB you will have to handle them so your code won't bomb out. Assuming you are using MSSQL you can use an isnull statement. Basically it looks like this:

Code:

Select Grade, isnull(OT, 'True') as OT, isnull(PT,'True') as PT from myTable
I hope this helps! Also please try not to double post. We will see it the first time you post it. 2 of the same thing just boggs things down.


All times are GMT -4. The time now is 05:40 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.