Please Help me. I am new in MVC and 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.
Note : DD BInding from db is working fine in this code
View Part :
SchoolManagementSystem.Models.studentmodel
@{
ViewBag.Title = "Add Student";
Layout = "~/Views/_dashboardLayout.cshtml";
}
<div class="wrapper">
<div class="row">
<div class="col-md-12">
<section class="panel tb">
<header class="panel-heading">
Add Student
<span class="tools pull-right">
Admin/Dashboard/Add New Student
</span>
</header>
@using (Html.BeginForm("AddStudent", "Admin", FormMethod.Post
, new { enctype = "multipart/form-data" }))
{ <div class="panel-body ">
<div class="adv-table col-md-8">
<div class="col-md-4"><span style=" color:black">Name</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.studentname, new { @placeholder = "Enter Student Name", @class = "textbox" })
@Html.ValidationMessageFor(a => a.studentname)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Guardian Name</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.guardianname, new { @placeholder = "Enter Guardian Name", @class = "textbox" })
@Html.ValidationMessageFor(a => a.guardianname)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Gender</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.gender, new { @placeholder = "Enter Gender Name", @class = "textbox" })
@Html.ValidationMessageFor(a => a.gender)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Religion</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.religion, new { @placeholder = "Enter Student Religion", @class = "textbox" })
@Html.ValidationMessageFor(a => a.religion)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Email</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.email, new { @placeholder = "Enter Student Email", @class = "textbox" })
@Html.ValidationMessageFor(a => a.email)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Phone</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.phone, new { @placeholder = "Enter Student Mobile No;If any", @class = "textbox" })
@Html.ValidationMessageFor(a => a.phone)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Student Current Address</span></div>
<div class="col-md-4">
@Html.TextAreaFor(a => a.address, new { @placeholder = "Enter Student Current Address", @class = "textbox" })
@Html.ValidationMessageFor(a => a.address)
</div>
</div>
<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.classlistid, Model.classlistvalue, "<--select-->", new { @class = "textbox" })
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Section</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.section, new { @placeholder = "Enter Student Class's Section", @class = "textbox" })
@Html.ValidationMessageFor(a => a.section)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Roll No</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.rollno, new { @placeholder = "Enter Student Roll No", @class = "textbox" })
@Html.ValidationMessageFor(a => a.rollno)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Student's Photo</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.studpic, new { type = "file" })
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">User Name</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.username, new { @placeholder = "Enter User Name", @class = "textbox" })
@Html.ValidationMessageFor(a => a.username)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Password</span></div>
<div class="col-md-4">
@Html.PasswordFor(a => a.password, new { @placeholder = "Enter Password", @class = "textbox" })
@Html.ValidationMessageFor(a => a.password)
</div>
</div>
<div style =" margin-top:15px" class="col-md-12">
<header class="panel-heading">
Parents Detail
</header>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Father's Name</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.fname, new { @placeholder = "Enter Student's Father Name", @class = "textbox" })
@Html.ValidationMessageFor(a => a.fname)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Mother's Name</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.mname, new { @placeholder = "Enter Student's Mother Name", @class = "textbox" })
@Html.ValidationMessageFor(a => a.mname)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Father's Profesion</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.fprofession, new { @placeholder = "Enter father's Profession", @class = "textbox" })
@Html.ValidationMessageFor(a => a.fprofession)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Mother's Profesion</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.mprofession, new { @placeholder = "Enter Mother's Profession", @class = "textbox" })
@Html.ValidationMessageFor(a => a.mprofession)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Email</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.pemail, new { @placeholder = "Enter Father/Mother's Email", @class = "textbox" })
@Html.ValidationMessageFor(a => a.pemail)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Father's Phone</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.pphone1, new { @placeholder = "Enter Father's Email", @class = "textbox" })
@Html.ValidationMessageFor(a => a.pphone1)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Mother's Phone</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.pphone2, new { @placeholder = "Enter Mother's Phone", @class = "textbox" })
@Html.ValidationMessageFor(a => a.pphone2)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Permanent Address</span></div>
<div class="col-md-4">
@Html.TextAreaFor(a => a.paddress, new { @placeholder = "Enter Student Permanent Address", @class = "textbox" })
@Html.ValidationMessageFor(a => a.paddress)
</div>
</div>
<div style=" margin-top:15px" class="col-md-12">
<header class="panel-heading">
School Detail
</header>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Last School Name</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.lastschool, new { @placeholder = "Enter Previous School's Name", @class = "textbox" })
@Html.ValidationMessageFor(a => a.lastschool)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Highest Education</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.lastclass, new { @placeholder = "Enter Highest Education", @class = "textbox" })
@Html.ValidationMessageFor(a => a.lastclass)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4"><span style=" color:black">Percentage</span></div>
<div class="col-md-4">
@Html.TextBoxFor(a => a.percent, new { @placeholder = "Enter Percentage", @class = "textbox" })
@Html.ValidationMessageFor(a => a.percent)
</div>
</div>
<div style=" margin-top:15px" class="col-md-8">
<div class="col-md-4">
</div>
<div class="col-md-4" align="right">
<input id="bigbutton" type="submit" value="Add Student" />
</div>
</div>
</div>
}
@{
if (ViewData["result"] != "" && ViewData["result"] != null)
{
ViewData["result"] = null;
<script type="text/javascript" language="javascript">
alert("Data saved Successfully");
</script>
}
}
</section>
</div>
</div>
</div>
Code Part :
public string AddStudent(studentmodel MD)
{
string pic1 = null;
if (MD.studpic != null)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(MD.studpic.FileName);
string fileExtension = Path.GetExtension(MD.studpic.FileName);
fileNameWithoutExtension = DateTime.Now.ToString("ddMMyyhhmmsstt") + "-cityindicator";
pic1 = fileNameWithoutExtension + fileExtension;
string targetPath = HttpContext.Current.Server.MapPath("~/uploadpic/" + pic1);
Stream strm = MD.studpic.InputStream;
var targetFile = targetPath;
//Based on scalefactor image size will vary
admission.GenerateThumbnails(0.6, strm, targetFile);
}
SqlConnection con = null;
string result = "";
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;
}
Modal Part :
using System;
using System.Collections.Generic;
using System.Linq;
using System.ComponentModel.DataAnnotations;
using System.Data;
using System.Web;
using System.Web.Mvc;
using System.Collections;
namespace SchoolManagementSystem.Models
{
public class studentmodel
{
[Required(ErrorMessage = "Please Enter Student Name")]
[Display(Name = "Enter Student Name")]
public string studentname { get; set; }
[Required(ErrorMessage = "Please Enter Guardian Name")]
[Display(Name = "Enter Guardian Name")]
public string guardianname { get; set; }
[Required(ErrorMessage = "Please Choose Student Gender")]
[Display(Name = "Choose Gender")]
public string gender { get; set; }
[Required(ErrorMessage = "Please Enter Religion")]
[Display(Name = "Choose Religion")]
public string religion { get; set; }
[Required(ErrorMessage = "Please Enter Email")]
[Display(Name = "Please Enter Email")]
public string email { get; set; }
[Required(ErrorMessage = "Please Enter Phone Number")]
[Display(Name = "Please Enter Phone Number")]
[StringLength(12, MinimumLength = 10, ErrorMessage = "Mobile Number Must Be 10 Digit!")]
public string phone { get; set; }
[Required(ErrorMessage = "Please Enter Current Address")]
[Display(Name = "Please Enter Address")]
public string address { get; set; }
[Required(ErrorMessage = "Please Choose Section")]
[Display(Name = "Please Choose section")]
public string section { get; set; }
[Required(ErrorMessage = "Please Enter Roll No")]
[Display(Name = "Please Enter Roll No")]
public string rollno { get; set; }
public HttpPostedFileBase studpic { get; set; }
[Required(ErrorMessage = "Please User Name")]
[StringLength(25, MinimumLength = 6, ErrorMessage = "User Name Must be Between 6 and 25 digit!")]
[Display(Name = "Please Enter User Name")]
public string username { get; set; }
[Required(ErrorMessage = "Please Enter Password")]
[StringLength(15, MinimumLength = 3, ErrorMessage = "Password Must be Between 3 and 15 digit!")]
[Display(Name = "Please Enter Password")]
public string password { get; set; }
[Required(ErrorMessage = "Please Enter Father Name")]
[Display(Name = "Enter Father Name")]
public string fname { get; set; }
[Required(ErrorMessage = "Please Enter Mother Name")]
[Display(Name = "Enter Mother Name")]
public string mname { get; set; }
[Required(ErrorMessage = "Please Enter Father Profession")]
[Display(Name = "Enter Father Profession")]
public string fprofession { get; set; }
[Required(ErrorMessage = "Please Enter Mother Profession")]
[Display(Name = "Enter Mother Profession")]
public string mprofession { get; set; }
[Required(ErrorMessage = "Please Enter Email")]
[Display(Name = "Enter Email")]
public string pemail { get; set; }
[Required(ErrorMessage = "Please Enter Phone 1")]
[Display(Name = "Enter Phone1")]
public string pphone1 { get; set; }
[Display(Name = "Enter Phone 2")]
public string pphone2 { get; set; }
[Required(ErrorMessage = "Please Enter Permanent Address")]
[Display(Name = "Enter Permanent Address")]
public string paddress { get; set; }
[Required(ErrorMessage = "Please Enter Last School Name")]
[Display(Name = "Enter Last School")]
public string lastschool { get; set; }
[Required(ErrorMessage = "Please Enter Last Attended Class")]
[Display(Name = "Enter Last Attended Class")]
public string lastclass { get; set; }
[Required(ErrorMessage = "Please Enter Percentage in Last Class")]
[Display(Name = "Enter Percentage")]
public string percent { get; set; }
[Required(ErrorMessage = "Please Enter Class")]
[Display(Name = "Enter Class")]
public string classlistid { get; set; }
public List<SelectListItem> classlistvalue
{
get;
set;
}
}
}
Controller Part :
public ActionResult AddStudent()
{
studentmodel model = new studentmodel();
model.classlistvalue = new List<SelectListItem>();
using (SqlConnection conn = new SqlConnection(connection))
{
try
{
conn.Open();
SqlDataReader myReader = null;
SqlCommand myCommand = new SqlCommand("SELECT classnumeric,newclass FROM class order by classnumeric desc", conn);
myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
model.classlistvalue.Add(new SelectListItem { Text = myReader["newclass"].ToString(), Value = myReader["classnumeric"].ToString() });
}
}
catch (Exception e)
{
string msg = e.Message;
}
finally
{
conn.Close();
}
}
// Create a list of SelectListItems so these can be rendered on the page
return View(model);
}
[HttpPost]
public ActionResult AddStudent(studentmodel MB,FormCollection formCollection) // Calling on http post (on Submit)
{
MB.classlistid = formCollection["classlistid"];
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();
}
}