我正在尝试从gridview和数据库中删除一行... 如果在gridview中单击了delte linkbutton,则应将其删除。 我按行如下进行行索引:
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
}
}
现在我要删除它...所以我遇到了这段代码的问题..我收到错误
必须声明标量变量“@original_MachineGroupName”...任何建议
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();
}
块引用
<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
”
答案 0 :(得分:0)
您没有在查询中添加@original_MachineGroupName参数。您的查询引用了多个变量,并且您只添加了其中一个变量。
cmd.Parameters.AddWithValue("@original_MachineGroupID", item);
cmd.Parameters.AddWithValue("@original_MachineGroupName", itemName);
cmd.Parameters.AddWithValue("@original_MachineGroupDesc", itemDesc);
cmd.Parameters.AddWithValue("@original_CanBeDeleted", true);
cmd.Parameters.AddWithValue("@original_TimeAdded", time);
您需要找到满足查询的额外信息,否则请更改您的查询以消除对它们的需求。
答案 1 :(得分:0)
您在查询中有几个参数,即@original_MachineGroupName,但只向命令对象添加一个实际参数。查询中的每个参数都需要一个。
答案 2 :(得分:0)
如果要传递给method的字符串是表中的主键或唯一键,则以下代码将从数据库中删除该行:
string sql = "DELETE FROM [MachineGroups] WHERE [MachineGroupID] =
@original_MachineGroupID";
SqlCommand cmd = new SqlCommand(sql, conn);
cmd.Parameters.AddWithValue("@original_MachineGroupID", item);
答案 3 :(得分:0)
同时观察您的数据绑定。如果从数据库中删除一行,则不会从gridview中删除数据。您需要重新获取数据并再次对其进行数据绑定。