Click here to Skip to main content
15,303,522 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Please Help me. I am trying to post dropdown value in database but i got error null value reference ,I am trying all Possible available code on google including c# corner,code project ,stack over flow but i got same error again and again i cant understand what i am missing.

View Part :

HTML
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Class</span></div>
<div class="col-md-4">
@Html.DropDownListFor(m => m.classlistvalue, Model.classlistvalue, "<--select-->", new { @class = "textbox" })
</div>
</div>


Model Class:

C#
[Required(ErrorMessage = "Please Enter Class")]
[Display(Name = "Enter Class")]

public string classlistid { get; set; }
public List<SelectListItem> classlistvalue
{
get;
set;
}


Controller:


C#
public ActionResult AddStudent(studentmodel MB,FormCollection formCollection) // Calling on http post (on Submit)
{

MB.classlistid = formCollection["classlistvalue"];

if (ModelState.IsValid)
{

DataLayer.student objDB = new DataLayer.student(); //calling class Dbdata
string result = objDB.AddStudent(MB); // passing Value to DBClass from model
ViewData["result"] = result; // for dislaying message after saving storing output.
ModelState.Clear(); //clearing model
return View();

}

else
{
ModelState.AddModelError("", "Error in saving data");
return View();
}
}
 Posting Metod (DataLaye):
con = new SqlConnection(ConfigurationManager.ConnectionStrings["mycon"].ToString());
SqlCommand cmd = new SqlCommand("AddStudent", con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@studentname", MD.studentname);
cmd.Parameters.AddWithValue("@gdname", MD.guardianname);
cmd.Parameters.AddWithValue("@gender", MD.gender);
cmd.Parameters.AddWithValue("@religion", MD.religion);
cmd.Parameters.AddWithValue("@email", MD.email);
cmd.Parameters.AddWithValue("@phone", MD.phone);
cmd.Parameters.AddWithValue("@studaderess", MD.address);
cmd.Parameters.AddWithValue("@class", MD.classlistid);
cmd.Parameters.AddWithValue("@section", MD.section);
cmd.Parameters.AddWithValue("@rollno", MD.rollno);
cmd.Parameters.AddWithValue("@studpic", "~/uploadpic/" + pic1);
cmd.Parameters.AddWithValue("@username", MD.username);
cmd.Parameters.AddWithValue("@pwd", MD.password);
cmd.Parameters.AddWithValue("@fname", MD.fname);
cmd.Parameters.AddWithValue("@mname", MD.mname);
cmd.Parameters.AddWithValue("@fprofession", MD.fprofession);
cmd.Parameters.AddWithValue("@mprofession", MD.mprofession);
cmd.Parameters.AddWithValue("@pemail", MD.pemail);
cmd.Parameters.AddWithValue("@mob1", MD.pphone1);
cmd.Parameters.AddWithValue("@mob2", MD.pphone2);
cmd.Parameters.AddWithValue("@permaddress", MD.paddress);
cmd.Parameters.AddWithValue("@lastschool", MD.lastschool);
cmd.Parameters.AddWithValue("@highesteducation", MD.lastclass);
cmd.Parameters.AddWithValue("@percentage", MD.percent);
con.Open();
result = cmd.ExecuteScalar().ToString();
return result;


Thanks and Regards
Mohd Kashif
Posted
Updated 25-Feb-16 22:54pm
v3
Comments
F-ES Sitecore 26-Feb-16 3:30am
   
What line do you get the error on?

Read this, and search this
[no name] 26-Feb-16 4:11am
   
You should get doropdown value in formcollection object. So where is there error?
John C Rayan 26-Feb-16 5:02am
   
Your DropDownListFor is wrong in the view.
It should be like
@Html.DropDownListFor(m => m.classlistid, Model.classlistvalue, "<--select-->", new { @class = "textbox" })

In your controller

MB.classlistid = formCollection["classlistid"];

Let me know you model class in your view too. Try this and let me know.
cityindicator.com 26-Feb-16 8:39am
   
Hello ,
Thanks for reply,
but i got same error "An exception of type 'System.NullReferenceException' occurred in App_Web_o33c4lci.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object."
John C Rayan 26-Feb-16 9:00am
   
Can you show me the code for DDL in view and the code in controller for getting the classlistid from formcollection

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900