I have a database which stores the location of photos held on the server and have made a gridview which shows the photo and a checkbox next to each 1. I want to beable to click on the button and delete all the photos off the server and the location held in th database that have been checked in the check box I am using the code below but when i click the delete button it doesn't delete anything and can't work out wots wrong with it can anyone help
the delete button code
protected void DeletePhotobtn_Click(object sender, EventArgs e)
{
for (int vLoop = 0; 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);
}
}
}
the gridview code
<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" Checked="false" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID="DeletePhotobtn" runat="server" Text="Delete" OnClick="DeletePhotobtn_Click" />