0

Possible Duplicate:
What is a NullReferenceException in .NET?

I am a beginner in MVC and I have tried to add a dropdown list to save its selected value in database using sql queries but my code throws a NullReferenceException.

Can anyone help me please?

This is the model

public class caradvert
{
    [Required]
    public SelectList GearType { get; set; }

    public int Selected { get; set; }

public caradvert()
{
    List<SelectListItem> listItems = new List<SelectListItem>();
    listItems.Add(new SelectListItem()
    {
        Value = "0",
        Text = "اتوماتيك "
    });


    listItems.Add(new SelectListItem()
    {
        Value = "1",
        Text = "عادي"
    });

        GearType = new SelectList(listItems, "Value", "Text");
}

public int CreatAdvert(int userid)
    {

        SqlConnection objConn = new SqlConnection("Data Source=ADMIN-PC;Initial Catalog=mvc4advertisment;Integrated Security=True");
        SqlCommand objCmd = new SqlCommand();
        objCmd.CommandType = CommandType.Text;
        objCmd.CommandText = "insert into Mercedes(Model,GearType,color,EngineCapacity,CarDescription,Price,Image1Url,Image2Url,Image3Url,Userid,State) values('1','" + GearType.SelectedValue.ToString() + "','r','221','ddd','2','ss','ss','ss','1','False')";
        objCmd.Connection = objConn;
        objConn.Open();
        int count = (int)objCmd.ExecuteNonQuery();
        objConn.Close();
        return count;
    }
}

This is controller

   [HttpGet]
    public ActionResult CreateAdvert()
    {

        caradvert model = new caradvert();
        List<SelectListItem> listItems = new List<SelectListItem>();
        listItems.Add(new SelectListItem()
        {
            Value = "1",
            Text = "اتوماتيك "
        });


        listItems.Add(new SelectListItem()
        {
            Value = "1",
            Text = "عادي"
        });
        model.GearType = new SelectList(listItems, "Value", "Text");



        return View(model);


    }

    [HttpPost]
    public ActionResult CreateAdvert(caradvert model )
    {
                int _records = model.CreatAdvert(1);
                if (_records > 0)
                {
                    return RedirectToAction("Index", "Account");
                }
                else
                {
                    ModelState.AddModelError("", "لا يمكنك اضافة اعلان");
                }

        return View(model);

    }

This is the view

<%:Html.DropDownListFor(m=>m.Selected,Model.GearType,") %>
Community
  • 1
  • 1

2 Answers2

0

Most likely the GearType or GearType.SelectedValue are null in this statement.

objCmd.CommandText = "insert into Mercedes(Model,GearType,color,EngineCapacity,CarDescription,Price,Image1Url,Image2Url,Image3Url,Userid,State) values('1','" + GearType.SelectedValue.ToString() + "','r','221','ddd','2','ss','ss','ss','1','False')";
Marius Bancila
  • 16,053
  • 9
  • 49
  • 91
0

at this line:

objCmd.CommandText = "insert into Mercedes(Model,GearType,color,EngineCapacity,CarDescription,Price,Image1Url,Imag‌​e2Url,Image3Url,Userid,State) values('1','" + GearType.SelectedValue.ToString() + "','r','221','ddd','2','ss','ss','ss','1','False')"; 
  1. If GearType is not initialized or null
  2. If GearType.SelectedValue is null

GearType.SelectedValue.ToString() can throw an exception like reference not set to an instance of an object.

To make problem more clear you can control GearType and GearType.SelectedValue before this line.

if(GearType != null && GearType.SelectedValue != null) {
 Mercedes(Model,GearType,color,EngineCapacity,CarDescription,Price,Image1Url,Imag‌​e2Url,Image3Url,Userid,State) values('1','" + GearType.SelectedValue.ToString() + "','r','221','ddd','2','ss','ss','ss','1','False')";
}
Matteo
  • 1,107
  • 2
  • 27
  • 47
aaltintop
  • 188
  • 9