【问题标题】:Automatic insertion of ID in execution在执行中自动插入 ID
【发布时间】:2015-12-21 06:38:55
【问题描述】:

我在 Visual Studio 2010 中创建了插入员工详细信息。当我单击“添加”时,它显示为已添加,并且 id 在后端自动递增。但如果我必须显示为(“你 EMP ID 是某个数字)而不是添加(eg.10) ) 在前端执行时,我该怎么办?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRMSController;
using HRMSBusinessEntities;
using System.Data;


namespace HumanResourceManagementSystems
{
    public partial class HRMSEmployee : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string username = (string)(Session["LoginUser"]);
            if (Session["LoginUser"] != null)
            {
                lblDisplay.Text = "Welcome..." + username;
            }
            Calendar1.Visible = false;
            Label1.Visible = false;
            txtcurrdate.Text = System.DateTime.Now.ToLongDateString();
        }

        protected void Btnmodified_Click(object sender, EventArgs e)
        {
            Calendar1.Visible = true;
            
        }
        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 = "Added" ;
            Clear();


        }
        public void Clear()
        {
            txtname.Text = "";
            txtcurrdate.Text = "";
            txtmodifieddate.Text = "";
        }

        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            txtmodifieddate.Text = Calendar1.SelectedDate.ToString();
        }

        protected void Button3_Click(object sender, EventArgs e)
        {
            Clear();
        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {
            Calendar1.Visible = true;
        }
    }


}

【问题讨论】:

  • 能否请您附上sn-ps?
  • 显示您的代码并说明异常发生在哪一行
  • 这不是一个例外。而不是添加,它应该显示在前端,因为您的 Emp ID 是(这里的数字)例如 10)@AlexJolig
  • @AlexJolig 请回复并提出解决方案。我有代码。

标签: c# sql visual-studio-2010 frontend backend


【解决方案1】:

添加记录后,您可以拉出最大 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() ;
}

【讨论】:

  • 如果表很大,那么您的查询将出现严重的性能问题。
  • 我希望它显示在前端。而不是添加,它应该显示为“您的 Empl ID 是 10”。我应该怎么做?
  • @raghav:- 您需要编辑您的问题并添加更多详细信息。 Mohit 给出的上述解决方案足以继续进行。你只需要从你的 C# 代码中执行这个查询。
  • @MohitShrivastava 标签应显示此消息。您的代码可以吗?
  • @MohitShrivastava 查看我编辑的代码。而不是添加它应该显示为您的 Emp Id 是 10 或某个数字
猜你喜欢
  • 1970-01-01
  • 2011-10-07
  • 2013-08-24
  • 1970-01-01
  • 1970-01-01
  • 2012-02-01
  • 2020-09-23
相关资源
最近更新 更多