Generate auto incremented id based on last value in database in ASP.Net Core MVC

trisetia302
 
on Jul 20, 2021 08:41 AM
748 Views

Hi Guys,

I'm trying to make custom auto id in Asp.Net Core Mvc 5.0 without EF.

I want is when user create new data it will generate auto id by costume in code, after that data save to database.

my costume auto id is like below :

PTG000001
PTG000002
PTG000003
PTG000004
PTG000005

and soon. How to do that ? Any help could be apriciate.

This is the PetugasController.cs

// GET: PetugasController/Create
[HttpGet]
public IActionResult Create()
{
    PetugasModel petugasModel = new PetugasModel();
    petugasModel = AutoNumberKodePetugas();
    return View(petugasModel);
}
[NonAction]
public PetugasModel AutoNumberKodePetugas()
{
    PetugasModel petugasModel = new PetugasModel();
    using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("db_perpustakaan")))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            con.Open();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "Select Kode_Petugas from Petugas where Kode_Petugas in(select max(Kode_Petugas) from Petugas) Order By Kode_Petugas Desc";
            SqlDataReader rdr = cmd.ExecuteReader();
            rdr.Read();
            if (rdr.HasRows)
            {
                long hitung = Convert.ToInt64(rdr[0].ToString().Substring(rdr["Kode_Petugas"].ToString().Length - 4, 4)) + 1;
                string joinstr = "000000" + hitung;
                string urut = "PTG" + joinstr.Substring(joinstr.Length - 4, 4);
            }
            else
            {
                string urut = "PTG000001";
                petugasModel.Kode_Petugas = urut;
            }
            rdr.Close();
        }
    }
    return petugasModel;
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
trisetia302
 
on Jul 20, 2021 10:32 PM

Alhamdulilah

Problem Solved.

This Code working properly

// GET: PetugasController/Create
[HttpGet]
public IActionResult Create()
{
    PetugasModel petugasModel = new PetugasModel();
    petugasModel = AutoNumberKodePetugas();
    return View(petugasModel);
}
[NonAction]
public PetugasModel AutoNumberKodePetugas()
{
    long _Count;
    string _Order;
    string _JoinString;
    PetugasModel petugasModel = new PetugasModel();
    using (SqlConnection con = new SqlConnection(this._configuration.GetConnectionString("db_perpustakaan")))
    {
        using (SqlCommand cmd = new SqlCommand())
        {
            con.Open();
            cmd.Connection = con;
            cmd.CommandType = CommandType.Text;
            //Command to get max number from field number
            cmd.CommandText = "Select Kode_Petugas from Petugas where Kode_Petugas in(select max(Kode_Petugas) from Petugas) Order By Kode_Petugas Desc";
            SqlDataReader rdr = cmd.ExecuteReader();
            // If Data was find
            rdr.Read();
            if (rdr.HasRows)
            {
                // add data from number field
                _Count = Convert.ToInt64(rdr[0].ToString().Substring(rdr["Kode_Petugas"].ToString().Length - 6, 6)) + 1;
                _JoinString = "000000" + _Count;
                // Take 6 caracter from last right from string joinstr and then add with string _Order
                _Order = "PTG" + _JoinString.Substring(_JoinString.Length - 6, 6);
            }
            else
            {
                //If Data was find then fill variable _Order with PTG000001
                _Order = "PTG000001";
            }
            rdr.Close();
            petugasModel.Kode_Petugas = _Order;
            con.Close();
        }
    }
    return petugasModel;
}