• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 634
  • Last Modified:

delete a row from database using TableAdpater command

Hello experts I am trying to delete a row from a database where the row deleted is based upon the PK of the selected row.

I am using the following Delete Command from within my Table Adapter
DELETE FROM vaUCregistration WHERE regID = @regID;

I am using tableAdapters for this and the query is titled Delete_RegRow(@regID)

When I try to capture the selected rows PK and insert it into the query as a parameter I get the error.
 
Data source is an invalid type.  It must be either an IListSource, IEnumerable, or IDataSource.

Below is the code on the RowCommand

would someone please help me understand what I need to do to correct this and possibly a sample?

Thank you in advance
protected void dg_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("Delete_Item"))
        {
            string SessID = sessLBL.Text;
            GridView gv = (GridView)sender;
            string myID = gv.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text; //captures row
            int regID = Convert.ToInt32(myID);
 
            idLBL.Text = myID.ToString(); //returns row 
            vaUCregistrationTA remUserRec = new vaUCregistrationTA();
            dg.DataSource = remUserRec.Delete_RegRow(regID);
            dg.DataBind();
            dg.Dispose();
	}
    }

Open in new window

0
rlcrews
Asked:
rlcrews
  • 2
1 Solution
 
GiftsonDJohnCommented:
The Delete_RegRow command will be executed as non query and indeed returns number of rows affected but it won't return any dataset back. You should call the Delete_RegRow separately and then you have to use remUserRec.Fill(DataSet) and then you have to bind the DataSet to the dg(DataGrid) object.
0
 
rlcrewsAuthor Commented:
I'm sorry I don't follow your comment.  The command Delete_ RegRow calls the sql delete command from my table adapter the argument regID is the PK captured from my grid view.

the first part of the code evlaluates the row and returns the value from the regID field in my grid view to a variable (int RegID).

that variable is then being inserted into the table adapter command where I wanted to pass it as a parameter back to the sql command so that the row from the database could be removed.

Can you help clarify the above a little more so that I can get a better understanding.

Thank you
0
 
rlcrewsAuthor Commented:
Ok so after working with the formula I relaized I do not need to set the command as a datasource. so the corrected formula is as follows:


    protected void dg_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        string SessID = sessLBL.Text;
        GridView gv = (GridView)sender;
        string myID = gv.Rows[Convert.ToInt32(e.CommandArgument)].Cells[2].Text;
        int regID = Convert.ToInt32(myID);

        if (e.CommandName.Equals("Delete_Item"))
        {
            idLBL.Text = myID.ToString();
            vaUCregistrationTA remUserRec = new vaUCregistrationTA();
            remUserRec.Delete_RegRow(regID);
}}

Of course I do not have the data grid rebinding or refreshing but the delete command now works.
0

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now