A
Alex
I've got a procedure designed to modify the contents of a single row
in a data table. The code appears to work fine in that it compiles
and executes without error and the changes are reflected in the
dataset. However, when I close and re-open the app, the changes are
lost, which means that they are not reaching the datasource. Can
anybody see why this is happening? I've searched all over and I think
that this should work - but it obviously doesn't (and I'm kind of a
moron). Any help is appreciated.
Thanks.
Dim strSQL As String
strSQL = "SELECT * FROM Orders WHERE OrderID = @OrderID"
cn.Open()
Dim da As New SqlDataAdapter(strSQL, cn)
da.SelectCommand.Parameters.AddWithValue("@OrderID",
tbOrder.Text)
Dim tbl As New DataTable("Orders")
With tbl
.Columns.Add("OrderID", GetType(String))
.PrimaryKey = New DataColumn() {.Columns("OrderID")}
.Columns.Add("Item", GetType(String))
End With
da.Fill(tbl)
Dim rowToUpdate As DataRow
rowToUpdate = tbl.Rows.Find(tbOrder.Text)
strSQL = "UPDATE Orders " & _
"SET OrderID = @OrderID_New, " & _
"Item = @Item_New " & _
"WHERE OrderID = @OrderID_Old"
Dim cmdUpdate As New SqlCommand(strSQL, cn)
cmdUpdate.Parameters.AddWithValue("@OrderID_New",
rowToUpdate("OrderID"))
cmdUpdate.Parameters.AddWithValue("@Item_New",
rowToUpdate("Item"))
cmdUpdate.Parameters.AddWithValue("@OrderID_Old",
rowToUpdate("OrderID", DataRowVersion.Original))
cmdUpdate.Parameters.AddWithValue("@Item_Old",
rowToUpdate("Item", DataRowVersion.Original))
Try
Dim intRecordsAffected As Integer
intRecordsAffected = cmdUpdate.ExecuteNonQuery()
If intRecordsAffected = 1 Then
rowToUpdate.AcceptChanges()
ElseIf intRecordsAffected = 0 Then
MessageBox.Show("Update Failed - Query Affected No
Rows", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else : MessageBox.Show("Query affected " &
intRecordsAffected & " rows?!?", "Error - Multiple Records Found",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End If
End Sub
in a data table. The code appears to work fine in that it compiles
and executes without error and the changes are reflected in the
dataset. However, when I close and re-open the app, the changes are
lost, which means that they are not reaching the datasource. Can
anybody see why this is happening? I've searched all over and I think
that this should work - but it obviously doesn't (and I'm kind of a
moron). Any help is appreciated.
Thanks.
Dim strSQL As String
strSQL = "SELECT * FROM Orders WHERE OrderID = @OrderID"
cn.Open()
Dim da As New SqlDataAdapter(strSQL, cn)
da.SelectCommand.Parameters.AddWithValue("@OrderID",
tbOrder.Text)
Dim tbl As New DataTable("Orders")
With tbl
.Columns.Add("OrderID", GetType(String))
.PrimaryKey = New DataColumn() {.Columns("OrderID")}
.Columns.Add("Item", GetType(String))
End With
da.Fill(tbl)
Dim rowToUpdate As DataRow
rowToUpdate = tbl.Rows.Find(tbOrder.Text)
strSQL = "UPDATE Orders " & _
"SET OrderID = @OrderID_New, " & _
"Item = @Item_New " & _
"WHERE OrderID = @OrderID_Old"
Dim cmdUpdate As New SqlCommand(strSQL, cn)
cmdUpdate.Parameters.AddWithValue("@OrderID_New",
rowToUpdate("OrderID"))
cmdUpdate.Parameters.AddWithValue("@Item_New",
rowToUpdate("Item"))
cmdUpdate.Parameters.AddWithValue("@OrderID_Old",
rowToUpdate("OrderID", DataRowVersion.Original))
cmdUpdate.Parameters.AddWithValue("@Item_Old",
rowToUpdate("Item", DataRowVersion.Original))
Try
Dim intRecordsAffected As Integer
intRecordsAffected = cmdUpdate.ExecuteNonQuery()
If intRecordsAffected = 1 Then
rowToUpdate.AcceptChanges()
ElseIf intRecordsAffected = 0 Then
MessageBox.Show("Update Failed - Query Affected No
Rows", "", MessageBoxButtons.OK, MessageBoxIcon.Error)
Else : MessageBox.Show("Query affected " &
intRecordsAffected & " rows?!?", "Error - Multiple Records Found",
MessageBoxButtons.OK, MessageBoxIcon.Error)
End If
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
cn.Close()
End Try
End If
End Sub