简体   繁体   中英

Delete row from database in gridview and c#

I'm using gridview with edit and delete button.

When i delete particular row in gridview, it will be removed. but again i reload the page, the deleted row again displayed. I mean, row is not remove in database.

Here is my code:

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SqlConnection con = Connection.DBconnection();
            if (e.CommandName == "EditRow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;
                int index = gr.RowIndex; 
                hiddenfield.Value = index.ToString(); 
                Textid.Text = gr.Cells[1].Text;
                Textusername.Text = gr.Cells[2].Text;
                Textclass.Text = gr.Cells[3].Text;
                Textsection.Text = gr.Cells[4].Text;
                Textaddress.Text = gr.Cells[5].Text;
            }
            else if (e.CommandName == "Deleterow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;      
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                var id = Int32.Parse(e.CommandArgument.ToString());                
                GridView1.Rows[id].Visible = false;
                com.ExecuteNonQuery();
                Response.Redirect("studententry.aspx");

            }
        }

and asps file:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="ID" DataSourceID="SqlDataSource1" 
        OnRowCommand="GridView1_RowCommand" AutoGenerateSelectButton="True" 
        EnablePersistedSelection="True" BackColor="White" EnableViewState="true" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
        <Columns>
            <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False" 
                ReadOnly="True" SortExpression="ID" />
            <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
            <asp:BoundField DataField="Class" HeaderText="Class" SortExpression="Class" />
            <asp:BoundField DataField="Section" HeaderText="Section" 
                SortExpression="Section" />
            <asp:BoundField DataField="Address" HeaderText="Address" 
                SortExpression="Address" />
            <asp:TemplateField HeaderText="Edit">
               <ItemTemplate>
                 <asp:Button runat="server" ID="btnedit" Text="Edit" CommandName="EditRow"></asp:Button>                    
               </ItemTemplate>
                <ControlStyle BorderColor="#CCFF66" />
          </asp:TemplateField>
          <asp:TemplateField HeaderText="Delete">
          <ItemTemplate>
                 <asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Deleterow"></asp:Button>                    
               </ItemTemplate>
          </asp:TemplateField>            
        </Columns>
        <SelectedRowStyle BackColor="#FF66FF" />
    </asp:GridView>

storedprocedure:

ALTER PROCEDURE StoredProcedure4
    (
    @id int 
)
AS
begin
Delete from Student where id=@id
End

I'm new to .net. can anyone help me to fix this?

Thanks,

pass parameter not valid @id change @ID

ALTER PROCEDURE StoredProcedure4
(
    @ID as int=0 
)
AS
begin
Delete from Student where id=@ID
End

Alternative way could be,

Short answer Pass ID using commandargument

Full answer

Replace

<asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Container.DataItemIndex %>' CommandName="Deleterow"></asp:Button>

By

<asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Eval("ID") %>' CommandName="Deleterow"></asp:Button>

in aspx and in cs

Replace

       else if (e.CommandName == "Deleterow")
            {
                GridViewRow gr = (GridViewRow)((Button)e.CommandSource).NamingContainer;      
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);
                var id = Int32.Parse(e.CommandArgument.ToString());                
                GridView1.Rows[id].Visible = false;
                com.ExecuteNonQuery();
                Response.Redirect("studententry.aspx");

            }

By

     else if (e.CommandName == "Deleterow")
            {     
                SqlCommand com = new SqlCommand("StoredProcedure4", con);
                com.CommandType = CommandType.StoredProcedure;
                com.Parameters.AddWithValue("@ID", Convert.ToInt32(e.CommandArgument));
                var id = Int32.Parse(e.CommandArgument.ToString());                
                GridView1.Rows[id].Visible = false;
                com.ExecuteNonQuery();
                Response.Redirect("studententry.aspx");

            }

Pass Id as command argument in delete button and access on code behind like below.

 <asp:TemplateField HeaderText="Delete">
      <ItemTemplate>
             <asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Eval("Id") %>' CommandName="Deleterow"></asp:Button>                    
           </ItemTemplate>
      </asp:TemplateField>


else if (e.CommandName == "Deleterow")
        {     
            SqlCommand com = new SqlCommand("StoredProcedure4", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.AddWithValue("@id", Convert.ToInt32(e.CommandArgument));
            var id = Int32.Parse(e.CommandArgument.ToString());                
            GridView1.Rows[id].Visible = false;
            com.ExecuteNonQuery();
            Response.Redirect("studententry.aspx");

        }                                               

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM