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