4
Reply

how to create employee code automatically using sqlserver as database and asp,net using C#

Goutam kundu

Goutam kundu

14y
11k
0
Reply

     

     WHEN WE DEFINE DATATYPE THEN WRITE

    employeeid IDENTITY(1,1)

    try
                {
                    string connStr = "Data Source=DEV1;Initial Catalog=SuperRental;Integrated Security=SSPI;";
                    using (SqlConnection conn = new SqlConnection(connStr))
                    {
                        conn.Open();
                        string sqlQuery = "INSERT INTO tbl_employee (EmployeeName) VALUES ('Sardar'); SELECT @@identity";
                        using (SqlCommand cmd = new SqlCommand(sqlQuery, conn))
                        {
                            object obj_employeeCode = cmd.ExecuteScalar();
                            int employeeCode = int.Parse(obj_employeeCode.ToString());

                            System.Console.WriteLine("New Employee code is : " + employeeCode);
                        }
                    }
                }
                catch (Exception ex)
                {
                    System.Console.WriteLine(ex.Message);
                }

    14y
    0

    You can do that easily by going into your datatable ,select the employee code column . On the column properties select Yes for the IsIdentity property .set the identity increament to the step you want each number to auto increament with . this way everytime you enter / create an employee entry the employee code auto increaments .

    I do hope this helps.

    con.open();
    string r = "select max(substring(empid,5,5))from employee";
    cmd=new sqlcommand(str,con);
    sqldatareader dr;
    dr=cmd.executenonquery();
    int d = Convert.ToInt32(r);
    int k = d + 1;

    if (dr.Read() == true)


    {
       
        textbox1.text="emp0"+k;
    }
    con.close();