I have an n-tiered app developed using VB6 that I am trying to convert to .Net. I am passing data between the tiers using datasets, and can populate screens on the user interface without any problems.
The question I have is how do I update the database using a dataset, have read that I need to use a the dataadapter.update method but cant seem to figure it out.
Heres an example of my code:
VB6
----
Public Function Add_Item(rsNewItemDetails As ADODB.Recordset) As Boolean
On Error GoTo BadInsert
cnMRP.Open
cnMRP.CursorLocation = adUseServer
strSQL = "SELECT * FROM tblItemMaster WHERE 1=2"
rsItemDetails.LockType = adLockOptimistic
rsItemDetails.CursorType = adOpenKeyset
rsItemDetails.Open strSQL, cnMRP, , , adCmdText
With rsItemDetails
.AddNew
!ItemNo = rsNewItemDetails!ItemNo
!ItemDescShort = rsNewItemDetails!ItemDescShort
!ItemDescLong = rsNewItemDetails!ItemDescLong
!CatID = rsNewItemDetails!CatID
!UMID = rsNewItemDetails!UMID
!SafetyStock = rsNewItemDetails!SafetyStock
!MinOrderQty = rsNewItemDetails!MinOrderQty
!MaxOrderQty = rsNewItemDetails!MaxOrderQty
!VendorID = rsNewItemDetails!VendorID
!VendorItemNumber = rsNewItemDetails!VendorItemNumber
!LeadTime = rsNewItemDetails!LeadTime
!CostPurchase = rsNewItemDetails!CostPurchase
!ClassID = rsNewItemDetails!ClassID
!CostStandard = rsNewItemDetails!CostStandard
!CostEstimated = rsNewItemDetails!CostEstimated
!CostAverage = rsNewItemDetails!CostAverage
!CostLast = rsNewItemDetails!CostLast
!InventoryValue = rsNewItemDetails!InventoryValue
!EngLevel = rsNewItemDetails!EngLevel
!DrawingNumber = rsNewItemDetails!DrawingNumber
!DrawingLocation = rsNewItemDetails!DrawingLocation
!TypeID = rsNewItemDetails!TypeID
.Update
End With
Add_Item = True
cnMRP.Close
Exit Function
BadInsert:
Select Case Err.Number
Case -2147217887
Err.Raise Number:=vbObjectError + 512 + 1, _
Description:="Duplicate Values Exist for this Item"
Err.Clear
Case Else
Err.Raise Err.Number & Err.Description
Err.Clear
End Select
Add_Item = False
cnMRP.Close
End Function
.Net
-----
Public Function Add_Item(ByVal ItemDetails As DataSet) As Boolean
Dim mySelectCommand As OleDbCommand = New OleDbCommand(strSQL, oConnection)
Dim myOleDbDataAdapter As OleDbDataAdapter = New OleDbDataAdapter(mySelectCommand)
myOleDbDataAdapter.Update(dsReturn)
Add_Item = True
End Function
The question I have is how do I update the database using a dataset, have read that I need to use a the dataadapter.update method but cant seem to figure it out.
Heres an example of my code:
VB6
----
Public Function Add_Item(rsNewItemDetails As ADODB.Recordset) As Boolean
On Error GoTo BadInsert
cnMRP.Open
cnMRP.CursorLocation = adUseServer
strSQL = "SELECT * FROM tblItemMaster WHERE 1=2"
rsItemDetails.LockType = adLockOptimistic
rsItemDetails.CursorType = adOpenKeyset
rsItemDetails.Open strSQL, cnMRP, , , adCmdText
With rsItemDetails
.AddNew
!ItemNo = rsNewItemDetails!ItemNo
!ItemDescShort = rsNewItemDetails!ItemDescShort
!ItemDescLong = rsNewItemDetails!ItemDescLong
!CatID = rsNewItemDetails!CatID
!UMID = rsNewItemDetails!UMID
!SafetyStock = rsNewItemDetails!SafetyStock
!MinOrderQty = rsNewItemDetails!MinOrderQty
!MaxOrderQty = rsNewItemDetails!MaxOrderQty
!VendorID = rsNewItemDetails!VendorID
!VendorItemNumber = rsNewItemDetails!VendorItemNumber
!LeadTime = rsNewItemDetails!LeadTime
!CostPurchase = rsNewItemDetails!CostPurchase
!ClassID = rsNewItemDetails!ClassID
!CostStandard = rsNewItemDetails!CostStandard
!CostEstimated = rsNewItemDetails!CostEstimated
!CostAverage = rsNewItemDetails!CostAverage
!CostLast = rsNewItemDetails!CostLast
!InventoryValue = rsNewItemDetails!InventoryValue
!EngLevel = rsNewItemDetails!EngLevel
!DrawingNumber = rsNewItemDetails!DrawingNumber
!DrawingLocation = rsNewItemDetails!DrawingLocation
!TypeID = rsNewItemDetails!TypeID
.Update
End With
Add_Item = True
cnMRP.Close
Exit Function
BadInsert:
Select Case Err.Number
Case -2147217887
Err.Raise Number:=vbObjectError + 512 + 1, _
Description:="Duplicate Values Exist for this Item"
Err.Clear
Case Else
Err.Raise Err.Number & Err.Description
Err.Clear
End Select
Add_Item = False
cnMRP.Close
End Function
.Net
-----
Public Function Add_Item(ByVal ItemDetails As DataSet) As Boolean
Dim mySelectCommand As OleDbCommand = New OleDbCommand(strSQL, oConnection)
Dim myOleDbDataAdapter As OleDbDataAdapter = New OleDbDataAdapter(mySelectCommand)
myOleDbDataAdapter.Update(dsReturn)
Add_Item = True
End Function