deleting out ouf a database with checkboxes
I have a web page showing all the photos with locations held in a database with a checkbox next to each 1 and a button at the bottom. I am trying to get it so when the user clicks the button all the checkboxes that are checked delete the photos location held in the databse and photo from the server. But with the code below it never gets in to the if statement " if (PhotoChk.Checked == true)" beacuse for some reason no matter wot checkboxes are checked it never detects them as being checked. If I change the if (PhotoChk.Checked == true) to if (PhotoChk.Checked == false) it deletes all the photos reqardless of wots boxes are checked. Can any help me I am a noob to this and can't work out why its not workeing cheers
my aspx file
<%@ Page Language="C#" MasterPageFile="~/fowlmereplaygroup.master" AutoEventWireup="true" CodeFile="AdminPhoto.aspx.cs" Inherits="AdminPhoto" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<h1>Photo Admin</h1>
<asp:Label ID="dbErrorMessage" runat="server" />
<asp:GridView ID="PhotoView" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="PhotoIDlbl" Text='<%# Eval("PhotoID") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="PhotoLocationlbl" Text='<%# Eval("PhotoLocation") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Photo">
<ItemTemplate>
<asp:Image ID="Image1" ImageUrl='<%# Eval("PhotoLocation") %>' runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Delete">
<ItemTemplate>
<asp:CheckBox ID="PhotoChk" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="Deletebtn" runat="server" Text="Delete" OnClick="Deletebtn_Click" />
</asp:Content>
my aspx.cs file
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class AdminPhoto : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
// Define data objects
SqlConnection conn;
SqlCommand comm;
SqlDataReader reader;
// Read the connection string from Web.Config
string connectionString = ConfigurationManager.ConnectionStrings["fowlmereplaygroup"].ConnectionString;
// Initialise connection
conn = new SqlConnection(connectionString);
// Create Command
comm = new SqlCommand("SELECT PhotoID, PhotoLocation FROM Photos", conn);
// Enclose database code in try catch finally
try
{
// Open the connection
conn.Open();
// Execute the command
reader = comm.ExecuteReader();
// Fill the grid with data
PhotoView.DataSource = reader;
PhotoView.DataBind();
// Close the reader
reader.Close();
}
finally
{
// Close the connectionString
conn.Close();
}
}
protected void Deletebtn_Click(object sender, EventArgs e)
{
for (int vLoop = 13; vLoop < PhotoView.Rows.Count; vLoop++)
{
CheckBox PhotoChk = (CheckBox)PhotoView.Rows[vLoop].FindControl("PhotoChk");
if (PhotoChk.Checked == true)
{
Label PhotoIDlbl = (Label)PhotoView.Rows[vLoop].FindControl("PhotoIDlbl");
int PhotoID = Convert.ToInt32(PhotoIDlbl.Text);
// using the PhotoID, delete that record. by using sqlcommand and its executenonquery() method
// Define data objects
SqlConnection conn;
SqlCommand comm;
// Read the connection string from Web.Config
string connectionString = ConfigurationManager.ConnectionStrings["fowlmereplaygroup"].ConnectionString;
// Initialise connection
conn = new SqlConnection(connectionString);
// Create Command
comm = new SqlCommand("DELETE FROM Photos WHERE PhotoID=@PhotoID", conn);
// Add Comand Parameters
comm.Parameters.Add("@PhotoID", System.Data.SqlDbType.Int);
comm.Parameters["@PhotoID"].Value = PhotoIDlbl.Text;
try
{
// Open the connection
conn.Open();
// Execute the command
comm.ExecuteNonQuery();
}
catch
{
// Display error message
dbErrorMessage.Text = "Error deleting event";
}
finally
{
// Close the connection
conn.Close();
}
Label PhotoLocationlbl = (Label)PhotoView.Rows[vLoop].FindControl("PhotoLocationlbl");
string filepath = PhotoLocationlbl.Text;
System.IO.File.Delete(filepath);
}
}
}
}