M
Materialised
Hi Folks,
Just a quickie, I am inserting some records (individually) into a SQL
Server Table. I am wondering how can I detect primary key violations on
this data which I am inserting into the table.
My Insert code is as follows (VB.Net):
Public Function InsertNewUser(ByVal _ConnString As String, ByVal
UserAccess As UserAccess.Access) As Integer
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
conn.ConnectionString = _ConnString
cmd.Connection = conn
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = "dbo.Access_Insert"
With cmd.Parameters
.AddWithValue("@Username", UserAccess.Username)
.AddWithValue("@RealName", UserAccess.Realname)
.AddWithValue("@accesslevel", 1)
.AddWithValue("@Email", UserAccess.Email)
.AddWithValue("@Webpage", UserAccess.Webpage)
.AddWithValue("@Sig", UserAccess.Sig)
.AddWithValue("Occupation", UserAccess.Occupation)
.AddWithValue("@Personal", UserAccess.Personal)
.AddWithValue("JoinDate", UserAccess.JoinDate)
.AddWithValue("@Password", UserAccess.Password)
End With
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Return -1
End Try
Return 0 ' success
End Function
As you can see this function returns 0 on success and -1 on failure. I
would like to be able to return some other integer value upon a primary
key conflict.
If anyone can help me out with this i'd be really gratful.
Kind Regards
Just a quickie, I am inserting some records (individually) into a SQL
Server Table. I am wondering how can I detect primary key violations on
this data which I am inserting into the table.
My Insert code is as follows (VB.Net):
Public Function InsertNewUser(ByVal _ConnString As String, ByVal
UserAccess As UserAccess.Access) As Integer
Dim conn As New SqlConnection
Dim cmd As New SqlCommand
conn.ConnectionString = _ConnString
cmd.Connection = conn
cmd.CommandType = Data.CommandType.StoredProcedure
cmd.CommandText = "dbo.Access_Insert"
With cmd.Parameters
.AddWithValue("@Username", UserAccess.Username)
.AddWithValue("@RealName", UserAccess.Realname)
.AddWithValue("@accesslevel", 1)
.AddWithValue("@Email", UserAccess.Email)
.AddWithValue("@Webpage", UserAccess.Webpage)
.AddWithValue("@Sig", UserAccess.Sig)
.AddWithValue("Occupation", UserAccess.Occupation)
.AddWithValue("@Personal", UserAccess.Personal)
.AddWithValue("JoinDate", UserAccess.JoinDate)
.AddWithValue("@Password", UserAccess.Password)
End With
Try
conn.Open()
cmd.ExecuteNonQuery()
Catch ex As Exception
Return -1
End Try
Return 0 ' success
End Function
As you can see this function returns 0 on success and -1 on failure. I
would like to be able to return some other integer value upon a primary
key conflict.
If anyone can help me out with this i'd be really gratful.
Kind Regards