0

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

2
Contributors
4
Replies
6
Views
7 Years
Discussion Span
Last Post by dnanetwork
0

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 :)

1

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

Votes + Comments
amazing help :)
0

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

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.