0

enter image description here

enter image description here

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();

            }


        }
    }
}

  • 1
    Add a breakpoint on the return and see what is in the object before it gets returned. – NorSer Nov 15 '22 at 15:35
  • 1
    Welcome to Stack Overflow! Please edit your question to [include code, data and error messages as text, not images](https://meta.stackoverflow.com/questions/285551/why-not-upload-images-of-code-errors-when-asking-a-question/285557#285557). It looks like the model being returned to the view from the controller is null, but without knowing which of the methods in the controller is being called I can't say much more. – sbridewell Nov 15 '22 at 17:06
  • You have some `View()` calls which don't have a model argument. When this call is executed, your view is displayed without any model data. Therefore, your `foreach` will result in a NRE. See https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it – Progman Nov 15 '22 at 18:47

0 Answers0