N
nsikkandar
Greetings,
I am in need of getting "Auto Generated Number" of Primary Key from
master table (I am, using Access) when I insert a row through ADO.NET
from VB.NET. Based on this ID, I want to generate new rows in child
tables.
When I was looking on the net to get some Idea, I found there is a
feature SELECT @@IDENTITY to get "Auto Generated Number". But, I could
not find clear details about this feature.
I did the following code and executed, I am getting the result. But, I
am not sure, what I am doing is right. I would be thankful, if any one,
just go through my below code and let me know it is correct or it may
cause some error at some circumstances such as when multiple user
trying to execute the application and fire this method.
<-----Code Start Here ------->
Dim NewID As Integer
Public Shared Function ExecuteNonQuery(ByVal query As String) As
Boolean
Dim ConnectionString As String =
System.Configuration.ConfigurationSettings.AppSettings("DSN") '
Declared in App.Config File for Connection String
Dim myConnection As New OleDbConnection(ConnectionString)
Try
myConnection.Open()
' Argument for the query is ""insert into Candidate_Personal
(Name,FatherName,DateOfBirth,PlaceOfBirth,Address,City,Pincode,Phone,Mobile,Email)
values ('" & txtCandName.Text & "', '" & txtCandFName.Text & "', '" &
txtCandDoB.Text & "', '" & txtCandPoB.Text & "', '" &
txtCandAddress.Text & "', '" & txtCandCity.Text & "', '" &
txtCandPincode.Text & "', '" & txtCandPhone.Text & "', '" &
txtCandMobile.Text & "', '" & txtCandEmail.Text & "') "
'Note that the table Candidate_Personal contains Autogenerated Primary
Key called ID.
Dim myCommand As New OleDbCommand(query, myConnection)
Dim myCommand1 As New OleDbCommand("SELECT @@IDENTITY",
myConnection)
myCommand.ExecuteNonQuery()
myCommand1.ExecuteNonQuery()
NewID = myCommand1.ExecuteScalar
'MsgBox("The Candidate ID Generated by the system is: " &
NewID)
Return True
Catch ex As Exception
MsgBox("Experiencing Technical Problem! Error at
DataManager.ExecuteNonQuery" & vbNewLine & ex.Message)
Return False
Finally
If myConnection.State = ConnectionState.Open Then
myConnection.Close()
End If
End Try
End Function
<---- Code End Here ---->
Many Thanks in advance
Sikkandar
I am in need of getting "Auto Generated Number" of Primary Key from
master table (I am, using Access) when I insert a row through ADO.NET
from VB.NET. Based on this ID, I want to generate new rows in child
tables.
When I was looking on the net to get some Idea, I found there is a
feature SELECT @@IDENTITY to get "Auto Generated Number". But, I could
not find clear details about this feature.
I did the following code and executed, I am getting the result. But, I
am not sure, what I am doing is right. I would be thankful, if any one,
just go through my below code and let me know it is correct or it may
cause some error at some circumstances such as when multiple user
trying to execute the application and fire this method.
<-----Code Start Here ------->
Dim NewID As Integer
Public Shared Function ExecuteNonQuery(ByVal query As String) As
Boolean
Dim ConnectionString As String =
System.Configuration.ConfigurationSettings.AppSettings("DSN") '
Declared in App.Config File for Connection String
Dim myConnection As New OleDbConnection(ConnectionString)
Try
myConnection.Open()
' Argument for the query is ""insert into Candidate_Personal
(Name,FatherName,DateOfBirth,PlaceOfBirth,Address,City,Pincode,Phone,Mobile,Email)
values ('" & txtCandName.Text & "', '" & txtCandFName.Text & "', '" &
txtCandDoB.Text & "', '" & txtCandPoB.Text & "', '" &
txtCandAddress.Text & "', '" & txtCandCity.Text & "', '" &
txtCandPincode.Text & "', '" & txtCandPhone.Text & "', '" &
txtCandMobile.Text & "', '" & txtCandEmail.Text & "') "
'Note that the table Candidate_Personal contains Autogenerated Primary
Key called ID.
Dim myCommand As New OleDbCommand(query, myConnection)
Dim myCommand1 As New OleDbCommand("SELECT @@IDENTITY",
myConnection)
myCommand.ExecuteNonQuery()
myCommand1.ExecuteNonQuery()
NewID = myCommand1.ExecuteScalar
'MsgBox("The Candidate ID Generated by the system is: " &
NewID)
Return True
Catch ex As Exception
MsgBox("Experiencing Technical Problem! Error at
DataManager.ExecuteNonQuery" & vbNewLine & ex.Message)
Return False
Finally
If myConnection.State = ConnectionState.Open Then
myConnection.Close()
End If
End Try
End Function
<---- Code End Here ---->
Many Thanks in advance
Sikkandar