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);
DialogResult dr = MessageBox.Show("Are you sure you want to delete this contact ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
DataTable tbl = new DataTable();
tbl = this.dSet.Tables[0];
tbl.Rows[i].Delete();
int j;
j = 20;
SqlCommand DeleteCommand = new SqlCommand("sp_deleteContact '" + j + "'", con);
dAdapter.DeleteCommand = DeleteCommand;
this.dAdapter.Update(tbl);
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);
DialogResult dr = MessageBox.Show("Are you sure you want to delete this contact ? ", "Confirm deleting", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (dr == DialogResult.Yes)
{
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();
this.dAdapter.Update(tbl);
dSet.AcceptChanges();
}
}