I'm only trying to Update a record in a db. I can't even get the DetailsView to appear because I keep getting: 'CheckBoxList1' has a SelectedValue which is invalid because it does not exist in the list of items.
Parameter name: value
I know it has to do with the Null value but how do I code for it? Also, how do I code my checkboxlist to keep the values the user selects and then update to the database. My selection gets lost and there is no update. I'm at wits end...
[u]
First page with GridView:</u>
<%@ 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>
<form id="form1" runat="server">
<div>
<strong><span style="font-size: 10pt; font-family: Arial">webcharts<br />
</span></strong>
</div>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataSourceID="SqlDataSource1" DataKeyNames="Room" Font-Names="Arial">
<Columns>
<asp:HyperLinkField Text="Select" DataNavigateUrlFields="Room" DataNavigateUrlFormatString="Default2.aspx?Room={0 }" />
<asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:BoundField DataField="Color" HeaderText="Color" SortExpression="Color" NullDisplayText="Red" />
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM [Fav]">
</asp:SqlDataSource>
</form>
</body>
</html>
[u]Second page with DetailsView</u>:
<%@ Page Language="
VB" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
Protected Sub DetailsView1_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DetailsViewPageEventArgs )
If Not IsPostBack Then
Dim i As Integer
Dim CheckBoxList1 As New CheckBoxList
For i = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(i).Selected Then
Response.Write("You've selected " & CheckBoxList1.Items(i).Text & "<BR>")
End If
Next
End If
End Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<strong><span style="font-size: 10pt; font-family: Arial">webcharts<br />
</span></strong>
</div>
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
Height="50px" Width="125px" DataKeyNames="Room" Font-Names="Arial" DefaultMode="Edit" OnPageIndexChanging="DetailsView1_PageIndexChangin g">
<Fields>
<asp:BoundField DataField="Room" HeaderText="Room" SortExpression="Room" ReadOnly="True" />
<asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
<asp:TemplateField HeaderText="Color" SortExpression="Color" ConvertEmptyStringToNull="False">
<EditItemTemplate>
<asp:CheckBoxList ID="CheckBoxList1" runat="server" SelectedValue='<%# Bind("Color") %>'>
<asp:ListItem>Red</asp:ListItem>
<asp:ListItem>Green</asp:ListItem>
<asp:ListItem>Blue</asp:ListItem>
</asp:CheckBoxList>
<br />
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Fields>
</asp:DetailsView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
CancelSelectOnNullParameter="false"
SelectCommand="SELECT * FROM [Fav] WHERE (Room=@Room)"
UpdateCommand="UPDATE [Fav]
SET [Name]=@Name,
[Color]=@Color
WHERE [Room]=@Room">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="Room" Name="Room" Type="String" />
</SelectParameters>
<UpdateParameters>
<asp:Parameter Name="Name" Type="String" ConvertEmptyStringToNull="false" />
<asp:Parameter Name="Color" Type="string" ConvertEmptyStringToNull="false" DefaultValue="Red"/>
</UpdateParameters>
</asp:SqlDataSource>
<br />
<asp:HyperLink ID="HyperLink1" Font-Names="arial" runat="server" NavigateUrl="~/Default.aspx">Return to Main</asp:HyperLink>
</form>
</body>
</html>
Seems like it should be straightfoward. I just can't figure out how to code for it. Thanks for the help.
Greg