Please Help me I am trying to post drop down 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 ...Please provide sample code if possible on [email protected].
View Part :
Class
@Html.DropDownListFor(m => m.classlistvalue, Model.classlistvalue, "<--select-->", new { @class = "textbox" })
Model Class:
[Required(ErrorMessage = "Please Enter Class")]
[Display(Name = "Enter Class")]
C#
public string classlistid { get; set; }
public List classlistvalue
{
get;
set;
}Controller:
Java
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
Code
return View();
}
else
{
ModelState.AddModelError("", "Error in saving data");
return View();
}
} Posting Metod (DataLaye):
SQL
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;Mohd Kashif
cityindicator.com
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.

Zaid Ansari147 pointsPosted Feb 26, 2016, 9:05 AM EST
Dictionary<string, string> droplist = new Dictionary<string, string>(); droplist = ObjBal.DropdownTypeBind("Type","","",""); //Sp Parameter ViewBag.DropdownList = droplist;public Dictionary<string, string> DropdownTypeBind(string flag, string flag1, string flag2, string flag3) { return ExcuteDropdown("Sp_MasterOfMaster", CommandType.StoredProcedure,protected Dictionary<string, string> ExcuteDropdown(string _SqlQuery,CommandType _command,params SqlParameter[] Param) { SqlCommand cmd = new SqlCommand(); Dictionary<string, string> dis = new Dictionary<string, string>(); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(); try { cmd.CommandText = _SqlQuery; cmd.CommandType = _command; cmd.Connection = con; cmd.Parameters.Clear(); foreach (var item in Param) { cmd.Parameters.Add(item); } da.SelectCommand = cmd; da.Fill(dt); dis.Add("0", "Seleted type"); for (int i = 0; i <dt.Rows.Count; i++) { //dt.Rows[i][0] ID //dt.Rows[i][0] Values dis.Add(dt.Rows[i][0].ToString(), dt.Rows[i][1].ToString()); } return dis; } catch (Exception ex) { return dis = null; } } If dropdown Bind Then Click on Save Button then apply Breakpoint And you will Get dropdown Selected Value....