C# 当用户单击“清除数据”按钮时,在文本框中添加自动生成ID的代码

C# 当用户单击“清除数据”按钮时,在文本框中添加自动生成ID的代码,c#,asp.net,c#-4.0,C#,Asp.net,C# 4.0,我在c#中创建了一个表单,在那里我需要自动生成的ID,当用户单击“清除数据”按钮时,该ID会自动显示。ID的格式应为LEL5序列号(序列号总是以1,2,3等递增形式出现) 我的第一个记录iD是LEL5-1 我的第二个记录id是LEL5-2 请帮忙。 多谢各位 static int EJDS_id = 1; string code; private void button1_Click_1(object sender, EventArgs e) { EJDS_id =

我在c#中创建了一个表单,在那里我需要自动生成的ID,当用户单击“清除数据”按钮时,该ID会自动显示。ID的格式应为LEL5序列号(序列号总是以1,2,3等递增形式出现)

我的第一个记录iD是LEL5-1

我的第二个记录id是LEL5-2

请帮忙。 多谢各位

static int EJDS_id = 1;
 string code;

private void button1_Click_1(object sender, EventArgs e)
    {

        EJDS_id = EJDS_id + 1;
        txtjobno.Text = "LEL5-" + EJDS_id.ToString();

    }

创建一些称为序列号的表:

create table SERIAL_NUMBERS(
    ID INT NOT NULL PRIMARY KEY,
    LASTNUMBER INT NOT NULL);
然后插入一个字符串以存储最后一个值:

insert into SERIAL_NUMBERS(ID, LASTNUMBER) values (1, 0);
然后在代码隐藏中添加以下名称空间:

using System.Data;
using System.Data.OleDb;
using System.Web.Configuration;
protected void button1_Click_1(object sender, EventArgs e) {
    UpdateLastNumber();
    txtjobno.Text = "LEL5-" + GetLastNumber().ToString();
}

protected int GetLastNumber(){
    int last = 0;
    OleDbConnection con = new OleDbConnection(WebConfigurationManager.ConnectionStrings["YourConnectionStringnameFromWebConfigFile"].ConnectionString);
    OleDbCommand cmd = new OleDbCommand("select lastnumber from serial_numbers where id = 1", con); // I added the condition just to be sure that only one value will be queried
    con.Open();
    last = Convert.ToInt32(cmd.ExecuteScalar().ToString());
    con.Close();
    return last;
}

protected void UpdateLastNumber(){
    OleDbConnection con = new OleDbConnection(WebConfigurationManager.ConnectionStrings["YourConnectionStringnameFromWebConfigFile"].ConnectionString);
    OleDbCommand cmd = new OleDbCommand("update serial_numbers set lastnumber = lastnumber + 1 where id = 1", con); // I added the condition just to be sure that only one value will be queried
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}
然后在代码隐藏中:

using System.Data;
using System.Data.OleDb;
using System.Web.Configuration;
protected void button1_Click_1(object sender, EventArgs e) {
    UpdateLastNumber();
    txtjobno.Text = "LEL5-" + GetLastNumber().ToString();
}

protected int GetLastNumber(){
    int last = 0;
    OleDbConnection con = new OleDbConnection(WebConfigurationManager.ConnectionStrings["YourConnectionStringnameFromWebConfigFile"].ConnectionString);
    OleDbCommand cmd = new OleDbCommand("select lastnumber from serial_numbers where id = 1", con); // I added the condition just to be sure that only one value will be queried
    con.Open();
    last = Convert.ToInt32(cmd.ExecuteScalar().ToString());
    con.Close();
    return last;
}

protected void UpdateLastNumber(){
    OleDbConnection con = new OleDbConnection(WebConfigurationManager.ConnectionStrings["YourConnectionStringnameFromWebConfigFile"].ConnectionString);
    OleDbCommand cmd = new OleDbCommand("update serial_numbers set lastnumber = lastnumber + 1 where id = 1", con); // I added the condition just to be sure that only one value will be queried
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}

你试过做些什么来达到这个目的吗?是的,我试过了,但没用。你试过什么?把你的密码贴在这里。没有人不会为您创建完整的解决方案。您必须发布您的代码,然后我们将帮助您检查上述代码。它将生成自动递增的ID,如LEL5-1、LEL5-2、LEL5-3…等等。但当表中有一些记录时,应从我的表1中获取最后一个ID。只有当用户在一个会话中使用它时,您的代码才是正确的。您在另一个会话上的静态变量将被重新设置。请告诉我需要在上面添加此变量的位置code@shweta转到“设计”部分。然后双击“清除数据按钮”。然后您还需要更改代码。用文本框名称更改“测试”和“结果”。textbox.textMy textbox名称为textbox1,标签为E_JDS_ID@shweta然后你可以编辑你的问题,然后在你的按钮中发布你做了什么以及你的代码。当我试图在我的文本框前面写字符串时。text..它给出了错误。不允许写:string然后是textbox name。@shweta不客气。但是下一次你必须自己做一些事情,如果它不起作用,那么把你的问题和代码贴在这里,好吗?请记住。@shweta批准任何答案,您的问题将被“解决”。