Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > VB Databases Basics
Password Reminder
Register
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
VB Databases Basics Beginning-level VB coding questions specific to using VB with databases. Issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the VB Databases Basics 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 June 25th, 2006, 07:48 AM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default Conversion from type 'DBNull' to type 'Boolean' is

I've been trying for a very long time to figure this out but can't seem to do it. I'm just trying to two-way databind a checkbox in a FormView but keep getting the exception: "Conversion from type 'DBNull' to type 'Boolean' is not valid." "When casting from a number, the value must be less than infinity." If you look at my code-behind page, you can see I've tried several things without success. Please, any direction would be very helpful and much appreciated.
Thanks,
Greg

Code:

Partial Class _Default
    Inherits System.Web.UI.Page

    Private RedField As Boolean

    Public Property Red() As Boolean
        Get
            Return Me.RedField
        End Get
        Set(ByVal value As Boolean)
            Me.RedField = False
        End Set
    End Property


    Protected Sub Fav_Initializing(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.Init

        Dim Fav As New FormView
        Try
            If IsDBNull(Fav.DataItem("Red")) Then
                Dim RedCheckBox As Boolean = False

            End If
        Catch ex As Exception

        End Try
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim SqlDataAdapter1 As New SqlDataSource
        Dim Fav As New FormView
        SqlDataAdapter1.DataBind()
        If Not IsPostBack Then
            FormView1.DataBind()
        End If

    End Sub

End Class

Public Class DynamicItemTemplate
    ' ITemplate - When implemented by a class, defines the Control object
    ' to which child controls and templates belong. These child controls
    ' are in turn defined within an inline template.
    Implements ITemplate

    Public Overridable Overloads Sub InstantiateIn(ByVal container As Control) Implements ITemplate.InstantiateIn
        ' InstantiateIn - When implemented by a class, defines the Control
        ' object to which child controls and templates belong. These child
        ' controls are, in turn, defined within an inline template.
        '
        ' Create an instance of a CheckBox object.
        Dim RedCheckBox As CheckBox = New CheckBox()

        ' When the DataBinding event of the CheckBox fires, call the sub
        ' BindCheckBox to properly bind.
        ' AddHandler oCheckBox.DataBinding, AddressOf BindCheckBox
        'Add the CheckBox to the controls collection.
        container.Controls.Add(RedCheckBox)
    End Sub


    Public Sub BindRedCheckBox(ByVal sender As Object, ByVal e As EventArgs)
        'Create a new instance of a CheckBox.
        Dim RedCheckBox As CheckBox = CType(sender, CheckBox)
        Dim container As FormView = CType(RedCheckBox.NamingContainer, FormView)
        'Evaluate the data from the Grid item and set the Checked property
        ' appropriatly
        If container.DataItem("Red").GetType.ToString = "System.DBNull" Then
            RedCheckBox.Checked = False
        Else
            RedCheckBox.Checked = CBool(container.DataItem("Red"))
        End If

    End Sub

    Public Sub BindBlueCheckBox(ByVal sender As Object, ByVal e As EventArgs)
        'Create a new instance of a CheckBox.
        Dim BlueCheckBox As CheckBox = CType(sender, CheckBox)
        Dim container As FormView = CType(BlueCheckBox.NamingContainer, FormView)
        'Evaluate the data from the Grid item and set the Checked property
        ' appropriatly
        If container.DataItem("Blue").GetType.ToString = "System.DBNull" Then
            BlueCheckBox.Checked = False
        Else
            BlueCheckBox.Checked = CBool(container.DataItem("Blue"))
        End If

    End Sub
End Class



<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body style="font-family: Arial;">
    <form id="form1" runat="server">
    <div>
        &nbsp;</div>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="SELECT * FROM [Fav]"
            UpdateCommand="UPDATE [Fav]
                            SET Name=@Name,
                                Color=@Color,
                                Red=@Red,
                                Blue=@Blue
                            WHERE [Room]=@Room">
            <UpdateParameters>
                <asp:Parameter Name=Room Type=String />
                <asp:Parameter Name=Name Type=String />
                <asp:Parameter Name=Color Type=String />
                <asp:Parameter Name=Red Type=Boolean DefaultValue=False ConvertEmptyStringToNull=false />
                <asp:Parameter Name=Blue Type=Boolean DefaultValue=False ConvertEmptyStringToNull=false />

            </UpdateParameters>


         </asp:SqlDataSource>
        <br />
        <table style="width: 514px; height: 154px">
            <tr>
                <td style="width: 100px">
        <asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
            DataSourceID="SqlDataSource1" Height="50px" Width="125px" DataKeyNames="Room">
            <Fields>
                <asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" ReadOnly=True />
                <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
                <asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" />
                <asp:CheckBoxField DataField="Red" HeaderText="Red" SortExpression="Red" />
                <asp:CheckBoxField DataField="Blue" Text="Blue" SortExpression="Blue" />
                <asp:CommandField ShowEditButton="True" />
            </Fields>
        </asp:DetailsView>
                </td>
                <td style="width: 100px">
                    <asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" Width="151px">
                        <EditItemTemplate>
                            Room:
                            <asp:TextBox ID="RoomTextBox" runat="server" Text='<%# Bind("Room") %>'>
                            </asp:TextBox><br />
                            Name:
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
                            </asp:TextBox><br />
                            Color:
                            <asp:TextBox ID="ColorTextBox" runat="server" Text='<%# Bind("Color") %>'>
                            </asp:TextBox><br />
                            Red:
                            <asp:CheckBox ID="RedCheckBox" runat="server" Checked='<%# Bind("Red") %>' /><br />
                            Blue:
                            <asp:CheckBox ID="BlueCheckBox" runat="server" Checked='<%# Bind("Blue") %>' /><br />
                            &nbsp;<br />
                            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                                Text="Update">
                            </asp:LinkButton>
                            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel">
                            </asp:LinkButton>
                        </EditItemTemplate>
                        <InsertItemTemplate>
                            Room:
                            <asp:TextBox ID="RoomTextBox" runat="server" Text='<%# Bind("Room") %>'>
                            </asp:TextBox><br />
                            Name:
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
                            </asp:TextBox><br />
                            Color:
                            <asp:TextBox ID="ColorTextBox" runat="server" Text='<%# Bind("Color") %>'>
                            </asp:TextBox><br />
                            Red:
                            <asp:CheckBox ID="RedCheckBox" runat="server" Checked='<%# Bind("Red") %>' /><br />
                            Blue:
                            <asp:CheckBox ID="BlueCheckBox" runat="server" Checked='<%# Bind("Blue") %>' /><br />
                            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
                                Text="Insert">
                            </asp:LinkButton>
                            <asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel">
                            </asp:LinkButton>
                        </InsertItemTemplate>
                        <ItemTemplate>
                            Room:
                            <asp:Label ID="RoomLabel" runat="server" Text='<%# Bind("Room") %>'></asp:Label><br />
                            Name:
                            <asp:Label ID="NameLabel" runat="server" Text='<%# Bind("Name") %>'></asp:Label><br />
                            Color:
                            <asp:Label ID="ColorLabel" runat="server" Text='<%# Bind("Color") %>'></asp:Label><br />
                            Red:
                            <asp:CheckBox ID="RedCheckBox" runat="server" Enabled="false" /><br />
                            Blue:
                            <asp:CheckBox ID="BlueCheckBox" runat="server" Enabled="false" /><br />
                            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
                                Text="Edit">
                            </asp:LinkButton>
                        </ItemTemplate>
                    </asp:FormView>
                </td>
            </tr>
        </table>
        <br />
        <br />
        <br />
        <br />
    </form>
</body>
</html>

Reply With Quote
  #2 (permalink)  
Old June 26th, 2006, 12:13 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

Do you think you could pare this down to the actual problem locus?

I'd like to help, but I don't have the time to read the volume that you have posted.
Reply With Quote
  #3 (permalink)  
Old June 29th, 2006, 07:46 AM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I figured it out. I used ISNULL of the Sql statement to evaluate the value of the bound checkbox. If the value was indead null, then I used the ISNULL function to assign it a value of "0". Works like a champ.
Now, I have a panel that is only visible if a user checks a bound checkbox in an edititemtemplate of a formview. I can't get the panel to become visible when I check the bound checkbox. How does one solve this? I'm assuming that the checkbox's checked property is not really true/false because it is bound to a datafield. How do I determine the checkbox's value ("0" is unchecked and "-1" is checked) to make the panel visible?

Reply With Quote
  #4 (permalink)  
Old June 29th, 2006, 02:00 PM
Friend of Wrox
 
Join Date: Nov 2004
Location: Port Orchard, WA, USA.
Posts: 1,621
Thanks: 1
Thanked 3 Times in 3 Posts
Default

I don't think that your assumption that "that the checkbox's checked property is not really true/false because it is bound to a datafield" is right. I think it does have an immediate value from being checked, and the form is waiting to impress that value into the underlying data when you move to another record.

What is an "edititemtemplate of a formview?" (You said "I have a panel that is only visible if a user checks a bound checkbox in an edititemtemplate of a formview.")

You should be able to use the Change event of the checkbox, I would think. Is this not so?
Reply With Quote
  #5 (permalink)  
Old June 29th, 2006, 06:13 PM
Authorized User
 
Join Date: Nov 2005
Location: , , .
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I thought so initially, however I haven't seemed to get it to work.

<asp:FormView ID="FormView1" runat="server" AllowPaging="True" DataSourceID="SqlDataSource1" Width="238px">
                        <EditItemTemplate>
                            Room:
                            <asp:TextBox ID="RoomTextBox" runat="server" Text='<%# Bind("Room") %>' Enabled=false>
                            </asp:TextBox><br />
                            Name:
                            <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>'>
                            </asp:TextBox><br />
                            Color:
                            <asp:TextBox ID="ColorTextBox" runat="server" Text='<%# Bind("Color") %>'>
                            </asp:TextBox><br />
                            Red:
                            <asp:CheckBox ID="RedCheckBox" runat="server" Checked='<%# Bind("Red") %>' AutoPostBack=true OnCheckedChanged="RedCheckBox_CheckedChanged" /><br />
                            Blue:
                            <asp:CheckBox ID="BlueCheckBox" runat="server" Checked='<%# Bind("Blue") %>' /><br />
                            <br />
                            <asp:Panel ID="Panel1" runat="server" Height="80px" Visible="False" Width="341px">
                                <table style="width: 334px; height: 37px">
                                    <tr>
                                        <td style="width: 100px; height: 20px">
                                            <asp:CheckBox ID="CheckBox1" runat="server" Text="Garnet" /></td>
                                        <td style="width: 100px; height: 20px">
                                            <asp:CheckBox ID="CheckBox2" runat="server" Text="Gold" /></td>
                                        <td style="width: 100px; height: 20px">
                                            <asp:CheckBox ID="CheckBox3" runat="server" Text="Black" /></td>
                                    </tr>
                                </table>
                            </asp:Panel>
                            <br />
                            <br />
                            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
                                Text="Update">
                            </asp:LinkButton>
                            <asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
                                Text="Cancel">
                            </asp:LinkButton>
                        </EditItemTemplate>

    Protected Sub RedCheckBox_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim RedCheckBox As CheckBox = CType(sender, CheckBox)
        Dim Panel1 As New Panel
        If RedCheckBox.Checked = True Then
            Panel1.Visible = True
        End If
    End Sub

Doesn't work. When I check the checkbox, the panel remains invisible.

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
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
Its a problem about type conversion subhasis.chakraborty General .NET 0 May 6th, 2005 10:50 AM
Re: type conversion fail, need help!! flyfish Access 4 December 21st, 2004 10:31 AM



All times are GMT -4. The time now is 09:46 AM.


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