getting id of the record you just added

R

Richard

In normal asp i used
---------

objRS.Open "tbl_Nieuws", objConn, 1, 3
objRS.AddNew

objRS.Fields("N_Datum") = FormatDateTime(Now(),2)
objRS.Fields("N_Title") = ReplaceHTML(Upload.Form("title"))
objRS.Fields("N_Intro") = ReplaceHTML(Upload.Form("intro"))
objRS.Fields("N_Body") = ReplaceHTML(Upload.Form("body"))
objRS.Fields("N_UserID") = Upload.Form("id")
objRS.Fields("N_Image") = file.Filename
objRS.Fields("N_Ip") = strIP
objRS.Fields("N_Deleted") = 0

objRS.Update
IntID = objRS("N_ID")
objRS.Close

---------
to get the id of the record i just added.
in asp.net i have this but i dont know how i can accomplish the same and
wanted to know if there
is some easyway for it.

-----------------

Dim myConnection As New SqlConnection(AppSettings("DatabaseCon"))
Dim strSQL As String = "insert into tbl_Users" & _
"(U_Username, U_Password, U_Email, U_KlantenNR, U_Auth_Code,
U_ToegangsID, U_Registered_On, U_Posts, U_Last_Logged_In, U_Banned,
U_Avatar, U_IP, U_Active, U_Deleted, U_MSN)" & _
"values('"& tbGebruikersNaam.Text & "'," & _
"'"& tbWachtwoord.Text & "'," & _
"'"& tbEmailAdres.Text & "'," & _
""& strKlantenNR &"," & _
"'"& i & "'," & _
"1," & _
"'"& DateTime.Now.ToString("MM'-'dd'-'yyyy HH':'mm':'ss") &"'," & _
"0," & _
"'"& DateTime.Now.ToString("MM'-'dd'-'yyyy HH':'mm':'ss") &"'," & _
"0," & _
"'no_avatar.gif'," & _
"'"& strIP & "'," & _
"0," & _
"0," & _
"'"& strMSN & "')"

Dim MyCommand As New SqlCommand(strSQL, myConnection)
myConnection.Open()
MyCommand.ExecuteNonQuery()
myConnection.Close()

--------------
i want the record of the record i just added to the tbl_Users. How 2? thats
the question.

thx in advance,

Richard
 
M

M. Zeeshan Mustafa

please post this question in this newsgroup:
microsoft.public.dotnet.framework.adonet
(news://news.microsoft.com/microsoft.public.dotnet.framework.adonet)
 
R

Richard

eeuh? Okay althrou i dont know why i cant ask the question here? its asp.net
as far i know.. :S

Richard
 
M

M. Zeeshan Mustafa

you are right Richard, but see your question is about database
(ado in fact) so if you ask this question in adonet ng, there you'd
find more helpers dedicated for ado.net help.. this is a general
practice, the more accurate place you post your question,
the more quickly/well you'll be answered.

good luck :)
 
S

Steven Cheng[MSFT]

Hi Richard,

As for the retrieving the new added record's identity problem. In ADO.NET,
if we use the DataSet and together with the DataAdapter component to manage
the data's insert, update , delete. For example, in vs.net , we create the
DataAdapter using the "DataAdapter Configuration wizard", there is an
advanced option called "Refresh the DataSet" which indicate that the
DataAdpater will add a Select statement after Insert and Update statements
to retrieve identity column values, default values and other values
calculated by the database. You can try draging a DataAdapter in VS.NET to
have a look.

If you don't use DataSet and DataAdapter , just use simple SqlCommand
object to execute sqlstatement, I'm afraid, the only means to rerieve the
identity of the new added record is to execute a select query after the
insert statement to retrieve the identity.
IN addition, here are some former threads in the group discussing on the
similar problem:

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=u3ARYxGtBHA.2552%
40tkmsftngp04&rnum=5&prev=/groups%3Fq%3Dado.net%2Bget%2Bnew%2Brecord%2Bident
ity%26ie%3DUTF-8%26hl%3Den

http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&threadm=51ab01c275c5$f6
7a9130%2437ef2ecf%40TKMSFTNGXA13&rnum=19&prev=/groups%3Fq%3Dado.net%2Bget%2B
new%2Brecord%2Bidentity%26hl%3Den%26lr%3D%26ie%3DUTF-8%26start%3D10%26sa%3DN

Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,829
Messages
2,569,737
Members
45,525
Latest member
RosalindSa

Latest Threads

Top