Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > why is this sql command executed twice?

Reply
Thread Tools

why is this sql command executed twice?

 
 
Bob
Guest
Posts: n/a
 
      02-21-2007
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 sql command ... if i remove "
oConnection.Open()", then error: "connection is not open".

see my code:
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql, na As String
Dim iden As Integer

sConnectionString =
ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
sql = "INSERT INTO table(...) VALUES (...);"
oConnection = New SqlConnection(sConnectionString)
comd = New SqlCommand(sql, oConnection)
comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
oConnection.Open()
comd.ExecuteNonQuery()

Thanks
Bob


 
Reply With Quote
 
 
 
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      02-22-2007
Bob wrote:
> 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 sql command ... if i remove "
> oConnection.Open()", then error: "connection is not open".
>
> see my code:
> Dim oConnection As SqlConnection
> Dim comd As SqlCommand
> Dim sConnectionString As String
> Dim sql, na As String
> Dim iden As Integer
>
> sConnectionString =
> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
> sql = "INSERT INTO table(...) VALUES (...);"
> oConnection = New SqlConnection(sConnectionString)
> comd = New SqlCommand(sql, oConnection)
> comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
> oConnection.Open()
> comd.ExecuteNonQuery()
>
> Thanks
> Bob
>
>


That code only runs the query once. What does the rest of the code do?

Also, I see that you edited out part of the SQL query. Anything else you
edited out?

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
 
 
 
Bob
Guest
Posts: n/a
 
      02-22-2007
Thanks for replying.

There is indeed more code: here is the whole code:
--------------------------------------------------
Dim trans As SqlTransaction = Nothing 'new
Dim oConnection As SqlConnection
Dim comd As SqlCommand
Dim sConnectionString As String
Dim sql, na As String
Dim iden As Integer
sConnectionString =
ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
sql = "INSERT INTO table(...) VALUES (...); SELECT
SCOPE_IDENTITY();"
oConnection = New SqlConnection(sConnectionString)
comd = New SqlCommand(sql, oConnection)
comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"

Try 'new

oConnection.Open()
comd.ExecuteNonQuery()

trans = connection.BeginTransaction 'new
comd.Transaction = trans 'new

'here another insert sqlcommand but even if i remove this part, it still
executes twice ...

trans.Commit() 'new
Catch sqlEx As SqlException
If trans IsNot Nothing Then
trans.Rollback()
End If
Throw New Exception("error!", sqlEx)
Return

Finally
If connection IsNot Nothing Then
connection.Close()
End If
End Try
Response.Redirect("fin.aspx")
End Sub







"Göran Andersson" <> schreef in bericht
news:u$5k%...
> Bob wrote:
>> 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 sql command ... if i
>> remove " oConnection.Open()", then error: "connection is not open".
>>
>> see my code:
>> Dim oConnection As SqlConnection
>> Dim comd As SqlCommand
>> Dim sConnectionString As String
>> Dim sql, na As String
>> Dim iden As Integer
>>
>> sConnectionString =
>> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
>> sql = "INSERT INTO table(...) VALUES (...);"
>> oConnection = New SqlConnection(sConnectionString)
>> comd = New SqlCommand(sql, oConnection)
>> comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
>> oConnection.Open()
>> comd.ExecuteNonQuery()
>>
>> Thanks
>> Bob

>
> That code only runs the query once. What does the rest of the code do?
>
> Also, I see that you edited out part of the SQL query. Anything else you
> edited out?
>
> --
> Göran Andersson
> _____
> http://www.guffa.com



 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      02-22-2007
I think it has to do with the second part of the query: SELECT
SCOPE_IDENTITY()"

How can i then make that the insert only executes once?

