473,378 Members | 1,377 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

why is this code executed twice?

Bob
Hi, (already posted but unsolved)

this code inserts twice the same record. I thin it is due to the "Selet
Scope_Identity" in the sqlcommand.
If i remove the Select part, the inserts occurs only once. If i remove the
line "comd.ExecuteNonQuery()", then the inserts also occurs once.

Is there something wrong in my code?

Thanks
Bob

Dim connection As SqlConnection
Dim comd As SqlCommand
Dim connectionstr, sql As String
Dim iden As Integer
connectionstr =
ConfigurationManager.ConnectionStrings("econn").Co nnectionString.ToString()
connection = New SqlConnection(connectionstr)
comd = New SqlCommand()
comd.Connection = connection
sql = "INSERT INTO table(field,...) VALUES (@fld,...); SELECT
SCOPE_IDENTITY()"
comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).Value =
txtvnm.Text
...
connection.Open()
iden = Convert.ToInt32(comd.ExecuteScalar())
comd.ExecuteNonQuery()
connection.Close()
Feb 22 '07 #1
2 1586
And already solved.

The line you suspect is the one that is doing the 2nd insert.
"Bob" <bo*@omail.nlwrote in message
news:ek*************@TK2MSFTNGP05.phx.gbl...
Hi, (already posted but unsolved)

this code inserts twice the same record. I thin it is due to the "Selet
Scope_Identity" in the sqlcommand.
If i remove the Select part, the inserts occurs only once. If i remove the
line "comd.ExecuteNonQuery()", then the inserts also occurs once.

Is there something wrong in my code?

Thanks
Bob

Dim connection As SqlConnection
Dim comd As SqlCommand
Dim connectionstr, sql As String
Dim iden As Integer
connectionstr =
ConfigurationManager.ConnectionStrings("econn").Co nnectionString.ToString()
connection = New SqlConnection(connectionstr)
comd = New SqlCommand()
comd.Connection = connection
sql = "INSERT INTO table(field,...) VALUES (@fld,...); SELECT
SCOPE_IDENTITY()"
comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).Value =
txtvnm.Text
...
connection.Open()
iden = Convert.ToInt32(comd.ExecuteScalar())
comd.ExecuteNonQuery()
connection.Close()
Feb 22 '07 #2
Hi Bob,

...
connection.Open()
iden = Convert.ToInt32(comd.ExecuteScalar())
comd.ExecuteNonQuery()
connection.Close()
Both executeScalar and executeNonQuery are starting the SQL line you have
told that has to be processed. You have them both in your program, therefore
it is logical that it is processed twice.

Cor
Feb 23 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

38
by: jrlen balane | last post by:
basically what the code does is transmit data to a hardware and then receive data that the hardware will transmit. import serial import string import time from struct import * ser =...
6
by: Alexander Stippler | last post by:
Hi, I have some question about the usage of delete. I have some reference counted classes, all derived from SharedObject. It's destructor looks like this: ~SharedObject() { if...
0
by: Matt | last post by:
I am running a exe from command line in my application and when I use ProcessStartInfo it seems to duplicate all the commands within the function. for instance I write a file. Without my...
8
by: Chris Kennedy | last post by:
I want to click a button and have it does something before the page reloads. I now realize the page processes the server side event after page loads. Is there any way of triggering the postback...
2
by: yxq | last post by:
Hello How to detect that own program has been executed? Prevent to execute twice! Thanks
1
by: Elie Grouchko | last post by:
Hi I have an ASP page which is randomly (?) executed twice. I noticed that the http header is different, as if there are two different requests sent to the server. Header 2 below is the one that...
2
by: Chris | last post by:
In SQL 2005 I have a stored procedure as below: @sub_no smallint OUTPUT BEGIN BEGIN TRANSACTION INSERT...INTO
10
by: Bob | last post by:
Hi, i wrote code for inserting data into a table, but it runs twice. If i remove the line: "comd.ExecuteNonQuery()", then it runs once; but i thought that line was necessary for executing the...
7
by: oju | last post by:
Sometimes my scripts calls the server twice even if is a simple stright-forward one. I usually runs a trace in the background when I'm testing new scripts. And quite often I can see, that the...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.