C# 在执行过程中自动插入ID

C# 在执行过程中自动插入ID,c#,sql,visual-studio-2010,frontend,backend,C#,Sql,Visual Studio 2010,Frontend,Backend,我已经在Visual studio 2010中创建了“插入员工详细信息”。当我单击“添加”时,它显示为“已添加”,并且id在后端自动递增。但是,如果在执行时我必须在前端显示为(“您的EMP id是某个数字(例如10)),我应该怎么做 使用系统; 使用System.Collections.Generic; 使用System.Linq; 使用System.Web; 使用System.Web.UI; 使用System.Web.UI.WebControl; 使用HRMSController; 使用HR

我已经在Visual studio 2010中创建了“插入员工详细信息”。当我单击“添加”时,它显示为“已添加”,并且id在后端自动递增。但是,如果在执行时我必须在前端显示为(“您的EMP id是某个数字(例如10)),我应该怎么做

使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.UI;
使用System.Web.UI.WebControl;
使用HRMSController;
使用HRMSBusinessEntities;
使用系统数据;
命名空间人力资源管理系统
{
公共部分类HRM员工:System.Web.UI.Page
{
受保护的无效页面加载(对象发送方、事件参数e)
{
字符串用户名=(字符串)(会话[“登录用户]);
如果(会话[“登录用户”]!=null)
{
lblDisplay.Text=“欢迎…”用户名;
}
日历1.可见=假;
标签1.可见=假;
txtcurrdate.Text=System.DateTime.Now.ToLongDateString();
}
受保护的无效BTN修改\u单击(对象发送方,事件参数e)
{
日历1.可见=真;
}
受保护的无效添加\单击(对象发送方,事件参数e)
{
EmployeeEntity记录=新EmployeeEntity
{
name=txtname.Text,
currentdate=Convert.ToDateTime(txtcurrdate.Text),
modifieddate=Convert.ToDateTime(txtmodifieddate.Text)
};
EmployeeController add=新的EmployeeController();
添加。添加(记录);
标签1.可见=真;
Label1.Text=“已添加”;
清除();
}
公共空间清除()
{
txtname.Text=“”;
txtcurrdate.Text=“”;
txtmodifieddate.Text=“”;
}
受保护的无效日历1\u选择已更改(对象发件人、事件参数e)
{
txtmodifieddate.Text=Calendar1.SelectedDate.ToString();
}
受保护的无效按钮3\u单击(对象发送者,事件参数e)
{
清除();
}
受保护的无效ImageButton1\u单击(对象发送者,ImageClickEventArgs e)
{
日历1.可见=真;
}
}

}
添加记录后,您可以调出max id记录并在UI中显示该记录

查询可能看起来像

Select * from Employee ORDER BY EmpId DESC LIMIT 1;
正如关于性能问题的建议,我们可以这样做

Select max(EmpId) from Employee;
编辑

add.Add(record);

SqlConnection con = new SqlConnection(ConnString);
con.Open();
IDbCommand Cmd = con.CreateCommand();
Cmd.CommandText = "Select max(EmpId) from Employee";
IDataReader LastID = Cmd.ExecuteReader();
LastID.Read();
Label1.Visible = true;
Label1.Text = Your Empl ID is " + LastID[0].ToString() ;
Clear();
con.Close();
另一种方式可能是

protected void Add_Click(object sender, EventArgs e)
{
    EmployeeEntity record = new EmployeeEntity
    {
        name = txtname.Text,
        currentdate = Convert.ToDateTime(txtcurrdate.Text),
        modifieddate = Convert.ToDateTime(txtmodifieddate.Text)
    };
    EmployeeController add = new EmployeeController();
    add.Add(record);
    Label1.Visible = true;
    Label1.Text = "Your Empl ID is " + FetchLastID();
    Clear();
}

public string FetchLastID()
{
    SqlConnection con = new SqlConnection(ConnString);
    con.Open();
    IDbCommand Cmd = con.CreateCommand();
    Cmd.CommandText = "Select max(EmpId) from Employee";
    IDataReader LastID = Cmd.ExecuteReader();
    LastID.Read();
    return LastID[0].ToString() ;
}

请将代码片段包括进来好吗?显示您的代码,并解释在哪一行出现的异常不是异常。它应该显示在前端,而不是添加,因为您的Emp ID是(此处的数字)(例如10)@AlexJolig@AlexJolig请回复并提出解决方案。我有代码。如果表太大,那么您的查询将有严重的性能问题。我希望它显示在前端。而不是添加,它应该显示为“您的员工ID为10”“。我该怎么做呢?@raghav:-你需要编辑你的问题并添加更多细节。Mohit给出的上述解决方案足以继续进行。您只需要从C#代码执行此查询。@Mohithrivastava标签应显示此消息。您的代码是否可行?@Mohithrivastava查看我编辑的代码。它应显示为您的Emp Id为10或某个数字,而不是添加