SqlDataReader value inserting 0 in table

Analyst_SQL 3,551 Reputation points
2023-02-25T07:31:10.28+00:00

I am populating Enrollnumber using Below Model Class into View

  public class RecordID
    {
        SqlConnection con = new SqlConnection("data source=SERVER1\\SQLEXPRESS;initial catalog=SilverProduction;integrated security=True;MultipleActiveResultSets=True;");


        public List<EmpMasterMV> GeneateEmpID()
        {
            List<EmpMasterMV> item = new List<EmpMasterMV>();
            con.Open();
            SqlCommand cmd = new SqlCommand("Sp_Lastinsert_Emp_ID", con);
            cmd.CommandType = CommandType.StoredProcedure;
            SqlDataReader rdr = cmd.ExecuteReader();
          
            while (rdr.Read())
            {
                var details = new EmpMasterMV();
                details.EnrollNumber = int.Parse(rdr["EnrollNumber"].ToString());
      
                item.Add(details);
            }
            con.Close();

            return item;
        }
    }

and Calling above class in Controller

 public class EmployeeController : Controller
    {
        SilverProductionEntities DB = new SilverProductionEntities();
        RecordID sdb = new RecordID();
        // GET: Employee
     
   
        public ActionResult CreaterEmployee()
        {
            
          
             empmastmv.EnrollNumbera =(sdb.GeneateEmpID());


            return View(empmastmv);

                }
        [HttpPost]
        [ValidateAntiForgeryToken]
        public ActionResult CreaterEmployee(EmpMasterMV empmastmv)
        {
            if (ModelState.IsValid)
            {
                var CheckEmployee = DB.EmpMasters.Where(u => u.EmpName == empmastmv.EmpName.Trim() && u.FatherName == empmastmv.FatherName).FirstOrDefault();
                if (CheckEmployee == null)
                {
                    var newemployee = new EmpMaster();
                    newemployee.EnrollNumber = empmastmv.EnrollNumber;
          


                    DB.EmpMasters.Add(newemployee);
                    DB.SaveChanges();
                    return RedirectToAction("AllEmployeeList");
                }
                else

                {
                    ModelState.AddModelError("Employee", "Already Exists..!");
                }

            }
            return View(empmastmv);
        } 

View

@model ERP_APP.Models.EmpMasterMV

@{
    ViewBag.Title = "CreaterEmployee";
}


<div class="col-lg-8">
    <div class="card card-default mb-5">
        <div class="card-header">Employee Form</div>
        <div class="card-body">

            @using (Html.BeginForm("CreaterEmployee", "Employee", FormMethod.Post))

            {
                @Html.AntiForgeryToken()

                <div class="form-horizontal">


                    <hr />
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.LabelFor(model => model.EnrollNumber, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.EnrollNumber, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.EnrollNumber, "", new { @class = "text-danger" })
                        </div>
                    </div>

                

                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Create" class="btn btn-default" />
                        </div>
                    </div>
                </div>
            }

            <div>
                @Html.ActionLink("Back to List", "AllEmployeeList")
            </div>
            </div></div></div>

when inserting into database ,then 0 Value is getting insert

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,326 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,414 questions
0 comments No comments
{count} votes

Accepted answer
  1. Viorel 113.3K Reputation points
    2023-02-25T08:29:45.1633333+00:00

    How did you check the value of inserted EnrollNumber? To verify that DB.SaveChanges() works, execute something like newemployee.EnrollNumber = 1234 before saving, then check the database.

    Also check if this modification helps:

    public ActionResult CreaterEmployee()
    {
       return View(sdb.GeneateEmpID()[0]);
    }
    

    or this:

    public ActionResult CreaterEmployee()
    {
       empmastmv.EnrollNumbera = sdb.GeneateEmpID();
       empmastmv.EnrollNumber = empmastmv.EnrollNumbera[0].EnrollNumber;
    
       return View(empmastmv);
    }
    

    Maybe EnrollNumbera is not needed.

    1 person found this answer helpful.
    0 comments No comments

0 additional answers

Sort by: Most helpful