C# 在gridview和c中从数据库中删除行#

C# 在gridview和c中从数据库中删除行#,c#,asp.net,gridview,C#,Asp.net,Gridview,我正在使用带有编辑和删除按钮的gridview 当我删除gridview中的特定行时,它将被删除。但我再次重新加载页面,删除的行再次显示。我的意思是,在数据库中并没有删除行 这是我的密码: protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { SqlConnection con = Connection.DBconnection();

我正在使用带有编辑和删除按钮的gridview

当我删除gridview中的特定行时,它将被删除。但我再次重新加载页面,删除的行再次显示。我的意思是,在数据库中并没有删除行

这是我的密码:

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");

            }
        }
和asps文件:

<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>
我不熟悉.net。有人能帮我修一下吗


谢谢,

传递参数无效
@id
更改
@id

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

另一种方式可能是

简短回答 使用commandargument传递ID

完整答案

替换

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


在delete按钮中将Id作为命令参数传递,并访问代码,如下所示

 <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");

        }                                               

else if(e.CommandName==“Deleterow”)
{     
SqlCommand com=新的SqlCommand(“StoredProcedure4”,con);
com.CommandType=CommandType.StoredProcess;
com.Parameters.AddWithValue(“@id”,Convert.ToInt32(e.CommandArgument));
var id=Int32.Parse(例如CommandArgument.ToString());
GridView1.Rows[id].Visible=false;
com.ExecuteNonQuery();
响应重定向(“studentry.aspx”);
}                                               

将SP“StoredProcedure4”代码放在这里。com.Parameters.AddWithValue(“@id”,gr.Cells[0]。Text);替换代码中的这一行。是否有任何异常?没有。没有exception@Imad@Rani,你在“gr.Cells[0].Text”中得到了什么值?我认为在遵循你的答案之后,大写不重要,即使在我的gridview中也无法删除复制的我的answer@Rani,删除此行“GridView1.Rows[id]。Visible=false无需隐藏此内容,因为它已被删除。是..再次显示错误。。s13.postimg.org/elc11n26f/untitled。jpg@Rani,哪一个例外?@Rani,好的,我知道了。请删除这一行“com.Parameters.AddWithValue(“@ID”,gr.Cells[0].Text)”;“删除这一行,因为您最后正在重定向。因此,在删除该行之后,它不会产生任何影响。下一行显示类似这样的错误。您删除了吗?我告诉过您替换您的代码和我的代码。我的代码没有
com.Parameters.AddWithValue(“@ID”,gr.Cells[0].Text”)所以删除它。
<asp:Button runat="server" ID="btndelete" Text="Delete" CommandArgument='<%# Eval("ID") %>' CommandName="Deleterow"></asp:Button>
       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");

            }
     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");

            }
 <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");

        }