"Göran Andersson" <> schreef in bericht
news:u$5k%...
> Bob wrote:
>> 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 sql command ... if i
>> remove " oConnection.Open()", then error: "connection is not open".
>>
>> see my code:
>> Dim oConnection As SqlConnection
>> Dim comd As SqlCommand
>> Dim sConnectionString As String
>> Dim sql, na As String
>> Dim iden As Integer
>>
>> sConnectionString =
>> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
>> sql = "INSERT INTO table(...) VALUES (...);"
>> oConnection = New SqlConnection(sConnectionString)
>> comd = New SqlCommand(sql, oConnection)
>> comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
>> oConnection.Open()
>> comd.ExecuteNonQuery()
>>
>> Thanks
>> Bob

>
> That code only runs the query once. What does the rest of the code do?
>
> Also, I see that you edited out part of the SQL query. Anything else you
> edited out?
>
> --
> Göran Andersson
> _____
> http://www.guffa.com



 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      02-22-2007
Bob wrote:
> Thanks for replying.
>
> There is indeed more code: here is the whole code:
> --------------------------------------------------
> Dim trans As SqlTransaction = Nothing 'new
> Dim oConnection As SqlConnection
> Dim comd As SqlCommand
> Dim sConnectionString As String
> Dim sql, na As String
> Dim iden As Integer
> sConnectionString =
> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
> sql = "INSERT INTO table(...) VALUES (...); SELECT
> SCOPE_IDENTITY();"
> oConnection = New SqlConnection(sConnectionString)
> comd = New SqlCommand(sql, oConnection)
> comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
>
> Try 'new
>
> oConnection.Open()
> comd.ExecuteNonQuery()
>
> trans = connection.BeginTransaction 'new
> comd.Transaction = trans 'new
>
> 'here another insert sqlcommand but even if i remove this part, it still
> executes twice ...
>
> trans.Commit() 'new
> Catch sqlEx As SqlException
> If trans IsNot Nothing Then
> trans.Rollback()
> End If
> Throw New Exception("error!", sqlEx)
> Return
>
> Finally
> If connection IsNot Nothing Then
> connection.Close()
> End If
> End Try
> Response.Redirect("fin.aspx")
> End Sub
>


I see that you use the same command object for the second query. It
still contains the query and parameters from the first call, do you
replace them?

Although unlikely to be the cause, you can try to remove the "select
scope_identity()" part of the query. You are not using that anyway.

--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      02-22-2007
the parameters are replaced, and i use the SCOPE_IDENTITY() for the second
insert


"Göran Andersson" <> schreef in bericht
news:%23h2l%...
> Bob wrote:
>> Thanks for replying.
>>
>> There is indeed more code: here is the whole code:
>> --------------------------------------------------
>> Dim trans As SqlTransaction = Nothing 'new
>> Dim oConnection As SqlConnection
>> Dim comd As SqlCommand
>> Dim sConnectionString As String
>> Dim sql, na As String
>> Dim iden As Integer
>> sConnectionString =
>> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
>> sql = "INSERT INTO table(...) VALUES (...); SELECT
>> SCOPE_IDENTITY();"
>> oConnection = New SqlConnection(sConnectionString)
>> comd = New SqlCommand(sql, oConnection)
>> comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
>>
>> Try 'new
>>
>> oConnection.Open()
>> comd.ExecuteNonQuery()
>>
>> trans = connection.BeginTransaction 'new
>> comd.Transaction = trans 'new
>>
>> 'here another insert sqlcommand but even if i remove this part, it still
>> executes twice ...
>>
>> trans.Commit() 'new
>> Catch sqlEx As SqlException
>> If trans IsNot Nothing Then
>> trans.Rollback()
>> End If
>> Throw New Exception("error!", sqlEx)
>> Return
>>
>> Finally
>> If connection IsNot Nothing Then
>> connection.Close()
>> End If
>> End Try
>> Response.Redirect("fin.aspx")
>> End Sub
>>

>
> I see that you use the same command object for the second query. It still
> contains the query and parameters from the first call, do you replace
> them?
>
> Although unlikely to be the cause, you can try to remove the "select
> scope_identity()" part of the query. You are not using that anyway.
>
> --
> Göran Andersson
> _____
> http://www.guffa.com



 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      02-22-2007
How do you use the result from scope_identity in the second insert, as
you don't accept any result from the first query?

