Im trying to get the values of checkboxes in a datalist and then display them in a label on a page, but im getting a nullreference error. on the line... if(chk.Checked)

i know these errors are asked for alot, but after researching i cannot find whats wrong :( please help!

//on page load
  private void Bind()
    {
        DataTable dt = new DataTable();
        SqlConnection connection = new SqlConnection(GetConnectionString());
        
        try
        {
            connection.Open();
            string sqlStatement = "SELECT * FROM Images";
            SqlCommand sqlCmd = new SqlCommand(sqlStatement, connection);
            SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
            sqlDa.Fill(dt);
            
            if (dt.Rows.Count > 0)
            {
                DataList1.DataSource = dt;
                DataList1.DataBind();
            }
            
        }

        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Fetch Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }

        finally
        {
            connection.Close();

        }

//on button click
  private void GetCheckedItems(){

         string Dl;
         foreach (DataListItem dli in DataList1.Items)
         {
             CheckBox chk = (CheckBox)dli.FindControl("ImageID");
             if (chk.Checked)
             {
                 Dl += (chk.Text + "<br />");
             }
         }
         Label1.Text = Dl;

    }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GalleryImageDelete.aspx.cs" Inherits="Default2" %>

<!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 id="Head1" runat="server">
    <title></title>
</head>
<body>

    <form id="form1" runat="server">
    <p style="text-align: right">
        
        <asp:DataList ID="DataList1" runat="server" RepeatDirection="Horizontal"  DataKeyField="ImageID">
        
         <ItemTemplate>
                    <asp:Image ID="Image" Height="100" Width ="100" runat="server" ImageUrl='<%# "ImageHandler.ashx?ImageID=" + Eval("ImageID")  %>' />
                    <br />
                    <asp:CheckBox ID=CheckBox1 Runat=server Text='<%# Eval ("ImageID")%>' 
                        ></asp:CheckBox>
        </ItemTemplate>
        </asp:DataList>
    </p>
    <p>
        <asp:Button ID="Button10" runat="server" onclick="Button10_Click" 
            Text="Button" />
   
    </form>
</body>
</html>

please help a damsel in distress! :) xx

Recommended Answers

All 4 Replies

i know its only been 3 hours - but im still stuck on this - if anyone could help (or even just tell me where to start looking if im asking for too much) i would be verrrrrry thankfull :)

There is no "ImageID" control in the DataList..
CheckBox id is = "CheckBox1"

private void GetCheckedItems(){

         string Dl;
         foreach (DataListItem dli in DataList1.Items)
         {
             [B]CheckBox chk = (CheckBox)dli.FindControl("CheckBox1");[/B]
             if (chk.Checked)
             {
                 Dl += (chk.Text + "<br />");
             }
         }
         Label1.Text = Dl;

    }

hope that will fix your problem..

commented: amazing help :) +2

Thank you so much! Your a star, i couldn't understand what was meant to go in there! works perfectly :)

happy 2 help but stay lil focused on your code :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.