Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > BOOK: Beginning ASP.NET 4 : in C# and VB
|
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 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
 
Old November 22nd, 2011, 10:43 AM
Authorized User
 
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
 
Old November 22nd, 2011, 03:29 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 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!
The Following User Says Thank You to Imar For This Useful Post:
leeWozyWarren (November 22nd, 2011)
 
Old November 22nd, 2011, 03:36 PM
Authorized User
 
Join Date: Oct 2010
Posts: 71
Thanks: 12
Thanked 0 Times in 0 Posts
Default

Thanks Imar :)





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





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