how can i display the last insert id into a textbox on my web form?
here is the code i have so far...
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim MyConnection As SqlConnection
MyConnection = New SqlConnection() 'declare new connection object
MyConnection.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\myfirstdatabase.mdf;Integrated Security=True;User Instance=True"
MyConnection.Open() 'open connection to database
Dim MyCommand As New SqlCommand() 'declare new command object
MyCommand.Connection = MyConnection
MyCommand.CommandText = "INSERT INTO mytemptable (firstname,surname) VALUES(@Name1,@Name2)"
MyCommand.Parameters.Add(New SqlParameter("@Name1", TextBox1.Text.ToString))
MyCommand.Parameters.Add(New SqlParameter("@Name2", TextBox2.Text.ToString))
MyCommand.ExecuteNonQuery()
MyCommand.CommandText = "SELECT SCOPE_IDENTITY()"
'MyCommand.ExecuteScalar()
MyConnection.Close()
MyCommand = Nothing
End Sub