PC Review


Reply
Thread Tools Rate Thread

delete in c# .net

 
 
jb
Guest
Posts: n/a
 
      17th Mar 2011
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.
 
Reply With Quote
 
 
 
 
Jeff Johnson
Guest
Posts: n/a
 
      17th Mar 2011
"jb" <(E-Mail Removed)> wrote in message
news:fd6fedea-acd3-4462-8eec-(E-Mail Removed)...

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.)


 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete data in a linked Excel sheet using Access code or seql delete Rocky Microsoft Access External Data 9 26th Jun 2005 12:42 AM
Re: Macro to delete sheets and saves remaining file does not properly delete module gazornenplat Microsoft Excel Programming 0 22nd Jun 2005 01:12 AM
Macro to delete sheets and saves remaining file does not properly delete module pherrero Microsoft Excel Programming 7 21st Jun 2005 05:16 PM
Delete every 3rd row, then delete rows 2-7, move info f/every 2nd row up one to the end and delete the row below Annette Microsoft Excel Programming 2 21st Sep 2004 02:40 PM
Re: When I highlight a sentence to delete, Word won't let me. I have to backspace. How can I deleted selected text with my delete key? Bill Foley Microsoft Word Document Management 1 4th Feb 2004 11:06 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:00 PM.