Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 22nd, 2011, 10:43 AM
Authorized User
Points: 285, Level: 5
Points: 285, Level: 5 Points: 285, Level: 5 Points: 285, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default Ch 13 - Conversion from type 'DBNull' to type 'Boolean' is not valid.

Working through Chapter 13 Customizing Grid view Columns and I have added the boolean to the markup and code behind and I get this error: Any thoughts why?


Code:
Server Error in '/WebSite1' Application.
--------------------------------------------------------------------------------

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 3:      Inherits System.Web.UI.Page
Line 4:      Protected Function GetBooleanText(ByVal booleanValue As Object) As String
Line 5:          Dim authorized As Boolean = CType(booleanValue, Boolean)
Line 6:          If authorized Then
Line 7:              Return "Yes"
 

Source File: C:\BegASPNET\WebSite1\Management\Default2.aspx.vb    Line: 5 

Stack Trace: 


[InvalidCastException: Conversion from type 'DBNull' to type 'Boolean' is not valid.]
   Microsoft.VisualBasic.CompilerServices.Conversions.ToBoolean(Object Value) +1286290
   Management_Default2.GetBooleanText(Object booleanValue) in C:\BegASPNET\WebSite1\Management\Default2.aspx.vb:5
   ASP.management_default2_aspx.__DataBinding__control8(Object sender, EventArgs e) in C:\BegASPNET\WebSite1\Management\Default2.aspx:28
   System.Web.UI.Control.OnDataBinding(EventArgs e) +91
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +92
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.Control.DataBindChildren() +201
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +101
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.Control.DataBindChildren() +201
   System.Web.UI.Control.DataBind(Boolean raiseOnDataBinding) +101
   System.Web.UI.Control.DataBind() +15
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +166
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +3896
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +66
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +14
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +128
   System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback) +33
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +143
   System.Web.UI.WebControls.BaseDataBoundControl.DataBind() +74
   System.Web.UI.WebControls.GridView.DataBind() +4
   System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound() +66
   System.Web.UI.WebControls.GridView.OnPreRender(EventArgs e) +26
   System.Web.UI.Control.PreRenderRecursiveInternal() +103
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Control.PreRenderRecursiveInternal() +175
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2496
Reply With Quote
  #2 (permalink)  
Old November 22nd, 2011, 03:29 PM
Imar's Avatar
Wrox Author
Points: 72,055, Level: 100
Points: 72,055, Level: 100 Points: 72,055, Level: 100 Points: 72,055, Level: 100
Activity: 100%
Activity: 100% Activity: 100% Activity: 100%
 
Join Date: Jun 2003
Location: Utrecht, Netherlands.
Posts: 17,086
Thanks: 80
Thanked 1,587 Times in 1,563 Posts
Default

Hi there,

The error message suggests that the value in the database is <null> rather than a boolean with a value of 1 or 0. You can manually update the database so the column contains only valid values (and then make it required to avoid null values there again) or check for the constant DBNull.Value in your GetBooleanText method and return a default value (true or false, up to you), when the booleanValue equals DBNull.Value.

Hope this helps,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
Reply With Quote
The Following User Says Thank You to Imar For This Useful Post:
leeWozyWarren (November 22nd, 2011)
  #3 (permalink)  
Old November 22nd, 2011, 03:36 PM
Authorized User
Points: 285, Level: 5
Points: 285, Level: 5 Points: 285, Level: 5 Points: 285, Level: 5
Activity: 0%
Activity: 0% Activity: 0% Activity: 0%
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Thanks Imar :)
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Conversion from type 'DBNull' to type 'Boolean' is not valid. alcsew ASP.NET 2.0 Basics 4 October 28th, 2009 03:38 AM
Conversion from type 'DBNull' to type 'Boolean' is not valid. alcsew ASP.NET 2.0 Basics 1 October 24th, 2009 09:42 PM
Conversion from type 'DBNull' to type 'Boolean' is not valid. alcsew ASP.NET 2.0 Basics 0 October 24th, 2009 11:25 AM
Conversion from type 'DBNull' to type 'String' is not valid. phuc2583 ASP.NET 3.5 Professionals 4 October 1st, 2009 09:46 AM
Conversion from type 'DBNull' to type 'Boolean' is steve35719 VB Databases Basics 4 June 29th, 2006 06:13 PM



All times are GMT -4. The time now is 04:49 PM.


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