Click here to Skip to main content
15,901,284 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Suppose I have registered an employee
InsertEmployee();
Now next statement I want to get information for that last registered employee because my requirement is according to the date of joining of that employee I need to calculate some value.
Posted

//this code in asp.cs file for autoid

private void Autoid()
{
string Mode = "M";
pr.Mode = Mode;
dt = new DataTable();
dt = logic.Maxid(pr);
if (dt.Rows[0][0].ToString() != "")
{
txtEMPId.Text = (int.Parse(dt.Rows[0][0].ToString()) + 1).ToString();
}
else
txtRMPId.Text = "1";
}
//inclass cs file

public DataTable InsertEmployeeFormat(ParametersBusinessObjects Schbo)
{
DataTable dt = new DataTable();

DataAcessHelper dashp = new DataAcessHelper();

if (Schbo.Mode != "M")
{
SqlCommand cmd = dashp.CreateCommand("sp_Employeedetails");
dashp.AddParameter(cmd, "@EmpIdid", SqlDbType.Int, ParameterDirection.Input, 4, Schbo.School);
dashp.AddParameter(cmd, "@EmpName", SqlDbType.VarChar, ParameterDirection.Input, 30, Schbo.SchoolName);

dashp.AddParameter(cmd, "@LandlineNo", SqlDbType.VarChar, ParameterDirection.Input, 20, Schbo.LandlineNo);
dashp.AddParameter(cmd, "@MobileNo", SqlDbType.VarChar, ParameterDirection.Input, 20, Schbo.MobileNo);
dashp.AddParameter(cmd, "@Address", SqlDbType.VarChar, ParameterDirection.Input, 30, Schbo.Address);
dashp.AddParameter(cmd,"@DateofBirth",SqlDbType.Date,ParameterDirection.Input ,30, Schbo.strDate);
dashp.AddParameter(cmd, "@EndDate", SqlDbType.Date, ParameterDirection.Input, 30, Schbo.EndDate);
dashp.AddParameter(cmd, "@Flag", SqlDbType.VarChar, ParameterDirection.Input, 2, Schbo.Mode);
if (Schbo.Mode != "A" && Schbo.Mode != "S" && Schbo.Mode != "G" && Schbo.Mode !="N")
{
dashp.ExecuteStoredProcedure(cmd);
}
else
{
dt = dashp.GetDataTable(cmd);

}
}
else
{
SqlCommand cmd = dashp.CreateCommand("sp_maxid");
dt = dashp.GetDataTable(cmd);

}

return dt;

}

//stored procedure

ALTER PROCEDURE sp_maxid
/*
(
@parameter1 int = 5,
@parameter2 datatype OUTPUT
)
*/
AS
select max(intEmployeeId) from EmployeeDetailsInfo
RETURN

u try this.............
 
Share this answer
 
v3
Comments
Qureshali 19-Aug-11 2:12am    
Yeah, Thanks for your reply.
The code is not usable for me but logic is up to the mark
hai,
try this idea.,
use select query on find maximum id on that table and get it from front end.

code like this:
----------------
con.open();
string se="select max(uid) from tablename";
sqlcommand cmd=new sqlcommand(se,con);
int lastid=convert.ToInt32(cmd.executescalar());
con.close();

now you can get lastid value...

i hope this is useful to you...
take care bye bye
 
Share this answer
 
Comments
Qureshali 19-Aug-11 2:12am    
Yeah, Thanks for your reply.
The code is not usable for me but logic is up to the mark
angappans 19-Aug-11 2:22am    
did you sql server and storeprocedure?
Qureshali 19-Aug-11 2:25am    
I use sql server 2005 and all my project work is done in 3 tier architecture
angappans 19-Aug-11 2:56am    
ok fine. then you try to get last id from your store procedure. that is easy one. you know. you just try following idea.

first you pass your inserted values parameters. and next you select last inserted id . i hope this is useful.
You can generate a field named 'CreatedOn' during InsertEmployee operation. Next statement can fetch the l(e)ast data based on CreatedOn field.
 
Share this answer
 
Comments
Qureshali 19-Aug-11 1:12am    
created on field will be of which data type.
can it be 'CreatedOn' as int which will be autoincremented
Qureshali 19-Aug-11 2:11am    
Thanks for your reply
Quite interesting problem,

You'll have to do two things in InsertEmployee()

1: insert the employee
2: Get the ID of the last inserted employee

# 2 can be done this way by executing select query on the base of any information that userentered(but this info should be unique) or on the base of time at which employee entered the information

In this way you can get ID of last registered employee and from it you can get his information
 
Share this answer
 
Comments
Qureshali 19-Aug-11 1:10am    
I have used stored procedures all over my project as I am doing the project through 3 tier architecture. So my question is how stored procedure will return the last record value
Qureshali 19-Aug-11 2:11am    
Thanks for your reply

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