【问题标题】:Delete row from database in gridview and c#在gridview和c#中从数据库中删除行
【发布时间】:2015-11-05 05:05:06
【问题描述】:

我正在使用带有编辑和删除按钮的 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>

存储过程:

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

我是 .net 的新手。谁能帮我解决这个问题?

谢谢,

【问题讨论】:

  • 把你的SP“StoredProcedure4”代码放在这里。
  • com.Parameters.AddWithValue("@id", gr.Cells[0].Text);替换代码中的这一行。
  • 你有什么异常吗?
  • 没有。没有得到任何异常@Imad
  • @Rani,你在“gr.Cells[0].Text”中得到什么值?

标签: c# asp.net gridview


【解决方案1】:

其他方式可能是,

简答 使用命令参数传递 ID

完整答案

替换

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

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

在 aspx 和 cs 中

替换

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

            }

【讨论】:

  • 删除该行,因为您最终要重定向。所以不影响
  • 删除该行之后。下一行显示像这样的错误s13.postimg.org/elc11n26f/untitled.jpg
  • 你删除了吗?
  • 我告诉过你替换你的代码和我的。我的代码没有com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);,所以删除它。
【解决方案2】:

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

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

【讨论】:

  • 我认为大小写无关紧要
  • 按照你的回答后,即使在我的gridview中也无法删除
【解决方案3】:

在删除按钮中将 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");

        }                                               

【讨论】:

  • 我的答案的复制版本
  • @Rani,删除这一行“GridView1.Rows[id].Visible = false;”无需隐藏,因为它已被删除。
  • 是..再次显示错误.. s13.postimg.org/elc11n26f/untitled.jpg
  • @Rani,哪个例外?
  • @Rani,好的,我知道了,请同时删除这一行 "com.Parameters.AddWithValue("@ID", gr.Cells[0].Text);"
猜你喜欢
  • 1970-01-01
  • 2016-01-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-05-15
相关资源
最近更新 更多