Click here to Skip to main content
15,887,596 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
My sql table has 3 columns. Username,Password,Usertype.
In a gridview im showing the users with gridview columns: Username, usertype. When i click delete rows should be deleted from sql as well as gridview. There is no error, pls let me know whats the mistake in my code.

HTML

XML
<asp:GridView ID="GridView1" runat="server" Width="40%" CellSpacing="2" AutoGenerateColumns="false" CellPadding="4" AllowPaging="true"
        PageSize="10" ForeColor="#333333"
        GridLines="Vertical" OnPageIndexChanging="GridView1_PageIndexChanging" DataKeyNames="Username"
       OnRowDeleting="GridView1_RowDeleting"  >

       

       <Columns>

           <asp:TemplateField HeaderText="Username">
                   <ItemTemplate>
                       <asp:Label ID="lbl2" runat="server" Text='<%#Bind("Username") %>'></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                      <asp:TextBox ID="txtName" runat="server" Width="140px" Text='<%#Bind("Username")%>'></asp:TextBox>
                  </EditItemTemplate>
                   <ItemStyle HorizontalAlign="Center" />
               </asp:TemplateField>
            <asp:TemplateField HeaderText="UserType">
                   <ItemTemplate>
                       <asp:Label ID="lbl3" runat="server" Text='<%#Bind("UserType") %>'></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                      <asp:TextBox ID="txtName" runat="server" Width="140px" Text='<%#Bind("UserType")%>'></asp:TextBox>
                  </EditItemTemplate>
                   <ItemStyle HorizontalAlign="Center" />
               </asp:TemplateField>

              <asp:TemplateField HeaderText="Delete User">
                   <ItemTemplate>
                        <span onclick="return confirm('Are you sure to Delete the record?')">
                       <asp:Button ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" />
                   </ItemTemplate>


                   <ItemStyle HorizontalAlign="Center" />
               </asp:TemplateField>
                   <asp:TemplateField >
                   <EditItemTemplate>
                        <asp:Button ID="btnCancel" runat="server" CommandName="Cancel" Text="Cancel" />
                   </EditItemTemplate>
                   <ItemStyle HorizontalAlign="Center" />
               </asp:TemplateField>


           </Columns>
       </asp:GridView

>




protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
       {
           //int Id = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Values[0]);
           Label Username = GridView1.Rows[e.RowIndex].FindControl("lbl2") as Label;
           Label Usertype = GridView1.Rows[e.RowIndex].FindControl("lbl3") as Label;
           string constr = WebConfigurationManager.ConnectionStrings["ConnectString"].ConnectionString;
           using (SqlConnection con = new SqlConnection(constr))
           {
               using (SqlCommand cmd = new SqlCommand("[Sp_InsertUserRegisteration]"))
               {
                   using (SqlDataAdapter sda = new SqlDataAdapter())
                   {
                       cmd.CommandType = CommandType.StoredProcedure;
                       cmd.Parameters.AddWithValue("@Action", "DELETE");
                       cmd.Parameters.AddWithValue("@Username", Username);

                       cmd.Parameters.AddWithValue("@Usertype", Usertype);
                       cmd.Connection = con;
                       con.Open();
                       cmd.ExecuteNonQuery();
                       con.Close();
                   }
               }
           }
           this.BindData();
       }
Posted
Updated 21-Feb-19 14:31pm
v2
Comments
RossMW 16-May-15 15:53pm    
Do you have a primary key on the table? Updates and deletes generally don't work without it.
Sinisa Hajnal 17-May-15 12:42pm    
there is no error or it gets handled / swallowed along the way. Put try..catch inside rowDeleting. Also, make sure that your SP Works when called through SQL client with same parameters (that is, check what you're sending into the SP. Put PRINT statements into SP so you Know which parts are executed.

I also recommend RedGate SQLProfiler.
Shivaram_i 19-May-15 1:01am    
Do u have any id in this grid view...???Because u should compare this id with your stored procedure id as well..Then only deletion operation is working...

1 solution

protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)

The above event will be triggered if Delete command button is clicked.

But I couldn't find any command button in your code. You only have a delete button inside Item Template.

https://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdeleting(v=vs.110).aspx[^]


Refer to the below link:
http://aspsnippets.com/Blue/Articles/ASPNet-GridView---Delete-Row-with-Confirmation.aspx[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900