Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL Stored proc running twice

Posted on 2014-02-27
6
Medium Priority
?
392 Views
Last Modified: 2014-02-28
Hi Experts,

I am building an applicationt that I need to insert data and then return the ID of the inserted record.

an examples of my code looks similar to this..

dim param as new sqlparameter("@risk", sqldbtype)
(there a number of parameters)

sqlconn.connection= connstr
sqlconn.open

sqlcmd.executenonquery

riskID = cint(sqlcmd.executescalar)

sqlconn.close

It is inserting the data, and returning the ID value...

however, its inserting it twice and returning the ID once... any suggestions?
0
Comment
Question by:SimonPrice33
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 13

Accepted Solution

by:
Jitendra Patil earned 2000 total points
ID: 39891803
the problem is with your below code

sqlcmd.executenonquery

riskID = cint(sqlcmd.executescalar)

you are running the same command query two differnt ways i.e. executenonquery and executescalar

Remove the sqlcmd.executenonquery statement.

hope this helps.
0
 

Author Comment

by:SimonPrice33
ID: 39891815
i will try this later this afternoon and get back to you thank you.

I thought it would be something like this, but was under the assumption the execuscalar would only return the @@identity data.

thanks

Simon
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39891821
both commands will actually RUN the command.
the difference is that the executenonquery will discard any results.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 40
ID: 39892126
If the command is a stored procedure that finish with SELECT @@IDENTITY, simply remove the ExecuteNonQuery. ExecuteScalar alone will do the insert and retrieve @@IDENTITY.

If the command is a SQL string, then create a second command object with "SELECT @@IDENTITY" and run in separately after the ExecuteNonQuery.
0
 

Author Closing Comment

by:SimonPrice33
ID: 39892369
Although each of you gave the answer that solved my issue, this was the first to give me the answer.

many thanks to you all
0
 
LVL 13

Expert Comment

by:Jitendra Patil
ID: 39894250
Thanks SimonPrice33
0

Featured Post

Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
In the first part of this tutorial we will cover the prerequisites for installing SQL Server vNext on Linux.
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

609 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question