Solved

SQL Stored proc running twice

Posted on 2014-02-27
6
348 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
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 142

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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now