Hello everyone!

Basically a user is able to select one or multiple rows using checkboxes in a GridView and then a delete button which deletes selected rows.

The problem is, the code behind for deleting works until I started using UpdatePanel. It just broke and I have no idea what went wrong.

Here's my ASPX code;

<asp:UpdatePanel ID="upl" runat="server">
                        <Triggers>
                            <asp:AsyncPostBackTrigger ControlID="btnSearch" EventName="serverclick" />
                            <asp:AsyncPostBackTrigger ControlID="GvGrade" />
                            <asp:PostBackTrigger ControlID="BtnRegister" />
                        </Triggers>
                        <ContentTemplate>
                            <asp:GridView ID="GvGrade" runat="server" AutoGenerateColumns="false" ClientIDMode="Static">
                                <Columns>
                                    <asp:TemplateField HeaderText="Task ID" HeaderStyle-HorizontalAlign="Center">
                                        <HeaderStyle Width="100" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:Label ID="lblID" CssClass="lblID" runat="server" Text='<%# Eval("drug_id") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <Columns>
                                    <asp:TemplateField HeaderText="Drug Name" HeaderStyle-HorizontalAlign="Center">
                                        <HeaderStyle Width="300" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:Label ID="lblDrugName" CssClass="lblImageName" runat="server" Text='<%# Eval("drug_name") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <Columns>
                                    <asp:TemplateField HeaderText="Drug Description" HeaderStyle-HorizontalAlign="Center">
                                        <HeaderStyle Width="500" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:Label ID="lblDrugDescription" CssClass="lblImage" runat="server" Text='<%# Eval("drug_description") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <Columns>
                                    <asp:TemplateField HeaderText="Task Status" HeaderStyle-HorizontalAlign="Center">
                                        <HeaderStyle Width="200" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:Label ID="lblTaskStatus" CssClass="lblImage" runat="server" Text='<%# Eval("task_status") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <Columns>
                                    <asp:TemplateField HeaderText="Task Type" HeaderStyle-HorizontalAlign="Center">
                                        <HeaderStyle Width="100" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:Label ID="lblTaskType" CssClass="lblImage" runat="server" Text='<%# Eval("task_type") %>'></asp:Label>
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                                <Columns>
                                    <asp:TemplateField HeaderText="Delete Tasks" ItemStyle-Width="100px" ItemStyle-HorizontalAlign="Center">
                                        <HeaderStyle Width="100" BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                                        <ItemStyle HorizontalAlign="Center" />
                                        <ItemTemplate>
                                            <asp:CheckBox ID="chkbox" runat="server" onclick="javascript:HighlightRow(this);" />
                                        </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>
                            <asp:Button ID="BtnRegister" runat="server" class="btn btn-info" Text="Delete" OnClick="BtnLogin_Click" />
                        </ContentTemplate>
                    </asp:UpdatePanel>
                </div>

And here's my code behind.

        protected void showData()
        {
            SqlDataAdapter da = new SqlDataAdapter("select * from Drug", con);
            DataSet ds = new DataSet();
            da.Fill(ds);
            GvGrade.DataSource = ds;
            GvGrade.DataBind();
        }

        protected void DeleteRecord(string drug_name)
        {
            SqlConnection con = new SqlConnection(cs);
            SqlCommand com = new SqlCommand("delete from Drug where drug_name=@ID", con);
            com.Parameters.AddWithValue("@ID", drug_name);
            con.Open();
            com.ExecuteNonQuery();
            con.Close();
        }

        protected void BtnLogin_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow grow in GvGrade.Rows)
            {
                CheckBox chkbox = (CheckBox)grow.FindControl("chkbox");
                if (chkbox.Checked)
                {
                    string drug_name = grow.Cells[1].Text;
                    DeleteRecord(drug_name);
                    showData();
                }
            }
            //Displaying the Data in GridView  
            showData();
        }

Recommended Answers

All 2 Replies

What is or isn't happening? Can you put a breakpoint on BtnLogin_Click and see if it hits that method, then walk through it?

Does that button need to be a postback trigger? Can it be an AsyncPostbackTrigger?

What is the rest of the code on the page? If you've got an UpdatePanel inside of an UpdatePanel, you might be setting yourself up for a bad time.

I've not done any asp.net for a long while so my only input will be about the backend coding.

You are calling showData after every delete rather than deleting all the records required and then calling showData - call it once at the end.

You should also move this statement and it's enclosing code

foreach (GridViewRow grow in GvGrade.Rows)

so that it's within connection open event.

At the moment you are opening a new SQL connection for every delete statement rather than just using the same connection to loop through the grid view and delete the records as required.

One last thing and this is a tip more than anything else, don't call a button BtnRegister if it's going to delete records call it btnDelete or btnDeleteRecords - if you or anybody else have to debug this code in 3 months time, the name BtnRegister will just cause confusion.

Anyway, I hope you manage to sort your problem and I apologise for not being much help in doing so. :)

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.