Solved

SQL Stored proc running twice

Posted on 2014-02-27
6
367 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 12

Accepted Solution

by:
jitendra patil earned 500 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 12

Expert Comment

by:jitendra patil
ID: 39894250
Thanks SimonPrice33
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…

737 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