423,103 Members | 1,428 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 423,103 IT Pros & Developers. It's quick & easy.

why is this code executed twice?

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.