Now my question is once I add the System and contact how do I know for sure which ContactID to add in my Sys_Con table? Because while I am doing this operation maybe someone can add an other contact. So is there a way for my store proc Add_Contact to return the contactID needed for my store proc Add_Sys_Con ?
TABLE Sys_Con
SystemID int
ContactID int
Table Contact
ContactID int Identity
ContactName
...
Table System
SystemID
...After your insert
SELECT @.@.IDENTITY AS 'Identity'
Here is the resource:@.@.IDENTITY
hope this helps,
sivilian|||Thanks for your reply,
how do I get the identity back from my insert sp of my contact? I dont seem to be able to make this work. I get the error: "Procedure 'Add_Cl_Contact' expects parameter '@.ContactId', which was not supplied." on the line "iContactID = cmdSelect.Parameters("ContactId").Value"
Here is what I have up to now:
|||I'd recommend using SCOPE_IDENTITY. check docs for more info about SCOPE_IDENTIY and @.@.IDENTITy..
ALTER procedure dbo.Add_Cl_Contact
(
@.ContactNamenvarchar(75),
@.Departmentnvarchar (50)=null,
@.Titlenvarchar(50)=null,
@.Phone1char(20)=null,
@.Phone2char(20)=null,
@.Phone3char(20)=null,
@.Ext1char(10)=null,
@.Ext2char(10)=null,
@.Ext3char(10)=null,
@.Faxnvarchar(50)=null,
@.Emailnvarchar(50)=null,
@.ContactIdintOUTPUT
)
AS
insert into Cl_Contacts (
ContactName,
Department,
Title,
Phone1,
Phone2,
Phone3,
Ext1,
Ext2,
Ext3,
Fax,
)
values (
@.ContactName,
@.Department,
@.Title,
@.Phone1,
@.Phone2,
@.Phone3,
@.Ext1,
@.Ext2,
@.Ext3,
@.Fax,
)
Select @.ContactId = @.@.IDENTITY'******************************************************************
'******************************************************************
Public Function Add_Cl_Contact(ByVal strContactName As String, _
ByVal strDep As String, ByVal strEmail As String, ByVal strExt1 As String, ByVal strExt2 As String, _
ByVal strExt3 As String, ByVal strFax As String, ByVal strPhone1 As String, ByVal strPhone2 As String, _
ByVal strPhone3 As String, ByVal strTitle As String, ByRef iContactID As Integer, ByRef strError As String) As Boolean
'******************************************************************
Dim bSuccess As Boolean = True
Dim connect As New SqlConnection(strConnection)
Dim cmdSelect As New SqlCommand("Add_Cl_Contact", connect)
Dim paramReturnValue As SqlParametercmdSelect.CommandType = CommandType.StoredProcedure
'PARAM
cmdSelect.Parameters.Add("@.ContactName", strContactName)
cmdSelect.Parameters.Add("@.Department", strDep)
cmdSelect.Parameters.Add("@.Title", strTitle)
cmdSelect.Parameters.Add("@.Phone1", strPhone1)
cmdSelect.Parameters.Add("@.Phone2", strPhone2)
cmdSelect.Parameters.Add("@.Phone3", strPhone3)
cmdSelect.Parameters.Add("@.Ext1", strExt1)
cmdSelect.Parameters.Add("@.Ext2", strExt2)
cmdSelect.Parameters.Add("@.Ext3", strExt3)
cmdSelect.Parameters.Add("@.Fax", strFax)
cmdSelect.Parameters.Add("@.Email", strEmail)paramReturnValue = cmdSelect.Parameters.Add("ContactId", SqlDbType.Int)
paramReturnValue.Direction = ParameterDirection.ReturnValueTry
connect.Open()
cmdSelect.ExecuteNonQuery()
iContactID = cmdSelect.Parameters("ContactId").Value
connect.Close()
Catch ex As Exception
bSuccess = False
strError = ex.Message
Finally
If connect.State = ConnectionState.Open Then
connect.Close()
End If
End Try
Return bSuccess
End Function
insert into Cl_Contacts (
Department,
Title,
Phone1,
Phone2,
Phone3,
Ext1,
Ext2,
Ext3,
Fax,
)
values (
@.Department,
@.Title,
@.Phone1,
@.Phone2,
@.Phone3,
@.Ext1,
@.Ext2,
@.Ext3,
@.Fax,
)SELECT @.contactid = SCOPE_IDENTITY()
hth
No comments:
Post a Comment