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>
</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 />
<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>