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.SelectComman d.Parameters.Ad dWithValue("@Or derID",
tbOrder.Text)
Dim tbl As New DataTable("Orde rs")
With tbl
.Columns.Add("O rderID", GetType(String) )
.PrimaryKey = New DataColumn() {.Columns("Orde rID")}
.Columns.Add("I tem", GetType(String) )
End With
da.Fill(tbl)
Dim rowToUpdate As DataRow
rowToUpdate = tbl.Rows.Find(t bOrder.Text)
strSQL = "UPDATE Orders " & _
"SET OrderID = @OrderID_New, " & _
"Item = @Item_New " & _
"WHERE OrderID = @OrderID_Old"
Dim cmdUpdate As New SqlCommand(strS QL, cn)
cmdUpdate.Param eters.AddWithVa lue("@OrderID_N ew",
rowToUpdate("Or derID"))
cmdUpdate.Param eters.AddWithVa lue("@Item_New" ,
rowToUpdate("It em"))
cmdUpdate.Param eters.AddWithVa lue("@OrderID_O ld",
rowToUpdate("Or derID", DataRowVersion. Original))
cmdUpdate.Param eters.AddWithVa lue("@Item_Old" ,
rowToUpdate("It em", DataRowVersion. Original))
Try
Dim intRecordsAffec ted As Integer
intRecordsAffec ted = cmdUpdate.Execu teNonQuery()
If intRecordsAffec ted = 1 Then
rowToUpdate.Acc eptChanges()
ElseIf intRecordsAffec ted = 0 Then
MessageBox.Show ("Update Failed - Query Affected No
Rows", "", MessageBoxButto ns.OK, MessageBoxIcon. Error)
Else : MessageBox.Show ("Query affected " &
intRecordsAffec ted & " rows?!?", "Error - Multiple Records Found",
MessageBoxButto ns.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.SelectComman d.Parameters.Ad dWithValue("@Or derID",
tbOrder.Text)
Dim tbl As New DataTable("Orde rs")
With tbl
.Columns.Add("O rderID", GetType(String) )
.PrimaryKey = New DataColumn() {.Columns("Orde rID")}
.Columns.Add("I tem", GetType(String) )
End With
da.Fill(tbl)
Dim rowToUpdate As DataRow
rowToUpdate = tbl.Rows.Find(t bOrder.Text)
strSQL = "UPDATE Orders " & _
"SET OrderID = @OrderID_New, " & _
"Item = @Item_New " & _
"WHERE OrderID = @OrderID_Old"
Dim cmdUpdate As New SqlCommand(strS QL, cn)
cmdUpdate.Param eters.AddWithVa lue("@OrderID_N ew",
rowToUpdate("Or derID"))
cmdUpdate.Param eters.AddWithVa lue("@Item_New" ,
rowToUpdate("It em"))
cmdUpdate.Param eters.AddWithVa lue("@OrderID_O ld",
rowToUpdate("Or derID", DataRowVersion. Original))
cmdUpdate.Param eters.AddWithVa lue("@Item_Old" ,
rowToUpdate("It em", DataRowVersion. Original))
Try
Dim intRecordsAffec ted As Integer
intRecordsAffec ted = cmdUpdate.Execu teNonQuery()
If intRecordsAffec ted = 1 Then
rowToUpdate.Acc eptChanges()
ElseIf intRecordsAffec ted = 0 Then
MessageBox.Show ("Update Failed - Query Affected No
Rows", "", MessageBoxButto ns.OK, MessageBoxIcon. Error)
Else : MessageBox.Show ("Query affected " &
intRecordsAffec ted & " rows?!?", "Error - Multiple Records Found",
MessageBoxButto ns.OK, MessageBoxIcon. Error)
End If
Catch ex As Exception
MessageBox.Show (ex.Message)
Finally
cn.Close()
End Try
End If
End Sub
Comment