deleting sqlserver table data with delete command using DataAdapter in c#
-
Hi, I am trying to insert/update/delete a field from sql server table using dataadapter in c# windows application. Here insert and update are happening,but 'delete' is not working I will show my delete part : public int UpdateAllUserDetails(DataSet dsUsers) { private SqlConnection connection; private SqlCommand command; private SqlDataAdapter dataAdapter; connection = new SqlConnection(ConfigurationManager.AppSettings.Get("conString")); connection.Open(); dataAdapter = new SqlDataAdapter(command); dataAdapter.DeleteCommand = new SqlCommand("DELETE FROM UserDetails WHERE vUserName=@vUserName", connection); dataAdapter.DeleteCommand.Parameters.Add("@vUserName", SqlDbType.VarChar, 50, "vUserName"); int ret = dataAdapter.Update(dsUsers); return ret; } can somebody say what is missing here?
-
Hi, I am trying to insert/update/delete a field from sql server table using dataadapter in c# windows application. Here insert and update are happening,but 'delete' is not working I will show my delete part : public int UpdateAllUserDetails(DataSet dsUsers) { private SqlConnection connection; private SqlCommand command; private SqlDataAdapter dataAdapter; connection = new SqlConnection(ConfigurationManager.AppSettings.Get("conString")); connection.Open(); dataAdapter = new SqlDataAdapter(command); dataAdapter.DeleteCommand = new SqlCommand("DELETE FROM UserDetails WHERE vUserName=@vUserName", connection); dataAdapter.DeleteCommand.Parameters.Add("@vUserName", SqlDbType.VarChar, 50, "vUserName"); int ret = dataAdapter.Update(dsUsers); return ret; } can somebody say what is missing here?
-
Try and run the same sql statement in your DBMS as it will tell you what if anything is wrong with your statement. My guess is that you may have some dependencies in your tables, that prevent you from deleting.
Thanks kstls, But I have no dependencies for this because i am trying to delete some newly inserted userdetails data, which will create a new field in that single table only and not in any of the dependency tables. Here by debugging the code i am getting the update command as : "DELETE FROM UserDetails WHERE vUserName=@vUserName" which when run directly in DBMS just replacing vUserName as 'testuser' which is the primarykey vUserName : [ DELETE FROM UserDetails WHERE vUserName='testuser' ] is deleting properly But not working from code
modified on Tuesday, November 17, 2009 6:48 AM