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