Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Visual Basic > VB 6 Visual Basic 6 > Pro VB Databases
|
Pro VB Databases Advanced-level VB coding questions specific to using VB with databases. Beginning-level questions or issues not specific to database use will be redirected to other forums.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Pro VB Databases section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 22nd, 2007, 06:20 PM
Friend of Wrox
 
Join Date: Apr 2006
Posts: 160
Thanks: 0
Thanked 0 Times in 0 Posts
Default why does this code run two times?

Hi,

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
H.

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()

 
Old March 16th, 2007, 07:24 AM
Authorized User
 
Join Date: Jun 2003
Posts: 50
Thanks: 0
Thanked 0 Times in 0 Posts
Default

       iden = Convert.ToInt32(comd.ExecuteScalar())
        comd.ExecuteNonQuery()

Both of the above lines perform the insert statement. You need to decide If you are going to ExecuteScalar or ExecuteNonQuery.






Similar Threads
Thread Thread Starter Forum Replies Last Post
compare run times storeprocedure and view trinhvantuyen BOOK: Beginning SQL 0 October 29th, 2008 04:28 AM
Run Code on Install niall29 Visual Basic 2005 Basics 0 October 17th, 2007 11:14 AM
code run in IE but not in FireFox .... help me ! soi den Dreamweaver (all versions) 1 July 18th, 2006 03:09 AM
Can't Run the Code geotechman BOOK: ASP.NET Website Programming Problem-Design-Solution 1 October 12th, 2004 09:08 PM
Better way to run this code harpua Classic ASP Databases 6 February 27th, 2004 03:41 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.