Wednesday, March 7, 2012

Returning the Primary Key

Is there a way to get asp.net to return the primary key in SQLServer after INSERT as I need to re-name an uploaded file with the primary key. Thanks.Do mean the IDENTITY cause otherwise you must have inserted the primary key yourself!?|||Yeah I mean the Identity.|||Hi nova,

if you are using a stored procedure to insert then

Function()
Delcare con,
Delcare command,adapters etc....
'declare a param
Dim parameter1 As New SqlParameter("@.ID", SqlDbType.Int)
parameter1.Direction = ParameterDirection.Output

'after the executescalar

Dim iID As Integer = yourcommand.Parameters("@.ID").Value.ToString()
Return iID
End Function

Hope this helps?
m_7e7|||Multiple ways to get Identity value from SQL:

System variable @.@.Identity, this is database scope

Function SCOPE_IDENTITY(), Or IDENT_CURRENT('Table name')|||I might have to do it the srored procedure way, however at the moment i'm just using a function that executes the sql variable when it's called e.g


Dim queryString As String = "INSERT INTO [tblKnowledgeBase] ([knowledgeTitle], [knowledgeType], [knowledgeCust"& _
"omerView], [knowledgeProjectID], [knowledgeDesc], [knowledgeAttachName], [knowle"& _
"dgeAttachType], [knowledgeAttachSize], [knowledgeAdditionUserID], [knowledgeAddi"& _
"tionDate], [KnowledgeApproved]) VALUES (@.knowledgeTitle, @.knowledgeType, @.knowle"& _
"dgeCustomerView, @.knowledgeProjectID, @.knowledgeDesc, @.knowledgeAttachName, @.kno"& _
"wledgeAttachType, @.knowledgeAttachSize, @.knowledgeAdditionUserID, @.knowledgeAddi"& _
"tionDate, @.KnowledgeApproved)"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection

Dim rowsAffected As Integer = 0
dbConnection.Open
Try
rowsAffected = dbCommand.ExecuteNonQuery
Finally
dbConnection.Close
End Try

Return rowsAffected

Is there a way I can retrieve it with this method of insertion, If I use stored procedures I will have compatibility problems when creating a microsoft Access/mySQL version.|||Is your ID(Primary Key) a AutoNumber ??|||My advice would be to bite the bullet and use a stored proc.|||Try this query instead.............

Dim queryString As String = "INSERT INTO [tblKnowledgeBase] ([knowledgeTitle], [knowledgeType], [knowledgeCust"& _

"omerView], [knowledgeProjectID], [knowledgeDesc], [knowledgeAttachName], [knowle"& _

"dgeAttachType], [knowledgeAttachSize], [knowledgeAdditionUserID], [knowledgeAddi"& _

"tionDate], [KnowledgeApproved]) VALUES (@.knowledgeTitle, @.knowledgeType, @.knowle"& _

"dgeCustomerView, @.knowledgeProjectID, @.knowledgeDesc, @.knowledgeAttachName, @.kno"& _

"wledgeAttachType, @.knowledgeAttachSize, @.knowledgeAdditionUserID, @.knowledgeAddi"& _

"tionDate, @.KnowledgeApproved)";"SELECT @.@.IDENTITY AS Ident";

Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand

dbCommand.CommandText = queryString

dbCommand.Connection = dbConnection

Dim rowsAffected As Integer = 0

dbConnection.Open

Try

rowsAffected = dbCommand.Executescalar

Finally

dbConnection.Close

End Try

Return rowsAffected

*********Note*************
Use an executescalar instead of execnonquery and try.....|||Yeah it is an utonumber|||hey you got it???

No comments:

Post a Comment