How to get @@identity in Visual Studio 2012 using SQL Server 2008

user3429462
Dim con As New SqlConnection
    Dim cmd As New SqlCommand
    Dim cmd1 As New SqlCommand
    Dim cmd2 As New SqlCommand
    Dim cmd3 As New SqlCommand
    Dim rd, rd1, rd2 As SqlDataReader
    Dim lastid As String

    con.ConnectionString = "Data Source=ELMAR-PC\ELMAR;Initial Catalog=users;Integrated Security=True;"
    cmd.Connection = con
    cmd1.Connection = con
    cmd2.Connection = con
    cmd3.Connection = con
    con.Open()

    If TextBox1.Text = "" Or RadioButton3.Checked = False And RadioButton4.Checked = False Then
        MsgBox("Please fill all the required fields", vbCritical)
    End If


    cmd.CommandText = "insert into tblquestion (question,type)values ('" + TextBox1.Text + "','" + TextBox10.Text + "') "
    cmd1.CommandText = "SELECT @@IDENTITY "
    ' TextBox11.Text = Val(cmd1.CommandText)
    lastid = cmd1.CommandText
    cmd2.CommandText = "update tblquestion set question_id ='" + lastid + "' where ID='" + lastid + "'limit 1"
    rd = cmd.ExecuteReader
    con.Close()
bastos.sergio

Try this:

' Validation
If TextBox1.Text = "" Or RadioButton3.Checked = False And RadioButton4.Checked = False Then
    MsgBox("Please fill all the required fields", vbCritical)
    Exit Sub
End If

' Open connection
Dim con As New SqlConnection
con.ConnectionString = "Data Source=ELMAR-PC\ELMAR;Initial Catalog=users;Integrated Security=True;"
con.Open()

Dim lastid As Int32

'Execute first insert
Dim cmd As New SqlCommand
cmd.Connection = con
cmd.CommandText = "insert into tblquestion (question,type)values ('" + TextBox1.Text + "','" + TextBox10.Text + "'); SELECT SCOPE_IDENTITY()"

lastid = Convert.ToInt32(cmd.ExecuteScalar()) 'this will give you the identity
cmd.Dispose()

'Execute second insert with lastid
Dim cmd1 As New SqlCommand
cmd1.Connection = con
cmd1.CommandText = "update tblquestion set question_id ='" + lastid + "' where ID='" + lastid + "'limit 1"
rd = cmd1.ExecuteNonQuery()
cmd1.Dispose()

'Close connection
con.Close()

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

How to turn IDENTITY_INSERT on and off using SQL Server 2008?

From Dev

Using Visual Studio 2012 IDE, but compile with Visual Studio 2008

From Dev

Error 948 :Visual Studio 2012 with SQL Server 2008 R2

From Dev

How to connect to integrated Visual Studio 2012 SQL Server

From Dev

Visual Studio 2012 Premium Compatibility with Windows Server 2008

From Dev

SQL Server 2008 dbms and Visual Studio

From Dev

How to create a new MVC 5 project using ASP.NET Identity in Visual Studio 2012?

From Dev

How to use Visual Studio 2012 to create SQL Server Reporting Service 2012 projects?

From Dev

Install Visual Studio 2012 (and SQL Server 2012) on SD card

From Dev

Autoincrement through IDENTITY in SQL Server 2012 Management Studio

From Dev

How to get recursivelevel using SQL Server 2012 hierarchyid?

From Dev

How to upgrade SQL query from SQL Server 2008 to 2012

From Dev

How to get the intellisense in visual studio 2012 for AngularJS

From Dev

How to get the intellisense in visual studio 2012 for AngularJS

From Dev

SQL Server Data Tool (SSDT) in Visual Studio 2012 not compatible with SQL Server 2012

From Dev

SQL Server Data Tool (SSDT) in Visual Studio 2012 not compatible with SQL Server 2012

From Dev

Connection to SQL Server Database inside Visual Studio 2012 C#

From Dev

Upgrading SQL Server Express 20008 -> 2012 (Visual Studio)

From Dev

SQL Server 2008 / 2012 - views & sql formatting

From Dev

How to get a view table query (code) in SQL Server 2008 Management Studio

From Dev

using tuple with visual studio 2008

From Dev

How I can get table definition in SQL SERVER 2008 R2 using SQL query?

From Dev

Downgrade database from SQL Server 2012 to 2008

From Dev

PDO DBLIB accessing SQL Server 2008 and 2012

From Dev

Move database from SQL Server 2012 to 2008

From Dev

SQL server 2008 and 2012 on the same instance of Windows

From Dev

How do I point CodeBlocks and Visual Studio Express 2008-2012 to custom Include and Lib directories?

From Dev

SQL Server 2012 Identity column after a restore

From Dev

SQL Server 2012 Identity column after a restore

Related Related

  1. 1

    How to turn IDENTITY_INSERT on and off using SQL Server 2008?

  2. 2

    Using Visual Studio 2012 IDE, but compile with Visual Studio 2008

  3. 3

    Error 948 :Visual Studio 2012 with SQL Server 2008 R2

  4. 4

    How to connect to integrated Visual Studio 2012 SQL Server

  5. 5

    Visual Studio 2012 Premium Compatibility with Windows Server 2008

  6. 6

    SQL Server 2008 dbms and Visual Studio

  7. 7

    How to create a new MVC 5 project using ASP.NET Identity in Visual Studio 2012?

  8. 8

    How to use Visual Studio 2012 to create SQL Server Reporting Service 2012 projects?

  9. 9

    Install Visual Studio 2012 (and SQL Server 2012) on SD card

  10. 10

    Autoincrement through IDENTITY in SQL Server 2012 Management Studio

  11. 11

    How to get recursivelevel using SQL Server 2012 hierarchyid?

  12. 12

    How to upgrade SQL query from SQL Server 2008 to 2012

  13. 13

    How to get the intellisense in visual studio 2012 for AngularJS

  14. 14

    How to get the intellisense in visual studio 2012 for AngularJS

  15. 15

    SQL Server Data Tool (SSDT) in Visual Studio 2012 not compatible with SQL Server 2012

  16. 16

    SQL Server Data Tool (SSDT) in Visual Studio 2012 not compatible with SQL Server 2012

  17. 17

    Connection to SQL Server Database inside Visual Studio 2012 C#

  18. 18

    Upgrading SQL Server Express 20008 -> 2012 (Visual Studio)

  19. 19

    SQL Server 2008 / 2012 - views & sql formatting

  20. 20

    How to get a view table query (code) in SQL Server 2008 Management Studio

  21. 21

    using tuple with visual studio 2008

  22. 22

    How I can get table definition in SQL SERVER 2008 R2 using SQL query?

  23. 23

    Downgrade database from SQL Server 2012 to 2008

  24. 24

    PDO DBLIB accessing SQL Server 2008 and 2012

  25. 25

    Move database from SQL Server 2012 to 2008

  26. 26

    SQL server 2008 and 2012 on the same instance of Windows

  27. 27

    How do I point CodeBlocks and Visual Studio Express 2008-2012 to custom Include and Lib directories?

  28. 28

    SQL Server 2012 Identity column after a restore

  29. 29

    SQL Server 2012 Identity column after a restore

HotTag

Archive