-1

I had the problem of the edit function adding a new entry instead of updating the data but now that I have changed the code it gives me the error as below:

object reference not set to an instance of an object

I have already tried returning the view model but it still didn't work as well as changing the code in the repository but still couldn't get it to work. I've already read some of the answers for the same issue but it didn't fix my issue. If you still need information on the program please do tell me, thanks in advance!

Controller:

   //EDIT
    [HttpGet]
    public ActionResult Edit(int id)
    {
        var repo = new ManagementRepository();
        var DVM = new DriverViewModel();

        Driver editDriver = db.Drivers.Find(id);
    //   repo.UpdateDriver(editDriver);
        return View(DVM);
    }

    [HttpPost]
    public ActionResult Edit(DriverViewModel DVM)
    {

        var repo = new ManagementRepository();
        var driver = new Driver();
        using (var db = new VehicleReservationEntities())
        { 
            var existingDriver = (from data in db.Drivers
                                  where data.DriverID == DVM.DriverID
                                  select data).FirstOrDefault();

            existingDriver.DriverID = DVM.DriverID;
            existingDriver.DriverLastName = DVM.DriverLastName;
            existingDriver.DriverFirstName = DVM.DriverFirstName;
            existingDriver.DriverLicense = DVM.DriverLicense;
            existingDriver.LicenseExpiry = DVM.LicenseExpiry;
            existingDriver.MobileNumber = DVM.MobileNumber;
            existingDriver.BusinessUnit = DVM.BusinessUnit;

            repo.UpdateDriver(existingDriver);
        }




        return View(DVM);

    }
}

View Model:

    public class DriverViewModel
{
    public int DriverID { get; set; }
    public string DriverLastName { get; set; }
    public string DriverFirstName { get; set; }
    public string MobileNumber { get; set; }
    public string DriverLicense { get; set; }
    public Nullable<System.DateTime> LicenseExpiry { get; set; }
    public string BusinessUnit { get; set; }
    public bool IsActive { get; set; }
    public System.DateTime DateRegistered { get; set; }

    public string FullName
    {
        get { return DriverLastName + ", " + DriverFirstName; }
    }
}

View:

    @model VehicleReservation.ViewModels.DriverViewModel

@{
    ViewBag.Title = "Edit";
}

@section Jscripts{

    <script type="text/javascript">
        $(function () {
            $("#LicenseExpiry").datepicker({
                changeMonth: true,
                changeYear: true,
                format: "yyyy-MM-dd"

            }
            ).datepicker("setDate", new DateTime());
        });
    </script>

    <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
    <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>

}

<h2>Edit Driver</h2>



<div class="row">
    <div class="col-md-12 ">

        <div class="panel" style="margin-top:3%; margin-bottom: 5%;">
            <div class="panel-heading">
                <div class="panel-title">
                </div>

                <div class="panel-body">

                    @using (Html.BeginForm())
                    {
                        @Html.AntiForgeryToken()

                        @Html.ValidationSummary(true, "", new { @class = "text-danger" })

                        @Html.HiddenFor(model => model.DriverID)
                        @Html.HiddenFor(model => model.DateRegistered)                        @Html.HiddenFor(model => model.DateRegistered)
                        @Html.HiddenFor(model => model.IsActive)


                        <div class="col-md-12">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            Last Name
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.DriverLastName, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.DriverLastName, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            First Name
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.DriverFirstName, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.DriverFirstName, "", new { @class = "text-danger" })

                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            Mobile Number
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.MobileNumber, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.MobileNumber, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                        </div>

                        <div class="col-md-12">
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            Driver's License
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.DriverLicense, new { htmlAttributes = new { @class = "form-control" } })
                                        @Html.ValidationMessageFor(model => model.DriverLicense, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>

                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label">
                                        <label>
                                            License Expiry
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => model.LicenseExpiry, new { htmlAttributes = new { @class = "form-control datepicker", @autocomplete = "off" } })
                                        @Html.ValidationMessageFor(model => model.LicenseExpiry, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                            <div class="col-md-4">
                                <div class="form-group">
                                    <div class="form-group control-label ">
                                        <label>
                                            Business Unit ID
                                        </label>
                                    </div>
                                    <div>
                                        @Html.EditorFor(model => 
    model.BusinessUnit, new { htmlAttributes = new { @class = "form-control", @type = "text", @style = "width:50%" } })
                                        @Html.ValidationMessageFor(model => model.BusinessUnit, "", new { @class = "text-danger" })
                                    </div>
                                </div>
                            </div>
                        </div>




                        <div class="form-group">
                            <div class="col-sm-12">
                                <input type="submit" value="Save" class="btn btn-success" /> |
                                @Html.ActionLink("Back To List", "List", null, new { @class = "btn btn-primary btn-large" })
                            </div>
                        </div>
                    }

                </div>
            </div>

        </div>

    </div>




</div>

Repository:

  public void UpdateDriver(Driver editDriver)
{
              using (var db = new VehicleReservationEntities())
               {
                  // var existingDriver = (from data in db.Drivers
                  //                       where data.DriverID == editDriver.DriverID
                  //                       select data).FirstOrDefault();

                   db.Entry(editDriver).State = EntityState.Modified;
                   db.SaveChanges();
               }

    /*            using (var db = new VehicleReservationEntities())
               {

                   db.Entry(id).State = EntityState.Modified;
                   db.SaveChanges();
               }
   */


}   
Joshua
  • 15
  • 6

1 Answers1

1
        var existingDriver = (from data in db.Drivers
                              where data.DriverID == DVM.DriverID
                              select data).FirstOrDefault();

This line will return "null" if there is no driver with that ID in the DataBase. Which will result in null reference exception later on when you do:

        existingDriver.DriverID = DVM.DriverID;
        existingDriver.DriverLastName = DVM.DriverLastName;
        existingDriver.DriverFirstName = DVM.DriverFirstName;
        existingDriver.DriverLicense = DVM.DriverLicense;
        existingDriver.LicenseExpiry = DVM.LicenseExpiry;
        existingDriver.MobileNumber = DVM.MobileNumber;
        existingDriver.BusinessUnit = DVM.BusinessUnit;
Luka Kovac
  • 45
  • 1
  • 7