Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I hope someone can help me, I am very new to all of this so I this might not be the best way to do what I want to do. Any help would be great!

I have the following function:

public void fnDelete(int i)
{
    con = new SqlConnection(conString);
    //ask user if wanting to delete
    DialogResult dr = MessageBox.Show("Are you sure you want to delete this contact ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (dr == DialogResult.Yes) //if 
    {
        //user clicked the "Delete" button
        DataTable tbl = new DataTable();
        tbl = this.dSet.Tables[0];
        tbl.Rows[i].Delete(); //delete the row

        int j;
        j = 20;
                    
        SqlCommand DeleteCommand = new SqlCommand("sp_deleteContact '" + j + "'", con);
                
        dAdapter.DeleteCommand = DeleteCommand;

        this.dAdapter.Update(tbl); //update the table
        dSet.AcceptChanges();
    }		  
}


I have a datatable that looks like this:

contactID | firtsName
2         | Terence
4         | John

When I call the function I call it like this:

fnDelete(currManager.Position);

So that deletes the row out of the datatable, but how can I get the value of the contactID into j so I can delete from the database to?

Added this:
var j = tbl.Rows[i][0];


Ok so this is what I have come up with, is this a good way of doing it?

public void fnDelete(int i)
{
    con = new SqlConnection(conString);
    //ask user if wanting to delete
    DialogResult dr = MessageBox.Show("Are you sure you want to delete this contact ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
    if (dr == DialogResult.Yes) //if
    {
        //user clicked the "Delete" button
        DataTable tbl = new DataTable();
        tbl = this.dSet.Tables[0];

        <big>var j = tbl.Rows[i][0];</big>
        SqlCommand DeleteCommand = new SqlCommand("sp_deleteContact '" + j + "'", con);
        dAdapter.DeleteCommand = DeleteCommand;

        tbl.Rows[i].Delete(); //delete the row

        this.dAdapter.Update(tbl); //update the table
        dSet.AcceptChanges();
    }
}
Posted
Updated 6-Jun-10 9:43am
v4

1 solution

Do you see the bit where you say
this.dAdapter.Update(tbl); //update the table
Have a look at MSDN[^]
 
Share this answer
 
Comments
Terence van Schalkwyk 6-Jun-10 13:27pm    
I am not sure what I am looking for...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900