purplehaze1
Programmer
I looked into many examples, I can't retrieve identity key after inserting a record. The identity value is returned as dbnull. Please help how I can
get identity key. Here's my code below. Thanks.
Public Function SaveGuardian() As Boolean
Dim strSQL As String
strSQL = "SELECT * FROM t_recip_guardian WHERE guardian_id=0"
Dim cn As New OleDbConnection(Login.SQL_ConnectionString)
Dim da As New OleDbDataAdapter(strSQL, cn)
Dim cb As New OleDbCommandBuilder(da)
AddHandler da.RowUpdated, AddressOf HandleRowUpdated
Dim ds As New DataSet()
da.Fill(ds, "Guardian")
Dim dr As DataRow
If ds.Tables(0).Rows.Count = 0 Then
dr = ds.Tables(0).NewRow()
IsNew = True
GoTo SaveRecord
Else
dr = ds.Tables(0).Rows(0)
End If
SaveRecord:
dr("recip_id") = 1
dr("first_nme") = IIf(m_gfname = String.Empty, String.Empty, m_gfname)
dr("last_nme") = IIf(m_glname = String.Empty, String.Empty, m_glname)
dr("create_dte") = CType(Today.Now, Date)
dr("lst_updt_dte") = CType(Today.Now, Date)
dr("lst_updt_id") = IIf(Login.m_UserName = String.Empty, String.Empty, Login.m_UserName)
If IsNew Then
ds.Tables("Guardian").Rows.Add(dr)
End If
da.Update(ds, "Guardian")
Skip_Save:
IsDirty = False
IsNew = False
Return True
End Function
Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As OleDbRowUpdatedEventArgs)
Dim cmdGetIdentity As New OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)
If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
e.Row("guardian_id") = cmdGetIdentity.ExecuteScalar()
e.Row.AcceptChanges()
End If
End Sub
get identity key. Here's my code below. Thanks.
Public Function SaveGuardian() As Boolean
Dim strSQL As String
strSQL = "SELECT * FROM t_recip_guardian WHERE guardian_id=0"
Dim cn As New OleDbConnection(Login.SQL_ConnectionString)
Dim da As New OleDbDataAdapter(strSQL, cn)
Dim cb As New OleDbCommandBuilder(da)
AddHandler da.RowUpdated, AddressOf HandleRowUpdated
Dim ds As New DataSet()
da.Fill(ds, "Guardian")
Dim dr As DataRow
If ds.Tables(0).Rows.Count = 0 Then
dr = ds.Tables(0).NewRow()
IsNew = True
GoTo SaveRecord
Else
dr = ds.Tables(0).Rows(0)
End If
SaveRecord:
dr("recip_id") = 1
dr("first_nme") = IIf(m_gfname = String.Empty, String.Empty, m_gfname)
dr("last_nme") = IIf(m_glname = String.Empty, String.Empty, m_glname)
dr("create_dte") = CType(Today.Now, Date)
dr("lst_updt_dte") = CType(Today.Now, Date)
dr("lst_updt_id") = IIf(Login.m_UserName = String.Empty, String.Empty, Login.m_UserName)
If IsNew Then
ds.Tables("Guardian").Rows.Add(dr)
End If
da.Update(ds, "Guardian")
Skip_Save:
IsDirty = False
IsNew = False
Return True
End Function
Private Sub HandleRowUpdated(ByVal sender As Object, ByVal e As OleDbRowUpdatedEventArgs)
Dim cmdGetIdentity As New OleDbCommand("SELECT @@IDENTITY", e.Command.Connection)
If e.Status = UpdateStatus.Continue AndAlso _
e.StatementType = StatementType.Insert Then
e.Row("guardian_id") = cmdGetIdentity.ExecuteScalar()
e.Row.AcceptChanges()
End If
End Sub