Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
| Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 24th, 2009, 11:16 AM
Registered User
 
Join Date: Aug 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conversion from type 'DBNull' to type 'Boolean' is not valid.

Hi,
I have a formview that contains OT and PT checkboxes that set to allow nulls in the database. I'm getting the error I've posted below. I've googled the error and "think" I will require codebehind to get past this but for the life of me don't know what to write nor what event it should be put under. I'm used to programming in Access so this is still pretty new to me and frustrating!!! Any help would be appreciated.

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 />
  #2 (permalink)  
Old October 24th, 2009, 06:53 PM
Lee Dumond's Avatar
Wrox Author
Points: 4,942, Level: 29
Points: 4,942, Level: 29 Points: 4,942, Level: 29 Points: 4,942, Level: 29
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jan 2008
Location: Decatur, IL, USA.
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Before you can handle this, you need to decide exactly how you intend to display a null OT or a null PT.

Right now, you are attempting to display a three-valued property with a two-valued control. In other words, there are THREE possible values for OT and PT: true, false, and NULL. However, there are only two states for a checkbox: checked and unchecked.

You would normally show a true value with a checked checkbox; and a false value with an unchecked checkbox. But, how do you intend to display a NULL value for OT or PT?

You could write code to show a NULL value as an unchecked checkbox, for example. But that way, there is now way to distinguish a NULL from a false when the values are displayed. Does that make sense?
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
  #3 (permalink)  
Old October 24th, 2009, 10:15 PM
Registered User
 
Join Date: Aug 2009
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm sorry for posting twice. I was having trouble with my internet and the page seemed to be stuck.

Under Misc. in the properties I have checked = false. Is this what you mean?

Thanks,
Leesha
  #4 (permalink)  
Old October 27th, 2009, 03:07 PM
Friend of Wrox
Points: 4,332, Level: 27
Points: 4,332, Level: 27 Points: 4,332, Level: 27 Points: 4,332, Level: 27
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Nov 2003
Location: , NJ, USA.
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

What he is saying is that a checkbox can only have 2 possible values, TRUE or FALSE. The problem you are having is that you are getting NULL values and a checkbox does not know how to display that. What you have to decide is that when you get a NULL back from your datasource, how do you want to display the checkbox, as checked or not checked?

I think the main problem here is that a boolean/bit data type should not allow NULLs.
  #5 (permalink)  
Old October 28th, 2009, 03:38 AM
Friend of Wrox
Points: 3,558, Level: 25
Points: 3,558, Level: 25 Points: 3,558, Level: 25 Points: 3,558, Level: 25
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Jun 2003
Location: California, USA
Posts: 996
Thanks: 2
Thanked 11 Times in 11 Posts
Send a message via Yahoo to melvik
Default

u may set a default for ur field in Dataset or use IsNull() in DB
__________________
Always,
Hovik Melkomian.


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion from type 'DBNull' to type 'String' is not valid. phuc2583 ASP.NET 3.5 Professionals 4 October 1st, 2009 09:46 AM
The boolean data type in C++? code_lover C++ Programming 2 July 14th, 2008 01:11 AM
Conversion from type 'DBNull' to type 'String' is GailCG ASP.NET 2.0 Basics 5 February 22nd, 2007 03:12 PM
Conversion from type 'DBNull' to type 'Boolean' is steve35719 VB Databases Basics 4 June 29th, 2006 06:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.