delete row from selected gridview and database

Posted by user175084 on Stack Overflow See other posts from Stack Overflow or by user175084
Published on 2009-10-05T17:14:31Z Indexed on 2014/06/09 9:25 UTC
Read the original article Hit count: 246

Filed under:
|
|

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"

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET