Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old April 11th, 2006, 10:57 AM
Authorized User
 
Join Date: Nov 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default CheckBoxList and Null Values

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

[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>
                        &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

 
Old April 21st, 2006, 09:42 AM
Authorized User
 
Join Date: Nov 2005
Posts: 16
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Any thoughts?






Similar Threads
Thread Thread Starter Forum Replies Last Post
how to handle null values imaginevictor ASP.NET 1.x and 2.0 Application Design 1 March 20th, 2007 01:52 AM
Null values jmcgranahan BOOK: Access 2003 VBA Programmer's Reference 0 August 29th, 2006 02:25 PM
Get Values from Checkboxlist derekl ASP.NET 1.0 and 1.1 Basics 3 May 26th, 2006 09:46 AM
Handling Null Values nvenkat75 ADO.NET 2 January 16th, 2006 05:46 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.