Subject: CheckBoxList and Null Values
Posted By: steve35719 Post Date: 4/11/2006 10:57:51 AM
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...

First page with GridView:

<%@ 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 />
            <hr />
        </span></strong>
    
    </div>
        &nbsp;<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>

Second page with DetailsView:

<%@ 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 />
            <hr />
        </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_PageIndexChanging">
            <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>
                        &nbsp;<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 />
        &nbsp;<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

Reply By: steve35719 Reply Date: 4/21/2006 9:42:00 AM
Any thoughts?


Go to topic 43235

Return to index page 307
Return to index page 306
Return to index page 305
Return to index page 304
Return to index page 303
Return to index page 302
Return to index page 301
Return to index page 300
Return to index page 299
Return to index page 298