retrieving last entry  
Author Message
LPlate





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

i have a table ID. so how can i retrieve the ID immediately of the last entry after inserting the data into the table

.NET Development31  
 
 
iamunmad





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top



 
 
LPlate





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

how do i do that
 
 
iamunmad





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

Like if your table is:

Table1

Field: IID - identity, not null , int and primary key

Name- varchar(50).

Then Insert Statement Should be

Insert Into Table1 (Name) Values ('LPlate');

SELECT Scope_Identity();



 
 
LPlate





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

when multiple users were to insert a new row of data, will this scope_identity() return the accurate ID
 
 
iamunmad





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

yeap even if multiple users are connected scope_identity will return the current session last inserted id.

 
 
LPlate





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

i tried it on my IDE, it returns me 11 rows of NULL. yeah, the last ID is 11. do not know why, maybe im doing it wrong.

SELECT scope_identity() AS Expr1 from ALERT


 
 
iamunmad





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

SELECT scope_identity() AS Expr1 from ALERT

Its not valid. After inserting in Alert Table use Select Scope_Identity()

That will work try now.



 
 
LPlate





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

im using sql server express working on a mobile app. actually im calling the retrieve through a web service which means i would call the insert method then the retrieve method available at the web service. i think this command is not going to fit into my web service method.
 
 
iamunmad





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

Scope_Identity and insert statement should be in the same session.

ScopeIdentity will not work if you call from two different functions.



 
 
LPlate





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

public bool InsertCustom(int sms, int alarm, string email)
{
bool success = false;
try
{
dc.ConnectToDB();
command = new SqlCommand("INSERT INTO Alert (SMS,Alarm,Email,isDefault) values (" + sms + " , " + alarm + " , '" + email + "' , 'N' )", dc.Connection);
command.ExecuteNonQuery();
dc.Connection.Close();
success = true;
}
catch
{
success = false;
}
return success;
}

my code here in the web service for inserting. think i have nowhere to place the scope_identity() in it.


 
 
Bappi





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

command.Parameters.Add ( "LastInsertedID", SqlDbType.Int );

command.Parameters[0].Direction = ParameterDirection.Output;

then execute nonquery. Check the value of command.Paramters[0].Value;




 
 
LPlate





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

if (command.Paramters[0].Value != null)
{
    return command.Paramters[0].Value;
}

am i right


 
 
Bappi





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

yeap

 
 
LPlate





PostPosted: .NET Framework Data Access and Storage, retrieving last entry Top

thank you. it is working. just to be sure, if multiple users add a new alert, will it return the accurate ID