ADO.Net-DataAdapter.DeleteCommand Can't Delete Row

I recently develop a tool to add, edit, delete, filter, search, locate record on a Database Table. Also includes Relations, my tool uses sqldataadapter, dataset.etc.

My problem is when I try to perform a Delete of a record look my code :

PrivateSub UpdateData()

Dim pAs ParameterControlUpdate

Dim TableToUpdateAsString = m_grdTableStyle.MappingName

Dim CmdAs SqlCommand

Try

Cmd =New SqlCommand

Cmd.CommandText = "UPDATE " & TableToUpdate & " SET "

ForEach pIn m_UpdateValues

IfNot p.KeyThen

Cmd.CommandText &= p.Name.Trim(Char.Parse("@")) & " = " & p.Name & ", "

EndIf

Next

Cmd.CommandText = Cmd.CommandText.Remove(Cmd.CommandText.Length - 2, 2)

Cmd.CommandText &= " WHERE "

ForEach pIn m_UpdateValues

If p.KeyThen

SelectCase p.DataType

CaseIs = enmDataType.typText, enmDataType.typDate

Cmd.CommandText &= p.Name.Trim(Char.Parse("@")) & "='" &CType(p.GetValue,String) & "' And "

CaseIs = enmDataType.typNumeric, enmDataType.typBoolean

Cmd.CommandText &= p.Name.Trim(Char.Parse("@")) & "=" &CType(p.GetValue,String) & " And "

EndSelect

EndIf

Next

Cmd.CommandText = Cmd.CommandText.Remove(Cmd.CommandText.Length - 5, 5)

ForEach pIn m_UpdateValues

Cmd.Parameters.Add(p.Name, p.GetValue)

Next

Cmd.Connection = m_DBCnn

daData.UpdateCommand = Cmd

daData.Update(dsData.Tables(TableToUpdate))

Cmd.Dispose()

Catch exAs Exception

ThrowNew Exception(ex.Message)

EndTry

EndSub

'This sub don't work as I need an don't raise an error.

PrivateSub DeleteData()

'This method can't work correctly, it don't throw any error and can't

'delete the record "I Need help with it" if some one can help me I will appreciate it a lot.

Dim pAs ParameterControlUpdate

Dim TableForDeleteAsString = m_grdTableStyle.MappingName

Dim CmdAs SqlCommand

Try

Cmd =New SqlCommand

Cmd.CommandText = "Delete " & TableForDelete & " "

Cmd.CommandText &= "WHERE "

ForEach pIn m_UpdateValues

If p.KeyThen

Cmd.CommandText &= p.Name.Trim(Char.Parse("@")) & " = " & p.Name & " And "

EndIf

Next

Cmd.CommandText = Cmd.CommandText.Remove(Cmd.CommandText.Length - 5, 5)

ForEach pIn m_UpdateValues

If p.KeyThen

Cmd.Parameters.Add(p.Name, p.GetValue)

EndIf

Next

Cmd.Connection = m_DBCnn

daData.DeleteCommand = Cmd

daData.Update(dsData.Tables(TableForDelete))

Cmd.Dispose()

Catch exAs Exception

ThrowNew Exception(ex.Message)

EndTry

EndSub

The DeleteData Sub don't raise any error, the commandtext used is correct, I tested directly in SQL Query Analizer, the Parameters of SqlCommand object are ok.

If you can help me, I can send you my entire project if you need it.

(I have a sample project which uses Northwind database)

Regards

Carlos Vara

[28267 byte] By [CarlosVara] at [2007-12-16]
# 1
Hi,
Please check if you call AcceptChanges() method in your datatable. Coz if you call the method before the update process occurs, then no changes would be made in your database...
cheers,
Paul June A. Domag
PaulDomag at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 2
Carlos,

I don't see any errors in your logic. You could try adding code to handle the DataAdapter's RowUpdating and RowUpdated events to see if the DataAdapter attempts to submit the expected changes. Also, using a simplified query (one key column, one non-key column) may also help.

I hope this information proves helpful.

