1
Reply

How to Insert Dropdownlist Select Value in DataBase...

Cityindicator.Com

Cityindicator.Com

Feb 26 2016 2:28 AM
322
 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 : 
 
<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:
 
[Required(ErrorMessage = "Please Enter Class")]
[Display(Name = "Enter Class")]
public string classlistid { get; set; }
public List<SelectListItem> classlistvalue
{
get;
set;
}
 
Controller: 
 
 
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
cityindicator.com 
 

Answers (1)