formview and checkboxes

G

Guest

hey all,
i'm trying to bind my dataset to a formview but i get a message that says:
Conversion from type 'DBNull' to type 'Boolean' is not valid.

in order to fix this do i have to specify false for the bit field in the
database or is theere a config in formview i can modify to accept it?

thanks,
rodchar
 
K

Ken Cox [Microsoft MVP]

Hi,

I'd use a helper function to make sure a DBNull doesn't upset the formview.
Some sample code below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

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

<script runat="server">
Function FixDBNull(ByVal inVal As Object) As String
If inVal Is DBNull.Value Then
Return "false or other string"
Else
Return inVal
End If
End Function

Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
dr = dt.NewRow()
dr(0) = 0
dr(1) = "Item 0"
dr(2) = 1.23
dr(3) = DBNull.Value
dt.Rows.Add(dr)
Return dt
End Function

Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
FormView1.DataSource = CreateDataSource()
FormView1.DataBind()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Formview and Nulls</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:formview id="FormView1" runat="server">
<itemtemplate>
<asp:label id="Label2" runat="server"
text='<%#eval("StringValue") %>'></asp:label><br />
<asp:label id="Label3" runat="server"
text='<%#eval("CurrencyValue","{0:C}") %>'></asp:label><br />
<asp:label id="Label1" runat="server"
text='<%#FixDBNull(eval("Boolean")) %>'></asp:label>
</itemtemplate>
</asp:formview>

</div>
</form>
</body>
</html>
 
G

Guest

Thank you.

Ken Cox said:
Hi,

I'd use a helper function to make sure a DBNull doesn't upset the formview.
Some sample code below.

Let us know if this helps?

Ken
Microsoft MVP [ASP.NET]

<%@ Page Language="VB" %>

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

<script runat="server">
Function FixDBNull(ByVal inVal As Object) As String
If inVal Is DBNull.Value Then
Return "false or other string"
Else
Return inVal
End If
End Function

Function CreateDataSource() As Data.DataTable
Dim dt As New Data.DataTable
Dim dr As Data.DataRow
dt.Columns.Add(New Data.DataColumn _
("IntegerValue", GetType(Int32)))
dt.Columns.Add(New Data.DataColumn _
("StringValue", GetType(String)))
dt.Columns.Add(New Data.DataColumn _
("CurrencyValue", GetType(Double)))
dt.Columns.Add(New Data.DataColumn _
("Boolean", GetType(Boolean)))
dr = dt.NewRow()
dr(0) = 0
dr(1) = "Item 0"
dr(2) = 1.23
dr(3) = DBNull.Value
dt.Rows.Add(dr)
Return dt
End Function

Protected Sub Page_Load _
(ByVal sender As Object, ByVal e As System.EventArgs)
If Not IsPostBack Then
FormView1.DataSource = CreateDataSource()
FormView1.DataBind()
End If
End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Formview and Nulls</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:formview id="FormView1" runat="server">
<itemtemplate>
<asp:label id="Label2" runat="server"
text='<%#eval("StringValue") %>'></asp:label><br />
<asp:label id="Label3" runat="server"
text='<%#eval("CurrencyValue","{0:C}") %>'></asp:label><br />
<asp:label id="Label1" runat="server"
text='<%#FixDBNull(eval("Boolean")) %>'></asp:label>
</itemtemplate>
</asp:formview>

</div>
</form>
</body>
</html>

rodchar said:
hey all,
i'm trying to bind my dataset to a formview but i get a message that says:
Conversion from type 'DBNull' to type 'Boolean' is not valid.

in order to fix this do i have to specify false for the bit field in the
database or is theere a config in formview i can modify to accept it?

thanks,
rodchar
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,203
Latest member
KaliShumat

Latest Threads

Top