473,767 Members | 1,793 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

finding out the id of a recently created record

Hi,

i am wanting to add some data to one table within my MySQL database
using asp.net in VB, however, i want to also add some of the same data
to another table at the same time. Easy yes, but one field within the
second table must contain the unique ID of the record which has just
been created in the other table.

For example.
table 1 will contain: name, address, tel, email etc...

table 2 will contain: unique id of record just created in table 1,
account details etc...

so is there a way to retreive this id once the record has just been
created?

Here is my code so far which is assigned to a button event.

mySqlQuery = "INSERT INTO table1(name, address,tel,ema il)"
mySqlQuery &= " VALUES ('" & strName & "','" & strAd & "','" & strTel &
"', '" & strEmail & "')"

Dim strConn As String =
ConfigurationSe ttings.AppSetti ngs("MySqlConne ctionStr")

Dim myConnection As New MySqlConnection (strConn)

myConnection.Op en()

Dim myCommand As New MySqlCommand()

myCommand.Conne ction = myConnection
myCommand.Comma ndText = mySqlQuery
myCommand.Execu teNonQuery()
myConnection.Cl ose()

Nov 19 '05 #1
3 1312
Dude,

This is how I get the identity back after an insert via stored procedure:

1.. Just after the stored procedure's insert command put:
2..
3.. SELECT @@Identity AS pk_NewRowId
4.. RETURN
For your code (it looks like you're not using a stored procedure) you'd do
it like this:

mySqlQuery = "INSERT INTO table1(name, address,tel,ema il)"
mySqlQuery &= " VALUES ('" & strName & "','" & strAd & "','" & strTel &"',
'" & strEmail & "');SELECT @@Identity AS pk_NewRowId"

(Note that there is a semicolon separating the two statements.)

Then use a MyCommand.Execu teScalar to return the new id.

By the way, instead of concatenating your long string with &= you can just
continue the line like this:

mySqlQuery = "INSERT INTO table1(name, address,tel,ema il)" _
& " VALUES ('" & strName & "','" & strAd & "','" & strTel &"', '" & strEmail
& "');SELECT @@Identity AS pk_NewRowId"

That's a space then an underscore after the first line. That tells visual
studio that next the line is a continuation of the first. When you
concatenate with &= you are actually creating a new string and merging the
two old ones together which is unnecessarily high on processing power.

--
Sincerely,

S. Justin Gengo, MCP
Web Developer / Programmer

www.aboutfortunate.com

"Out of chaos comes order."
Nietzsche
"TheDude5B" <ti*******@gmai l.com> wrote in message
news:11******** *************@g 49g2000cwa.goog legroups.com...
Hi,

i am wanting to add some data to one table within my MySQL database
using asp.net in VB, however, i want to also add some of the same data
to another table at the same time. Easy yes, but one field within the
second table must contain the unique ID of the record which has just
been created in the other table.

For example.
table 1 will contain: name, address, tel, email etc...

table 2 will contain: unique id of record just created in table 1,
account details etc...

so is there a way to retreive this id once the record has just been
created?

Here is my code so far which is assigned to a button event.

mySqlQuery = "INSERT INTO table1(name, address,tel,ema il)"
mySqlQuery &= " VALUES ('" & strName & "','" & strAd & "','" & strTel &
"', '" & strEmail & "')"

Dim strConn As String =
ConfigurationSe ttings.AppSetti ngs("MySqlConne ctionStr")

Dim myConnection As New MySqlConnection (strConn)

myConnection.Op en()

Dim myCommand As New MySqlCommand()

myCommand.Conne ction = myConnection
myCommand.Comma ndText = mySqlQuery
myCommand.Execu teNonQuery()
myConnection.Cl ose()

Nov 19 '05 #2
thanks for that S. Justin

it all works now thanks and using _ & will come in very handy!!

I also found that i can use

"Select LAST_INSERT_ID( )"
myCommand.Execu teScalar

Thanks again

Nov 19 '05 #3
"TheDude5B" <ti*******@gmai l.com> wrote in message
news:11******** **************@ g49g2000cwa.goo glegroups.com.. .
I also found that i can use

"Select LAST_INSERT_ID( )"
myCommand.Execu teScalar


You might also have a look at SCOPE_IDENTITY in BOL in case that's more
appropriate for your needs...
Nov 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
1770
by: Devesh Aggarwal | last post by:
Hi, I have a backup and restore module in my project. The backup uses a typed dataset object (XSD) to get the data from database and creates a xml file as the backup file (using the WriteXml method of dataset). When doing the restore i have to overwrite the data from xml back to database. these are the steps that i follow. 1. get the data from database.
1
1524
by: Jorge Godoy | last post by:
Hi. I have created an interface where I have a QDataBrowser and all of its editing and navigating controls and one of the displayed controls is in a QSpinBox. I have the navigation from current to next and previous records working fine if I only use the data browser controls. I also can recover the correct record if I type in a valid code at the spin box.
2
3167
by: rivka.howley | last post by:
I wrote some code that creates a table with a date/time field at 15-minute intervals. Here's how I create and populate the table With tblDataTemp ..Fields.Append .CreateField("CT_ID", dbLong) ..Fields.Append .CreateField(strTmpIDFld, dbLong) ..Fields.Append .CreateField(strTmpDateFld, dbDate) ..Fields.Append .CreateField(strTmpDataFld, dbDouble)
1
947
by: Ian | last post by:
Hi everyone, I have a (probably very simple) problem which is bugging me. I have created a form which has several controls bound to a dataset. I would like to implement a 'search' combobox; you select the appropriate record in the combox and the form changes the data shown. I have created the combobox abut cannot seem to be able to change the current data shown on the form. I have tried using dataviews: Dim dv As New...
2
3609
by: MidnightSun | last post by:
I'm having trouble trying to find the next ID in a mysql table which is set to autoincrement. I would like to retrieve this before the record is created so I can store its ID as a foriegn key with into another table. I've tried a query SELECT Max(ID) nextID FROM screenings But I found this works until I delete the most recently created records because the Max ID is no longer the one previous of the next ID. Any help would be...
6
2746
by: AA Arens | last post by:
Hi, I have a database with 2 main forms. Contacts and companies. I share the base with two others via LAN. On the companies form I have buttons to navigate throught the records (>400). We are mostly handling let say 5 companies. Every time I have to navigate or choose the find record button to get the right company. I am looking fo a feature to have listed in a combo list the last 5 visited records ("recently visited records").
1
1499
by: t.mitchell | last post by:
Hi, I have a python gtk app that allows users to have one project open at a time. I have recently discovered that projects are not being freed when they are closed - the refcount is not hitting zero. I have used gc.get_referrers() to track down a few culprits, but I have now found that some of my dialog boxes are staying alive after being closed too (and keeping a reference to the project). e.g. gc.collect()
3
1302
by: cemmons41 | last post by:
We recently converted from windows 2000 server to window 2003 server. The access database worked fine on the 2000 server. But now on the 2003 server searches are slower and sometimes search does not find the record then users will redo the search or move off the current record and redo the search and it finds the record. We are using Access 2000 backend that resides on the 2003 server with Access 2002 front ends that reside on the users xp...
4
4201
by: Thomas Arthur Seidel | last post by:
Hello to all, I have a small or big problem with a customer data base, where during a change of system we might have created duplicate records. This should be easy to find, you might think, but, we are talking about roughly 10000 records or less in a total volume of 1 MIO records or more. I have considered a strategy: The station ID and a field with something like a sequence number are supposed to be unique during that period. The...
0
9405
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10169
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10013
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9960
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9841
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7383
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6655
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3930
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2807
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.