System.NullReferenceException: 'Object reference not set to an instance of an object.' model was null

Naveinthiran 20 Reputation points
2024-06-21T09:37:52.91+00:00

sdafdasf

model

{

public int VehicleID { get; set; }

public int VehicleTypeID { get; set; }

public string Brand { get; set; }

public string Model { get; set; }

public int Capacity { get; set; } = 0;

public bool IsActive { get; set; }

}

controller

// GET: Vehicle

[AccessRights(AccessKey = "VE")]

public ActionResult Vehicle()

{

    DataSet tempDS = new DataSet();

    string __ConnectionString = ConfigurationManager.ConnectionStrings["MySqlConnector"].ConnectionString;

    tempDS = SelectDataSet(__ConnectionString, "SELECT * FROM vehicle WHERE IsActive = 1");

    var ds = tempDS;

    ViewBag.dataSource = ds.Tables[0];

    ViewBag.Search = true;

    return View();

}

[AccessRights(AccessKey = "VE")]

public ActionResult VehicleInsert(VehicleModel model)

{

    try

    {

        string baseSql = "INSERT INTO `vehicle` (`Brand`, `Model`, `IsActive`) VALUES (@brand, @model, @isActive);";

        Dictionary<string, object> param = new Dictionary<string, object>

        {

            { "@brand", model.Brand },

            { "@model", model.Model },

            { "@isActive", model.IsActive }

        };

        DB2.ExecuteNonQueryEscape(baseSql, param);

    }

    catch (Exception ex)

    {

        return Json(new { success = false, error = ex.Message });

    }

    return Json(new { success = true });

}

[HttpGet]

[AccessRights(AccessKey = "VE")]

public ActionResult EditVehicle(int ID)

{

    DataSet tempDS = new DataSet();

    string __ConnectionString = ConfigurationManager.ConnectionStrings["MySqlConnector"].ConnectionString;

    string query = $"SELECT * FROM vehicle WHERE VehicleID=" + ID + ";";

    tempDS = SelectDataSet(__ConnectionString, query);

    var ds = tempDS;

    VehicleModel model = new VehicleModel();

    foreach (DataRow row in ds.Tables[0].Rows)

    {

        model = (DBObjectMapper.Map<VehicleModel>(row));

    }

    return PartialView(model);

}

[AccessRights(AccessKey = "VE")]

public ActionResult VehicleUpdate(VehicleModel model)

{

    try

    {

        if (model.VehicleID != 0)

        {

            string baseSql = "UPDATE `vehicle` SET `Brand` = @brand, `Model` = @model, `IsActive` = @isActive WHERE `VehicleID` = @vehicleId";

            Dictionary<string, object> param = new Dictionary<string, object>

            {

                { "@brand", model.Brand },

                { "@model", model.Model },

                { "@isActive", model.IsActive }

            };

            DB2.ExecuteNonQueryEscape(baseSql, param);

        }

        else

        {

            return Json(new { success = false, error = "ID cannot be null" });

        }

    }

    catch (Exception ex)

    {

        return Json(new { success = false, error = ex.Message });

    }

    return Json(new { success = true });

}

[AccessRights(AccessKey = "VE")]

public ActionResult VehicleDelete(VehicleModel model)

{

    try

    {

        if (model.VehicleID != 0)

        {

            string baseSql = "UPDATE `vehicle` SET `IsActive` = @isActive WHERE `VehicleID` = @vehicleId;";

            Dictionary<string, object> param = new Dictionary<string, object>()

            {

                { "@vehicleId", model.VehicleID },

                { "@isActive", 0 },

            };

            DB2.ExecuteQueryScalarEscape(baseSql, param);

        }

        else

        {

            return Json(new { success = false, error = "ID cannot be null" });

        }

    }

    catch (Exception ex)

    {

        return Json(new { success = false, error = ex.Message });

    }

    return Json(new { success = true });

}
Developer technologies | ASP.NET | ASP.NET Core
Microsoft 365 and Office | Development | Office JavaScript API
Developer technologies | .NET | Other
Developer technologies | Visual Studio | Other
Developer technologies | Visual Studio | Other
A family of Microsoft suites of integrated development tools for building applications for Windows, the web, mobile devices and many other platforms. Miscellaneous topics that do not fit into specific categories.
Developer technologies | C#
Developer technologies | 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.
{count} votes

Answer accepted by question author
  1. AgaveJoe 30,491 Reputation points
    2024-06-21T18:30:38.4866667+00:00

    Change the "model" to something else like "vm". The name "model" is causing a naming conflict with the property named "model"

    public ActionResult VehicleInsert(VehicleModel vm)
    {
    
    1 person found this answer helpful.

2 additional answers

Sort by: Most helpful
  1. SurferOnWww 4,951 Reputation points
    2024-06-21T11:00:49.32+00:00

    Use Fiddler to see if the data are properly sent from client to server.

    Use debugger of Visual Studio to see if the data sent from client are properly bound to the model of the action method VehicleInsert(VehicleModel model).

    1 person found this answer helpful.

  2. Soaad Nahas 0 Reputation points
    2024-06-21T17:47:56.88+00:00

    شكرا للتواصل

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.