Deleting rows with C#

Joined
Oct 26, 2011
Messages
2
Reaction score
0
Hi, I have problems deleting rows from a database table. I have read on the internet, and adjusted the code but apparently I am missing something, any help greatly appreciated. Below is the vital code that is used:


myCon.Open();
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add(
"tbl6_MONK_LoggedIn");
SqlCommand myCommand = new SqlCommand();
myCommand.CommandType =
CommandType.Text;
myCommand.CommandText =
"SELECT * FROM tbl6_MONK_LoggedIn";

myCommand.Connection = myCon;
myCommand.CommandTimeout = 180;
SqlDataAdapter myAdapter = new SqlDataAdapter(myCommand);
SqlCommandBuilder cb = new SqlCommandBuilder(myAdapter);


myAdapter.Fill(myDataSet, "tbl6_MONK_LoggedIn");


for ( int i = 0; i < myDataSet.Tables["tbl6_MONK_LoggedIn"].Rows.Count; i++)
{
DataRow myRow = myDataSet.Tables["tbl6_MONK_LoggedIn"].Rows;
myRow.Delete();
myRow.AcceptChanges();

myDataSet.Tables["tbl6_MONK_LoggedIn"].AcceptChanges();
}


myAdapter.Update(myDataSet, "tbl6_MONK_LoggedIn");
myCon.Close();

 
Joined
Oct 26, 2011
Messages
2
Reaction score
0
Thanks för all the replys.

Here is the code that fixed it:

myCon.Open();
string sql = "";
sql =
"DELETE FROM tbl6_MONK_LoggedIn";
SqlDataAdapter myAdapter = new SqlDataAdapter();
myAdapter.DeleteCommand = myCon.CreateCommand();
myAdapter.DeleteCommand.CommandText = sql;
myAdapter.DeleteCommand.ExecuteNonQuery();
myCon.Close();
 

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