G
Guest
Below is the code I have in the Page_Load event of my web application/page.
However, the records I select are not being deleted for some reason. It does
go through the code(look at: 'find the specified row and delete it' section)
and the msg label does say it was deleted. When I look at the rows in the
DataBase the record is still there.
Why aren't my records being deleted?
lblResults.Text = ""
If IsPostBack Then
'create a connection
Dim cnn As SqlConnection = New SqlConnection("Data
Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI")
Dim ds As DataSet = New DataSet
Dim da As SqlDataAdapter = New SqlDataAdapter
'create a sql command to select data
Dim cmdSelect As SqlCommand = cnn.CreateCommand()
cmdSelect.CommandType = CommandType.Text
cmdSelect.CommandText = "Select CustomerID, ContactName From
Customers"
'create a sql command to delete data
Dim cmdDelete As SqlCommand = cnn.CreateCommand
cmdDelete.CommandType = CommandType.Text
cmdDelete.CommandText = "Delete From Customers Where CustomerID
= @CustomerID"
cmdDelete.Parameters.Add("@CustomerID", SqlDbType.NChar, 5,
"CustomerID")
cmdDelete.Parameters("@CustomerID").SourceVersion =
DataRowVersion.Original
'set up a sqldataadapter and fill the dataset
da.SelectCommand = cmdSelect
da.DeleteCommand = cmdDelete
da.Fill(ds, "Customers")
'find the specified row and delete it
Dim dr As DataRow
For Each dr In ds.Tables("Customers").Rows
If dr(0) = txtCustomerID.Text Then
ds.Tables("Customers").Rows.Remove(dr)
lblResults.Text = "Row Deleted!"
Exit For
End If
Next
'save the change
da.Update(ds, "Customers")
However, the records I select are not being deleted for some reason. It does
go through the code(look at: 'find the specified row and delete it' section)
and the msg label does say it was deleted. When I look at the rows in the
DataBase the record is still there.
Why aren't my records being deleted?
lblResults.Text = ""
If IsPostBack Then
'create a connection
Dim cnn As SqlConnection = New SqlConnection("Data
Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI")
Dim ds As DataSet = New DataSet
Dim da As SqlDataAdapter = New SqlDataAdapter
'create a sql command to select data
Dim cmdSelect As SqlCommand = cnn.CreateCommand()
cmdSelect.CommandType = CommandType.Text
cmdSelect.CommandText = "Select CustomerID, ContactName From
Customers"
'create a sql command to delete data
Dim cmdDelete As SqlCommand = cnn.CreateCommand
cmdDelete.CommandType = CommandType.Text
cmdDelete.CommandText = "Delete From Customers Where CustomerID
= @CustomerID"
cmdDelete.Parameters.Add("@CustomerID", SqlDbType.NChar, 5,
"CustomerID")
cmdDelete.Parameters("@CustomerID").SourceVersion =
DataRowVersion.Original
'set up a sqldataadapter and fill the dataset
da.SelectCommand = cmdSelect
da.DeleteCommand = cmdDelete
da.Fill(ds, "Customers")
'find the specified row and delete it
Dim dr As DataRow
For Each dr In ds.Tables("Customers").Rows
If dr(0) = txtCustomerID.Text Then
ds.Tables("Customers").Rows.Remove(dr)
lblResults.Text = "Row Deleted!"
Exit For
End If
Next
'save the change
da.Update(ds, "Customers")