Get value of primary key after INSERT statement using Web Matrix (VB.NET)

I am using the following function developed using Web Matrix to insert a record into a table when the user clicks the submit button.  How can i get the "primary key" or value of the inserted record back to the application.  The following is my insert statement:

Sub btnSubmit_Click(sender As Object, e As EventArgs)
      CreateMyRecord("98", "UU1") 
      '==> need the primary key of the inserted record here.......
End Sub

Function CreateMyRecord(ByVal streetNumber As Integer, ByVal authorityRef As String) As Integer
        Dim connectionString As String = "server='(local)'; trusted_connection=true; database='MyDbName'"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

        Dim queryString As String = "INSERT INTO [DelegationAuthority] ([streetNumber], [AuthorityRef]) VALUES (@streetNumber, @AuthorityRef)"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_streetNumber As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_streetNumber.ParameterName = "@streetNumber"
        dbParam_streetNumber.Value = streetNumber
        dbParam_streetNumber.DbType = System.Data.DbType.Int32
        dbCommand.Parameters.Add(dbParam_streetNumber)
        Dim dbParam_authorityRef As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_authorityRef.ParameterName = "@AuthorityRef"
        dbParam_authorityRef.Value = authorityRef
        dbParam_authorityRef.DbType = System.Data.DbType.String
        dbCommand.Parameters.Add(dbParam_authorityRef)

        Dim rowsAffected As Integer = 0
        dbConnection.Open
        Try
            rowsAffected = dbCommand.ExecuteNonQuery
        Finally
            dbConnection.Close
        End Try

        Return rowsAffected
End Function


 

0
Raymond
11/12/2005 10:18:53 AM
asp.net.getting-started 91979 articles. 3 followers. Follow

3 Replies
1051 Views

Similar Articles

[PageSpeed] 4

How is the the primary key generated? If its just an automatic index, yuou should be able just to do

SELECT top 1 (whatever the PRIMARYKEY is called) from DelegationAuthority where streetnumber = xxx and authorityref = yyy

It is possible the generate the index from your program - use something like GUID if you want to generate the index from code and then put it into your table when you insert yout streetnumber and authorityref

hth
0
fUNKYgIBBON
11/12/2005 11:27:07 AM
I am actually creating a record with more than 2 fields.  I was thinking if anyone has tried using SCOPE_IDENTITY or @@IDENTITY.
0
Raymond
11/12/2005 12:42:30 PM
OleDbCommand dbCommand = new OleDbCommand();
dbCommand.Connection = yourDbConnection;
dbCommand.CommandText = "SELECT @@IDENTITY AS NEW_ID";
dbCommand.CommandType = CommandType.Text;
int newId = (int)dbCommand.ExecuteScalar();

NC...
0
NC01
11/12/2005 4:42:05 PM
Reply:

Similar Artilces:

How to get the HREF value of the Anchor tag in a web page using the web browser control in vb.net
Hi Friends,               i have a webserver control in my application.i load the web browser control on a button click using the below code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click                AxWebBrowser1.Navigate("http://www.rateitall.com/i-322335-.aspx", False) End sub once the page is loaded in the browser i want the <a> HREF value in that page.For example if that page has ...

