Hi All
I am developing an n-tier application using vb.net (vs2010) and have been able to access my data through a dataset.
Everything works very well until I have to submit some data to the database.
This is my function in the DataManager:
Public Function SaveReceipt(ByRef NewReceipt As ZHRISBLL.ExamsRegisterDataSet) As Boolean
Dim saved = False
If NewReceipt.HasChanges AndAlso Not NewReceipt.HasErrors Then
Dim Manager As New ZHRISDAL.ExamsRegisterDataSetTableAdapters.TableAdapterManager
Manager.InsertReceiptTableAdapter = New ZHRISDAL.ExamsRegisterDataSetTableAdapters.InsertReceiptTableAdapter
Manager.BackupDataSetBeforeUpdate = True
saved = (Manager.UpdateAll(NewReceipt) > 0)
End If
Return saved
End Function
The above code is supposed to handle saving data to the database table. Other functions are saving successfully
I want to save to a table with an IDENTITY column that generates the Primary Key for the entry, and then my table adapter has to extract the generated PK and send it back to the client. (I am using the SCOPE_IDENTITY SQL function and have been able to see the value returned in the Dataset.TableAdapter Preview Data facility in Data Source Design) However I do not see the new record inserted into the database, but if I insert a record into the database table manually the autogenerated PK is higher than the one returned in VS2010 Dataset Designer TableAdapter preview.
To try and insert the newly created data from the client side I have the following code handling the click event from the client form:
Private Sub btnSaveAndPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveAndPrint.Click
Using proxy As New ZHRISService.ServiceClient
Dim changes = CType(Me.ExamsRegisterDataSet.GetChanges, ZHRISBLL.ExamsRegisterDataSet)
If changes IsNot Nothing Then
If proxy.SaveReceipt(changes) Then
Dim addedrows = From row In Me.ExamsRegisterDataSet.Receipts _
Where row.RowState = DataRowState.Added
For Each row In addedrows.ToArray
row.Delete()
Next
Me.ExamsRegisterDataSet.Merge(changes)
Me.ExamsRegisterDataSet.AcceptChanges()
MsgBox("Receipt Generated")
Else
MsgBox("Receipt not Generated")
End If
End If
End Using
End Sub
I have the following code in the Service and IService respectively
IService:
<OperationContract()> _
Function SaveReceipt(ByRef NewReceipt As ExamsRegisterDataSet) As Boolean
Service:
Public Function SaveReceipt(ByRef NewReceipt As ZHRISBLL.ExamsRegisterDataSet) As Boolean Implements IService.SaveReceipt
Dim Manager As New ZHRISDataUpdateManager
Return (Manager.SaveReceipt(NewReceipt))
End Function
When I run my code I do not get any errors but the message "Receipt Not Generated".
Could I get help on the following in summary:
1. The Function that saves to the database and returns the transaction's generated Primary Key.
2. The correct Code for my client form's click event so that they will be able to use the newly generated PK from the database for further operations on their form.
Take note I am really new to vb.net but a fast learner, any help is appreciated even if it's criticism on my approach and code.
Thanks