I keep getting this error.. The program is to capture data & import excel to the database(SQL) the import excel to database is working but I keep getting this error.
I've doubled check that I returned every model but I keep getting this error.
Controller:
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["BoardExamConnectionstring"].ConnectionString);
OleDbConnection Econ;
private void ExcelConn(string filepath)
{
string constr = string.Format(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties=""Excel 12.0 Xml;HDR=YES;""", filepath);
Econ = new OleDbConnection(constr);
}
private void InsertExceldata(string fileepath, string filename)
{
string fullpath = Server.MapPath("/excelfolder/") + filename;
ExcelConn(fullpath);
string query = string.Format("Select * from [{0}]", "Sheet1$");
OleDbCommand Ecom = new OleDbCommand(query, Econ);
Econ.Open();
DataSet ds = new DataSet();
OleDbDataAdapter oda = new OleDbDataAdapter(query, Econ);
Econ.Close();
oda.Fill(ds);
DataTable dt = ds.Tables[0];
SqlBulkCopy objbulk = new SqlBulkCopy(con);
objbulk.DestinationTableName = "dbo.Student";
objbulk.ColumnMappings.Add("StudentID", "StudentID");
objbulk.ColumnMappings.Add("FirstName", "FirstName");
objbulk.ColumnMappings.Add("LastName", "LastName");
objbulk.ColumnMappings.Add("Program", "Program");
objbulk.ColumnMappings.Add("YearGraduate", "YearGraduate");
objbulk.ColumnMappings.Add("BoardScore", "BoardScore");
con.Open();
objbulk.WriteToServer(dt);
con.Close();
}
StudentDAL _studentDAL = new StudentDAL();
// GET: Student
public ActionResult Index()
{
var studentList = _studentDAL.GetallStudents();
if (studentList.Count == 0)
{
TempData["InfoMessage"] = "Student data unavailable in the Database.";
}
return View(studentList);
}
[HttpPost]
public ActionResult Index(HttpPostedFileBase file)
{
string filename = Guid.NewGuid() + Path.GetExtension(file.FileName);
string filepath = "/excelfolder/" + filename;
file.SaveAs(Path.Combine(Server.MapPath("/excelfolder"), filename));
InsertExceldata(filepath, filename);
return View("index",new MyObject());
}
// GET: Student/Details/5
public ActionResult Details(int id)
{
try
{
var student = _studentDAL.GetStudentByID(id).FirstOrDefault();
if (student == null)
{
TempData["InfoMessage"] = "Student unavailable with ID" + id.ToString();
return RedirectToAction("Index");
}
return View(student);
}
catch (Exception ex)
{
TempData["ErrorMessage"] = ex.Message;
return View();
}
}
// GET: Student/Create
public ActionResult Create()
{
return View();
}
// POST: Student/Create
[HttpPost]
public ActionResult Create(Student student)
{
bool IsInserted = false;
try
{
if (ModelState.IsValid)
{
IsInserted = _studentDAL.InsertStudent(student);
if (IsInserted)
{
TempData["SuccessMessage"] = "Student data saved successfully!";
}
else
{
TempData["ErrorMessage"] = "Unable to save student data.";
}
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
TempData["ErrorMessage"] = ex.Message;
return View();
}
}
// GET: Student/Edit/5
public ActionResult Edit(int id)
{
var students = _studentDAL.GetStudentByID(id).FirstOrDefault();
if (students == null)
{
TempData["InfoMessage"] = "Student unavailable with ID" + id.ToString();
return RedirectToAction("Index");
}
return View(students);
}
// POST: Student/Edit/5
[HttpPost, ActionName("Edit")]
public ActionResult UpdateStudent(Student student)
{
try
{
if (ModelState.IsValid)
{
bool IsUpdated = _studentDAL.UpdateStudent(student);
if (IsUpdated)
{
TempData["SuccessMessage"] = "Student data updated successfully!";
}
else
{
TempData["ErrorMessage"] = "Unable to update student data.";
}
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
TempData["ErrorMessage"] = ex.Message;
return View();
}
}
// GET: Student/Delete/5
public ActionResult Delete(int id)
{
try
{
var student = _studentDAL.GetStudentByID(id).FirstOrDefault();
if (student == null)
{
TempData["InfoMessage"] = "Student unavailable with ID" + id.ToString();
return RedirectToAction("Index");
}
return View(student);
}
catch (Exception ex)
{
TempData["ErrorMessage"] = ex.Message;
return View();
}
}
// POST: Student/Delete/5
[HttpPost, ActionName("Delete")]
public ActionResult DeleteConfirmation(int id)
{
try
{
string result = _studentDAL.DeleteStudent(id);
if (result.Contains("deleted"))
{
TempData["SuccessMessage"] = result;
}
else
{
TempData["ErrorMessage"] = result;
}
return RedirectToAction("Index");
}
catch (Exception ex)
{
TempData["ErrorMessage"] = ex.Message;
return View();
}
}
}
}