Sending SMS using vb.net or C#.net using vb.net or c#.net
Hi  My requirement is I hav one csv file with these fields id,mobilenum,messgae,status.intiallu staus is 0. once i read the all fileds and take that mobile number.using tat mobile number  i need to send sms .after sending sms i shuld change status as 1 How to send sms thru coding (please dont provide any links.if it is provide also please give working links becox i checked codeproject .i didnt get any nice link. and also provide the how to update the status field im csv file   Thank ssandhya   To send SMS, you need some third party SMS providers. if you consul...

How to get HTML Source of web page opened by web browser using vb.net
hello friends,         i have opened an web browser in vb.net using the below code Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim nullObject As System.Object = 0 Dim str As String = "" Dim nullObjStr As System.Object = str Windows.Forms.Cursor.Current = Cursors.WaitCursor Dim i As Int16 For i = 1 To 2 AxWebBrowser1.Navigate("http://www.google.co.in/search?hl=en&q=test&meta=", True) AxWebBrowser1.Document. Next Windows.Forms.Cursor.Current = Cursors.Default End Sub  ...

How to get new web app to use .NET 2.0 on Win2K server using .NET 1.1 as default framework?
I have .net framework1.1 and 2.0 on our server running win2K. i have existing applications that uses asp.net 1.1, i also have a new app that use ASP.net 2.0.  how can i set my new .NET 2.0 app to point to asp.net 2.0 without messing up my existing .NET 1.1 apps? To specify the ASP .Net version fro a web application, do the followingIn IIS Manager select the web application -> Properties -> ASP.Net TabSelect  ASP.Net version you want to use for the applicationRegards,PrashantDont forget to click "Mark as Answer" on the post that helped you. But I am running Win2K...

used a c#.net to vb.net converter and am now getting a syntax error.
ok I used a cool utility at http://www.kamalpatel.net/ConvertCSharp2VB.aspx to convert some c#.net to vb.net and it spit out a bunch of code which almost works but I am getting a syntax error on this line Imports (StreamReader sr = Shadows Function)() As StreamReader(objResponse.GetResponseStream()) I tried it that way and also like Imports (StreamReader sr = Shadows Function)() As StreamReader(objResponse.GetResponseStream()) but either way gave the same error Compiler Error Message: BC30035: Syntax error. Source Error: Line 26: Line ...

how to get the primary key autoincrement id inserted in ADO.NET?
Hi all! I have a great problem in my C# application. I'm using ADO.NET with ASA9 ..NET Data Provider. I need to execute an insert statement of one row in a table of my database and I need to get the autoincrement primary key id just inserted. I don't know if there is a good design patter to do that, but i try to get this in this manner..... The table is defined as follows: CREATE TABLE Utenti ( id_utente SMALLINT NOT NULL DEFAULT AUTOINCREMENT, utente VARCHAR(16) NOT NULL, password VARCHAR(16) NULL, nome VARCHAR(30) N...

getting start with vb.net and crystal repots(web form)
I use Crystal Report9 in Visual Studio.NET 2003..... Example at Northwind database I want a application that.....a form include a combobox that contain CustomerID.....If I choose one CustomerID and click OK button....a Report will display information about this Customer......I think that must use Paramater.....how to do that ???? (How do I start to coding --please step by step or sample) please help me !!! Thank you so much!!! very good I have Dropdownlist when I choose the 'month'--> the all data of this field will show I use this Private Sub ddlMonth_Sel...

Get Scope Identity Value using ObjectDataSource and Vb.Net
Hi, I have been trying to get the scope Identity after inserting a record using an ObjectDataSource. I can't find what event, or how to get the value that the scope identity returns. Here is my Sproc. ALTER PROCEDURE dbo.[YourCompany_LanCustomer_Insert]     (     @DNNUserID int,     @FirstName nvarchar(50),     @LastName nvarchar(50),     @Address nvarchar(50),     @Address2 nvarchar(50),     @City nvarchar(50),     @State nvarchar(50),    ...

How to get the selected text HREF value using vb.net
Hello friends,                     If i have a page opened in a web browser control for example www.google.com .if i select a particular content in google web page i have to get the href value of the selected text.how to do this vb.net.can anyone help me. with regards, Balaji.   Balaji,Chennai...

ASA9: how to get the primary key autoincrement id inserted in ADO.NET?
Hi all! I have a great problem in my C# application. I'm using ADO.NET with ASA9 ..NET Data Provider. I need to execute an insert statement of one row in a table of my database and I need to get the autoincrement primary key id just inserted. I don't know if there is a good design patter to do that, but i try to get this in this manner..... The table is defined as follows: CREATE TABLE Utenti ( id_utente SMALLINT NOT NULL DEFAULT AUTOINCREMENT, utente VARCHAR(16) NOT NULL, password VARCHAR(16) NULL, nome VARCHAR(30) NULL...

How to get RS (From VB6) to .NET (VB.NET)
Hi Friends,         I have requirement, there is one function in VB6 it returns a RS, I need to call that function in  VB.NET, how to get those values into .NET, if can any body can help (pass me some code snippets), that will be greate, Thank you.Madhu... -Madhu hey guys i got this one just by using fill method.-Madhu...

Insert datetime, using ADO.net VB.net
Hello there,I got a little code there are inserting a record into my msSQL database..But i cant insert the datetime, for one reason?The problem is in line 7 () where i want to insert DateTime.Now 1 Protected Sub SendPmTilAfviste(ByVal modtager As String, ByVal festID As String) 2 ' Connection 3 Dim conn As SqlConnection = New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True") 4 conn.Open() 5 6 ' SQL-kommandoen 7 Dim cm...

.Net 2.0 Web application using Vb.net is unable to create object of another dll writen in C# .net 2.0
Hi, I habe created one ASP.net web application using Vb.net which is adding reference of dlls written in C# and .net 2.0. But whenever trying to create object of referencing dll, it is throwing error :Object reference does not set to an object. But, locally it is working fine,. In the server i have deployed the .aspx files and dll files in the bin. There were already an web.config in the server which is of .net 1.1. But checked it is retrieving value for that web.config correctly. Should i have to deply any other files and if not what can be the solution for this? Please help. Thanks, So...

how to get CallerID using vb.net
Dear All!! does anyone know how to get the callerid of the incoming call using vb net or C# i have a telephone line connected with a modem, i need to answer the incoming call by getting the caller id information. Need assistance.     Hey,This forum is dedicated to asp.net which is a web based technology.As such we dont have access to this level of hardware through the browser. I am sure if anyone on here sees this post and they know the answer they will post it but you would probably get a better response if you want to the dedicated Windows Forms forums at:http://...

Web resources about - Get value of primary key after INSERT statement using Web Matrix (VB.NET) - asp.net.getting-started

User:Jimbo Wales/Statement of principles - Wikipedia, the free encyclopedia
As we move forward with software and social changes, I think it is imperative that I state clearly and forcefully my views on openness and the ...

Category:Articles containing potentially dated statements from June 2006 - Wikipedia, the free encyclopedia ...
This is an administration category . It is used for administration of the Wikipedia project and is not part of the encyclopedia. It contains ...

The innovation nation: Elitist statement which won't generate change
Readers discuss the Turnbull government's innovation statement.

The statements murdered grandmother Helen Dawson Key's family will never get to read
If the family members of Helen Dawson Key had their chance, they would have faced her killer and told of their pain.&nbsp;

Innovation statement: At a glance
Find out the main points from Malcolm Turnbull's innovation statement.

Obama's statement on ISIS - Business Insider Deutschland
US President Barack Obama spoke at the Pentagon on Monday to give an overview of the country's strategy to combat the terrorist group ISIS.

FOMC Statement: Fed Funds Rate target range increased to 1/4 to 1/2 percent
FOMC Statement: Information received since the Federal Open Market Committee met in October suggests that economic activity has been expanding ...

What changed in new Fed statement
This is a comparison of today's FOMC statement with the one issued after the Fed's previous policy-making meeting on Oct. 28.

Dallas Stars: Cold facts: Loss to Blues 'clearly a statement' that Stars' upcoming divisional games will ...
Blues 3, Stars 0Click here for the full box score.Three Stars1. David Backes, Blues Goal, assist and fight give St. Louis captain the Gordie... ...

Comic-Con Organizers Issue Statement After Fan Confusion
... desire to expand their annual convention in San Diego. Comic-Con International made the unusual move today of putting out a statement regarding ...

Resources last updated: 12/17/2015 5:01:17 AM