delete in c# .net

J

jb

i try to delete a record using this code;

string ConStr = "Provider=Microsoft.Jet.OleDB.4.0; Data Source =" +
Application.StartupPath + "\\db1.mdb";
using (OleDbConnection Conn = new OleDbConnection(ConStr))
{

Conn.Open();

OleDbCommand Cmd = new OleDbCommand("Select * from table1 WHERE
PatientNo='" + "11-11001" + "'", Conn);
OleDbDataAdapter dAdapter = new OleDbDataAdapter(Cmd);
DataSet dDataSet = new DataSet();

dAdapter.Fill(dDataSet, "table1");

dDataSet.Tables["table1"].Rows[0].Delete();

dAdapter.Update(dDataSet, "table1");
dDataSet.AcceptChanges();

Conn.Close();

}

but the message is: Update requires a valid DeleteCommand when passed
DataRow collection with delete rows.
 
J

Jeff Johnson

i try to delete a record using this code;
string ConStr = "Provider=Microsoft.Jet.OleDB.4.0; Data Source =" +
Application.StartupPath + "\\db1.mdb";
using (OleDbConnection Conn = new OleDbConnection(ConStr))
{

Conn.Open();

OleDbCommand Cmd = new OleDbCommand("Select * from table1 WHERE
PatientNo='" + "11-11001" + "'", Conn);
OleDbDataAdapter dAdapter = new OleDbDataAdapter(Cmd);
DataSet dDataSet = new DataSet();

dAdapter.Fill(dDataSet, "table1");

dDataSet.Tables["table1"].Rows[0].Delete();

dAdapter.Update(dDataSet, "table1");
dDataSet.AcceptChanges();

Conn.Close();

}

but the message is: Update requires a valid DeleteCommand when passed
DataRow collection with delete rows.

You probably have a larger question in mind, but I'm going to focus on what
you asked exactly, and here goes: Why in the world are you using such
complicated code to delete a single record? If ALL you want to do is delete
patient number 11-11001, just issue a real DELETE command:

OleDbCommand Cmd = new OleDbCommand("DELETE FROM table1 WHERE
PatientNo='11-11001'", Conn);
Cmd.ExecuteNonQuery();

Of course, I'm sure you'll be passing the patient number in as a variable,
so I'd recommend you throw a parameter into the command and then fill it.
(And definitely use parameters, not the string concatenation you showed
above; that's just asking for SQL injection.) I don't know the parameter
syntax for OleDb or I'd have given a sample. (I think it's the question
mark.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top