简体   繁体   中英

delete row from selected gridview and database

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

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

<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>

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

You're not adding an @original_MachineGroupName parameter to your query. Your query references multiple variables, and you've only added one of them.

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

You'll need to find the extra information to satisfy your query, or else change your query to remove the need for them.

You have several parameters in you query ie @original_MachineGroupName but are only adding one actual parameter to you command object. You will need one for each parameter in the query.

The following code will delete the row from the database if the string you are passing to method is primary or unique key in table:

string sql = "DELETE FROM [MachineGroups] WHERE [MachineGroupID] = 
                  @original_MachineGroupID";
    SqlCommand cmd = new SqlCommand(sql, conn);
    cmd.Parameters.AddWithValue("@original_MachineGroupID", item);

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.

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