David Sceppa
ADO.NET Program Manager
Microsoft

DavidSceppa at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 3
I coded the events you told me, and the message I saw is "1 row affected" when
I use Add New, Edit And Save, Delete; but "Delete" don't phisically Deletes the row on DataBase. I'm really tired of try one thing and another without success.

Can I send you a little sample project to you? may be you can figure out this behavior I hope this will not be a Microsoft's Error.

CarlosVara at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 4
Carlos,

One thing to try is to take a look at SQL Profiler and see if your Delete command is even being sent to the database.

BonnieB at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 5

Well said, Connie.

Carlos, if you're not seeing the RowUpdating event fire for the pending deletions and/or you're not seeing the DELETE queries in the SQL Profiler trace, this might be an issue with how you're deleting the rows.

How are you deleting the rows? Make sure you're not calling Remove which physically removes the DataRow from the Rows collection. You should be calling Delete so the row is marked as Deleted but is still available in the Rows collection so the DataAdapter can submit the pending change to the database.

David Sceppa
ADO.NET Program Manager
Microsoft

DavidSceppa at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 6
I am having a similar issue, and I ran the profiler. I see no delete queries nor any row updating event in the results. Additionally my codebehind is not throwing an exception, so I am stuck as to what I should do next.
ShawnK. at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 7


How are you deleting the rows? Are you calling AcceptChanges prior to calling Update?

David Sceppa
ADO.NET Program Manager
Microsoft

DavidSceppa at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 8
I am deleting a row in the following manor:
DataSet.Tables[0].Rows[Counter].Delete();
Here is how I am handeling the update. I am passing a dataset to a function, then updating. I do not call AcceptChanges, but I also do not see it as an option through intelesense.
SqlConnection SqlConn = new SqlConnection(connString);
SqlConn.Open();
SqlDataAdapter myDataAdapter = new SqlDataAdapter("SELECT * FROM " + updatedTable, SqlConn);
SqlCommandBuilder myCommandBuilder = new SqlCommandBuilder(myDataAdapter);
myDataAdapter.Update(SavedDataSet, updatedTable);
SqlConn.Close();
** Edit **
I was looking for AcceptChanges off the DataAdapter and not the DataSet, so I added that to my code right before I call the function that does the db connection and update. However, it still does not delete rows.
ShawnK. at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 9
Shawn,

When David was asking about the AcceptChanges() it's because he wanted to make sure that you were NOT using it (then it wouldn't work at all!!)

Does your code work for Updates or Inserts ok? Is it only the Deletes that aren't working?

BonnieB at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 10
I'll be sure to remove that then. I am not doing any updates, but inserts were working just fine. Just deletes that don't happen.
ShawnK. at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 11


(Thanks for clarifying my mention of AcceptChanges, Bonnie.)

You're deleting the rows correctly by calling Delete and your overall approach appears sound. Try following your current approach in a new application using basic sample tables (like Northwind.Customers or pubs.authors).

I'm assuming that will work. Let us know either way. If it doesn't work, post enough code for someone to reproduce the behavior. That will make it easier for someone to help provide a solution.

David Sceppa
ADO.NET Program Manager
Microsoft

DavidSceppa at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 12
Thanks, I will try using some sample tables to see if it has the same effect. Are there any settings in sql server I can check that might prevent this from working properly such as permissions, or stored procedures? The person who set these tables up is no longer with us, and am curious if something he setup could possibly be causing this issue. Thanks!!
ShawnK. at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 13
Shawn,

Since you said that you ran the Profiler and saw no delete commands being passed to SQL Server, then it's not anything in the SQL Server database itself that could be causing the problems.

BonnieB at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...
# 14
I had no luck with this. I don't have northwind laying around, so I created a new table from scratch and used that. I got the same result. Rows would added fine but would not removed. I zipped a trace file, along with sample code (hopefully it is enough) and placed it at http://www.tyfu.com/samplecode.zip. I am going to try using an xml file next as my datastore just to see if I get the same results and I am getting with the sql database.
ShawnK. at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Data Access and Storage...

.NET Development

Site Classified