Bob wrote:
> the parameters are replaced, and i use the SCOPE_IDENTITY() for the second
> insert
>
>
> "Göran Andersson" <> schreef in bericht
> news:%23h2l%...
>> Bob wrote:
>>> Thanks for replying.
>>>
>>> There is indeed more code: here is the whole code:
>>> --------------------------------------------------
>>> Dim trans As SqlTransaction = Nothing 'new
>>> Dim oConnection As SqlConnection
>>> Dim comd As SqlCommand
>>> Dim sConnectionString As String
>>> Dim sql, na As String
>>> Dim iden As Integer
>>> sConnectionString =
>>> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
>>> sql = "INSERT INTO table(...) VALUES (...); SELECT
>>> SCOPE_IDENTITY();"
>>> oConnection = New SqlConnection(sConnectionString)
>>> comd = New SqlCommand(sql, oConnection)
>>> comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
>>>
>>> Try 'new
>>>
>>> oConnection.Open()
>>> comd.ExecuteNonQuery()
>>>
>>> trans = connection.BeginTransaction 'new
>>> comd.Transaction = trans 'new
>>>
>>> 'here another insert sqlcommand but even if i remove this part, it still
>>> executes twice ...
>>>
>>> trans.Commit() 'new
>>> Catch sqlEx As SqlException
>>> If trans IsNot Nothing Then
>>> trans.Rollback()
>>> End If
>>> Throw New Exception("error!", sqlEx)
>>> Return
>>>
>>> Finally
>>> If connection IsNot Nothing Then
>>> connection.Close()
>>> End If
>>> End Try
>>> Response.Redirect("fin.aspx")
>>> End Sub
>>>

>> I see that you use the same command object for the second query. It still
>> contains the query and parameters from the first call, do you replace
>> them?
>>
>> Although unlikely to be the cause, you can try to remove the "select
>> scope_identity()" part of the query. You are not using that anyway.
>>
>> --
>> Göran Andersson
>> _____
>> http://www.guffa.com

>
>



--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      02-22-2007
i forgot this line which gets the value of the scope_identity:
iden = Convert.ToInt32(comd.ExecuteScalar())



"Göran Andersson" <> schreef in bericht
news:...
> How do you use the result from scope_identity in the second insert, as you
> don't accept any result from the first query?
>
> Bob wrote:
>> the parameters are replaced, and i use the SCOPE_IDENTITY() for the
>> second insert
>>
>>
>> "Göran Andersson" <> schreef in bericht
>> news:%23h2l%...
>>> Bob wrote:
>>>> Thanks for replying.
>>>>
>>>> There is indeed more code: here is the whole code:
>>>> --------------------------------------------------
>>>> Dim trans As SqlTransaction = Nothing 'new
>>>> Dim oConnection As SqlConnection
>>>> Dim comd As SqlCommand
>>>> Dim sConnectionString As String
>>>> Dim sql, na As String
>>>> Dim iden As Integer
>>>> sConnectionString =
>>>> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
>>>> sql = "INSERT INTO table(...) VALUES (...); SELECT
>>>> SCOPE_IDENTITY();"
>>>> oConnection = New SqlConnection(sConnectionString)
>>>> comd = New SqlCommand(sql, oConnection)
>>>> comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
>>>>
>>>> Try 'new
>>>>
>>>> oConnection.Open()
>>>> comd.ExecuteNonQuery()
>>>>
>>>> trans = connection.BeginTransaction 'new
>>>> comd.Transaction = trans 'new
>>>>
>>>> 'here another insert sqlcommand but even if i remove this part, it
>>>> still executes twice ...
>>>>
>>>> trans.Commit() 'new
>>>> Catch sqlEx As SqlException
>>>> If trans IsNot Nothing Then
>>>> trans.Rollback()
>>>> End If
>>>> Throw New Exception("error!", sqlEx)
>>>> Return
>>>>
>>>> Finally
>>>> If connection IsNot Nothing Then
>>>> connection.Close()
>>>> End If
>>>> End Try
>>>> Response.Redirect("fin.aspx")
>>>> End Sub
>>>>
>>> I see that you use the same command object for the second query. It
>>> still contains the query and parameters from the first call, do you
>>> replace them?
>>>
>>> Although unlikely to be the cause, you can try to remove the "select
>>> scope_identity()" part of the query. You are not using that anyway.
>>>
>>> --
>>> Göran Andersson
>>> _____
>>> http://www.guffa.com

