Click here to Skip to main content
15,905,508 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I want to pass the id(primary key) of the last inserted row in a table in my database as a parameter while calling a method.Are there any ways to do it without using stored procedures???? plz help.Thanks in advance.
Posted
Updated 27-Nov-11 21:45pm
v2
Comments
[no name] 28-Nov-11 3:45am    
now that's clear
D K N T H 28-Nov-11 3:47am    
then what do you want? in a command text?
devoidofsilence 28-Nov-11 4:05am    
no sir my major problem is in calling the method.I have my coding as:

int i = blbr.AddNewBorrower(txtBorrowerName.Text, txtBorrowerAddress.Text, Convert.ToInt32(txtBorrowerRollNo.Text), txtBorrowerFaculty.Text, Convert.ToInt32(txtBorrowerEnrolledYear.Text), Convert.ToInt64(txtBorrowerContactNo.Text), count);
int j = blbr.BooksBorrowedDetails(PARAMETER, Convert.ToInt32(dataGridView1.CurrentRow.Cells[0].Value.ToString()), DateTime.Now);

case i>0 inserts the details of a new book borrower in a library and i want to make it possible that case j>0 keeps record of the borrower and the books he borrowed. So for case j>0 is true or false, the parameter PARAMETER that i have used has to take the borrower_id of the new user that i>0 has inserted just before. How to pass the parameter PARAMETER in this case without using stored procedure???? thank you.

Well, if you really don't want to use stored procedures, which I'd strongly recommend, you should do a
SQL
select @@Identity
immediatly after your insert statement within the same connection. Do not close your connection to stay in the same scope with your insert.
 
Share this answer
 
Comments
Michel [mjbohn] 28-Nov-11 4:07am    
Giving a reason for downvote would be great. If you know a better way for retrieving the id just let me know
check this out,
SQL
Declare @ID int;



insert into Table1(Name, Age)
values (Name,Age)

set @ID = Select Max(ID) from Table 1 

--ID - is your primary key

-- call a method
--sample method

select * from Table2 where ID = @ID

SQL
-- here you can see that the newly inserted id in Table1 is in @ID Parameter, so in --that case the value of the @ID is the newly inserted item in Table, where you can --use this to another method..


hope it helps,

just ask me if you have a question..
don't forget to mark this as answer if it helps you,

thanks
 
Share this answer
 
Comments
devoidofsilence 28-Nov-11 3:34am    
sory sir this didn't help. i asked without using stored procedure.
 
Share this answer
 
Try this one:

SQL
SELECT TOP 1 id FROM [TABLENAME] ORDER BY id DESC 


Regards,
Eduard
 
Share this answer
 
Comments
devoidofsilence 28-Nov-11 3:34am    
sory sir this didn't help. i asked without using stored procedure.Thanks.
[no name] 28-Nov-11 3:44am    
you wont get it without storedproc
Michel [mjbohn] 28-Nov-11 3:45am    
As long as your are the only user on the database and your inserts are running all on the same thread, this might work

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900