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" />

Recommended Answers

All 7 Replies

when you debug it does PhotoID have the right integer in it? does the sql return any errors?

when you debug it does PhotoID have the right integer in it? does the sql return any errors?

Just noticed from debugging that its not going into the if statement beacause the PhotoChk.Checked always = false but can't work out why

Does it do the same if you remove the Checked="false" part?

Does it do the same if you remove the Checked="false" part?

yeah tried deleting the if statement but it still doesn't delete any photos

Then are you sure that the checkbox you see is actually one from your form? it would strike me its not found it for some reason, but because you forced it into checkbox type it says its a valid checkbox.

oh right ok i see i'm quite new to this how do i go about getting it to see the checkbox cheers for all ur help

thats what debugging is for, step through see what values each thing has..

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.