从选定的GridView和数据库中删除行 [英] delete row from selected gridview and database

查看:54
本文介绍了从选定的GridView和数据库中删除行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从gridview和数据库中删除一行...如果在gridview中单击delte linkbutton,则应将其删除.我开始行索引如下:

i am trying to delete a row from the gridview and database... It should be deleted if a delte linkbutton is clicked in the gridview.. I am gettin the row index as follows:

protected void LinkButton1_Click(object sender, EventArgs e)
{
  LinkButton btn = (LinkButton)sender;
  GridViewRow row = (GridViewRow)btn.NamingContainer;
  if (row != null)
  {
    LinkButton LinkButton1 = (LinkButton)sender;

    // Get reference to the row that hold the button
    GridViewRow gvr = (GridViewRow)LinkButton1.NamingContainer;

    // Get row index from the row
    int rowIndex = gvr.RowIndex;
    string str = rowIndex.ToString();
    //string str = GridView1.DataKeys[row.RowIndex].Value.ToString();
    RemoveData(str); //call the delete method

  }
}

现在我要删除它...所以我在这段代码上有问题..我收到错误消息

now i want to delete it... so i am having problems with this code.. i get an error

必须声明标量变量"@original_MachineGroupName" ...任何建议

Must declare the scalar variable "@original_MachineGroupName"... any suggestions

private void RemoveData(string item)
{
  SqlConnection conn = new SqlConnection(@"Data Source=JAGMIT-PC\SQLEXPRESS;
  Initial Catalog=SumooHAgentDB;Integrated Security=True");
  string sql = "DELETE FROM [MachineGroups] WHERE [MachineGroupID] = 
                @original_MachineGroupID;
  SqlCommand cmd = new SqlCommand(sql, conn);
  cmd.Parameters.AddWithValue("@original_MachineGroupID", item);

  conn.Open();

  cmd.ExecuteNonQuery();
  conn.Close();
}

Blockquote

Blockquote

<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ConnectionString="<%$ ConnectionStrings:SumooHAgentDBConnectionString %>" 
    SelectCommand="SELECT MachineGroups.MachineGroupID, MachineGroups.MachineGroupName,
    MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, 
    MachineGroups.CanBeDeleted, COUNT(Machines.MachineName) AS Expr1, 
    DATENAME(month, (MachineGroups.TimeAdded - 599266080000000000) / 864000000000) + 
    SPACE(1) + DATENAME(d, (MachineGroups.TimeAdded - 599266080000000000) / 
     864000000000) + 
    ', ' + DATENAME(year, (MachineGroups.TimeAdded - 599266080000000000) /
    864000000000) AS Expr2 FROM MachineGroups FULL OUTER JOIN Machines ON 
    Machines.MachineGroupID = MachineGroups.MachineGroupID GROUP BY 
    MachineGroups.MachineGroupID, MachineGroups.MachineGroupName, 
    MachineGroups.MachineGroupDesc, MachineGroups.TimeAdded, 
    MachineGroups.CanBeDeleted" 
  DeleteCommand="DELETE FROM [MachineGroups] WHERE 
                  [MachineGroupID] =@original_MachineGroupID" >
        <DeleteParameters>
            <asp:Parameter Name="@original_MachineGroupID" Type="Int16" />
            <asp:Parameter Name="@original_MachineGroupName" Type="String" />
            <asp:Parameter Name="@original_MachineGroupDesc" Type="String" />
            <asp:Parameter Name="@original_CanBeDeleted" Type="Boolean" />
            <asp:Parameter Name="@original_TimeAdded" Type="Int64" />
        </DeleteParameters>   
</asp:SqlDataSource>

我仍然遇到错误:必须声明标量变量" @original_MachineGroupID "

I still get an error : Must declare the scalar variable "@original_MachineGroupID"

推荐答案

还请注意您的数据绑定.如果您从数据库中删除一行,则不会从gridview中删除数据.您将需要重新获取数据并再次对其进行数据绑定.

Watch your databinding also. If you delete a row from the database, that doesn't delete the data from the gridview. You will need to re-fetch your data and databind it again.

这篇关于从选定的GridView和数据库中删除行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