>>
>>

>
>
> --
> Göran Andersson
> _____
> http://www.guffa.com



 
Reply With Quote
 
=?ISO-8859-1?Q?G=F6ran_Andersson?=
Guest
Posts: n/a
 
      02-22-2007
Oh, so you execute the query again to get the identity?

Guess why it's executed twice?

That line will add another record, but it will not get the identity of
either of the records added. The result of the query contains two record
sets. The first set is returned by the insert query and is empty. The
second set contains the identity returned by the select query.

Bob wrote:
> i forgot this line which gets the value of the scope_identity:
> iden = Convert.ToInt32(comd.ExecuteScalar())
>
>
>
> "Göran Andersson" <> schreef in bericht
> news:...
>> How do you use the result from scope_identity in the second insert, as you
>> don't accept any result from the first query?
>>
>> Bob wrote:
>>> the parameters are replaced, and i use the SCOPE_IDENTITY() for the
>>> second insert
>>>
>>>
>>> "Göran Andersson" <> schreef in bericht
>>> news:%23h2l%...
>>>> Bob wrote:
>>>>> Thanks for replying.
>>>>>
>>>>> There is indeed more code: here is the whole code:
>>>>> --------------------------------------------------
>>>>> Dim trans As SqlTransaction = Nothing 'new
>>>>> Dim oConnection As SqlConnection
>>>>> Dim comd As SqlCommand
>>>>> Dim sConnectionString As String
>>>>> Dim sql, na As String
>>>>> Dim iden As Integer
>>>>> sConnectionString =
>>>>> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
>>>>> sql = "INSERT INTO table(...) VALUES (...); SELECT
>>>>> SCOPE_IDENTITY();"
>>>>> oConnection = New SqlConnection(sConnectionString)
>>>>> comd = New SqlCommand(sql, oConnection)
>>>>> comd.Parameters.Add("@var1", SqlDbType.NVarChar, 10).value="ok"
>>>>>
>>>>> Try 'new
>>>>>
>>>>> oConnection.Open()
>>>>> comd.ExecuteNonQuery()
>>>>>
>>>>> trans = connection.BeginTransaction 'new
>>>>> comd.Transaction = trans 'new
>>>>>
>>>>> 'here another insert sqlcommand but even if i remove this part, it
>>>>> still executes twice ...
>>>>>
>>>>> trans.Commit() 'new
>>>>> Catch sqlEx As SqlException
>>>>> If trans IsNot Nothing Then
>>>>> trans.Rollback()
>>>>> End If
>>>>> Throw New Exception("error!", sqlEx)
>>>>> Return
>>>>>
>>>>> Finally
>>>>> If connection IsNot Nothing Then
>>>>> connection.Close()
>>>>> End If
>>>>> End Try
>>>>> Response.Redirect("fin.aspx")
>>>>> End Sub
>>>>>
>>>> I see that you use the same command object for the second query. It
>>>> still contains the query and parameters from the first call, do you
>>>> replace them?
>>>>
>>>> Although unlikely to be the cause, you can try to remove the "select
>>>> scope_identity()" part of the query. You are not using that anyway.
>>>>
>>>> --
>>>> Göran Andersson
>>>> _____
>>>> http://www.guffa.com
>>>

>>
>> --
>> Göran Andersson
>> _____
>> http://www.guffa.com

>
>



--
Göran Andersson
_____
http://www.guffa.com
 
Reply With Quote
 
Bob
Guest
Posts: n/a
 
      02-22-2007
