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;
}