Visual Basic Code , VB.NET Code, VB Code
  Home   :  Code   :  Forums   :  Submit   :  Mailing List   :  About   :  Contact


delete in c# .net


delete in c# .net

Poll
delete a row in c# .net

0% - 0 votes n/a
0% 0 votes
Member Votes: 0, Guest Votes: 0. You don't have permission to vote within this poll.
Author
Message
marcus416
marcus416
Forum God
Forum God (3.5K reputation)Forum God (3.5K reputation)Forum God (3.5K reputation)Forum God (3.5K reputation)Forum God (3.5K reputation)Forum God (3.5K reputation)Forum God (3.5K reputation)Forum God (3.5K reputation)Forum God (3.5K reputation)

Group: Forum Members
Posts: 48, Visits: 147
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.

(,'')marcus™
Joey
Joey
Forum God
Forum God (15K reputation)Forum God (15K reputation)Forum God (15K reputation)Forum God (15K reputation)Forum God (15K reputation)Forum God (15K reputation)Forum God (15K reputation)Forum God (15K reputation)Forum God (15K reputation)

Group: Forum Members
Posts: 72, Visits: 46
Why 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.)



AmrMohallel
AmrMohallel
Forum God
Forum God (3.3K reputation)Forum God (3.3K reputation)Forum God (3.3K reputation)Forum God (3.3K reputation)Forum God (3.3K reputation)Forum God (3.3K reputation)Forum God (3.3K reputation)Forum God (3.3K reputation)Forum God (3.3K reputation)

Group: Forum Members
Posts: 7, Visits: 5
As Joey posted, using OleDbcommand is the easiest way to delete/update/insert records.



Using Data adapters and DataTables is more helpful when you're processing data and displaying them afterwards.


Amr Mohallel

University of Sunderland

Faculty of Applied Sciences - Applied Business Computing
rostiger
rostiger
Forum God
Forum God (501 reputation)Forum God (501 reputation)Forum God (501 reputation)Forum God (501 reputation)Forum God (501 reputation)Forum God (501 reputation)Forum God (501 reputation)Forum God (501 reputation)Forum God (501 reputation)

Group: Forum Members
Posts: 1, Visits: 3
but what if I want to delete all rows from datatable with rowstate property value Deleted.

Is there any efficient way?
annaharris
annaharris
Forum God
Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)

Group: Forum Members
Posts: 20, Visits: 21
You are making it too complex, just use the DELETE query to delete the record that you want.
annaharris
annaharris
Forum God
Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)Forum God (9.6K reputation)

Group: Forum Members
Posts: 20, Visits: 21
did you solve the issue using the delete query instead of that complex code?
GO


Similar Topics


Reading This Topic


Login
Existing Account
Email Address:


Password:


Social Logins

Select a Forum....

















A1VBCode Forums


Search