That's what i suspected in my earlier message in this thread.
The only solution i found is removing "comd.ExecuteNonQuery()".
Is that a good thing? If not, what can i do?
Thanks



"Göran Andersson" <> schreef in bericht
news:...
> Oh, so you execute the query again to get the identity?
>
> Guess why it's executed twice?
>
> That line will add another record, but it will not get the identity of
> either of the records added. The result of the query contains two record
> sets. The first set is returned by the insert query and is empty. The
> second set contains the identity returned by the select query.
>
> Bob wrote:
>> i forgot this line which gets the value of the scope_identity:
>> iden = Convert.ToInt32(comd.ExecuteScalar())
>>
>>
>>
>> "Göran Andersson" <> schreef in bericht
>> news:...
>>> How do you use the result from scope_identity in the second insert, as
>>> you don't accept any result from the first query?
>>>
>>> Bob wrote:
>>>> the parameters are replaced, and i use the SCOPE_IDENTITY() for the
>>>> second insert
>>>>
>>>>
>>>> "Göran Andersson" <> schreef in bericht
>>>> news:%23h2l%...
>>>>> Bob wrote:
>>>>>> Thanks for replying.
>>>>>>
>>>>>> There is indeed more code: here is the whole code:
>>>>>> --------------------------------------------------
>>>>>> Dim trans As SqlTransaction = Nothing 'new
>>>>>> Dim oConnection As SqlConnection
>>>>>> Dim comd As SqlCommand
>>>>>> Dim sConnectionString As String
>>>>>> Dim sql, na As String
>>>>>> Dim iden As Integer
>>>>>> sConnectionString =
>>>>>> ConfigurationManager.ConnectionStrings("myconn").C onnectionString.ToString()
>>>>>> sql = "INSERT INTO table(...) VALUES (...); SELECT
>>>>>> SCOPE_IDENTITY();"
>>>>>> oConnection = New SqlConnection(sConnectionString)
>>>>>> comd = New SqlCommand(sql, oConnection)
>>>>>> comd.Parameters.Add("@var1", SqlDbType.NVarChar,
>>>>>> 10).value="ok"
>>>>>>
>>>>>> Try 'new
>>>>>>
>>>>>> oConnection.Open()
>>>>>> comd.ExecuteNonQuery()
>>>>>>
>>>>>> trans = connection.BeginTransaction 'new
>>>>>> comd.Transaction = trans 'new
>>>>>>
>>>>>> 'here another insert sqlcommand but even if i remove this part, it
>>>>>> still executes twice ...
>>>>>>
>>>>>> trans.Commit() 'new
>>>>>> Catch sqlEx As SqlException
>>>>>> If trans IsNot Nothing Then
>>>>>> trans.Rollback()
>>>>>> End If
>>>>>> Throw New Exception("error!", sqlEx)
>>>>>> Return
>>>>>>
>>>>>> Finally
>>>>>> If connection IsNot Nothing Then
>>>>>> connection.Close()
>>>>>> End If
>>>>>> End Try
>>>>>> Response.Redirect("fin.aspx")
>>>>>> End Sub
>>>>>>
>>>>> I see that you use the same command object for the second query. It
>>>>> still contains the query and parameters from the first call, do you
>>>>> replace them?
>>>>>
>>>>> Although unlikely to be the cause, you can try to remove the "select
>>>>> scope_identity()" part of the query. You are not using that anyway.
>>>>>
>>>>> --
>>>>> Göran Andersson
>>>>> _____
>>>>> http://www.guffa.com
>>>>
>>>
>>> --
>>> Göran Andersson
>>> _____
>>> http://www.guffa.com

>>
>>

>
>
> --
> Göran Andersson
> _____
> http://www.guffa.com



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Wait for a system command executed in the background Brice Python 2 11-05-2006 10:21 PM
How to know which command executed for DataAdapter.Update(DataSet) Samy ASP .Net 0 11-16-2005 04:51 AM
Getting output from executed command that is not STDIN Sami Viitanen Python 1 08-07-2003 12:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57