public ActionResult Create(Volunteer volunteer) { if (ModelState.IsValid) { db.Volunteers.Add(volunteer); db.SaveChanges(); return RedirectToAction("Index"); } return View(volunteer); }
public ActionResult Index(HttpPostedFileBase fUpload) { OleDbConnection objConn = null; OleDbCommand objCmd = null; OleDbDataAdapter objAdapter = null; DataSet ds = null; if (fUpload != null) { try { string sPath = System.IO.Path.Combine(Server.MapPath("~/Downloads"), System.IO.Path.GetFileName(fUpload.FileName)); fUpload.SaveAs(sPath); ViewBag.Message = "File upload Successful!"; // EXCEL IMPORT //string sConn = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + sPath + "; Extended Properties=Excel 8.0;"; //using (objConn = new System.Data.OleDb.OleDbConnection(sConn)) //{ // objConn.Open(); // objCmd = new OleDbCommand("Select * From [My Attendees$]", objConn); // objAdapter = new OleDbDataAdapter(objCmd); // ds = new DataSet("Attendees"); // objAdapter.Fill(ds); // objConn.Close(); //} // CSV OR PLAIN TEXT IMPORT //string sLine = string.Empty; //System.IO.StreamReader sReader = new System.IO.StreamReader(sPath); //while ((sLine = sReader.ReadLine()) != null) //{ // //Response.Write(sLine); //} //sReader.Close(); //sReader = null; //CSV IMPORT USING OLEDB FOR DATASET string sConn = "Provider=Microsoft.Jet.OleDb.4.0; Data Source=" + Server.MapPath("~/Downloads") + "; Extended Properties=text;"; using (objConn = new System.Data.OleDb.OleDbConnection(sConn)) { objConn.Open(); objCmd = new OleDbCommand("Select * From [" + System.IO.Path.GetFileName(fUpload.FileName) + "]", objConn); objAdapter = new OleDbDataAdapter(objCmd); ds = new DataSet("Attendees"); objAdapter.Fill(ds); objConn.Close(); } //GCBLL.VolunteerImport objVolunteer = new GCBLL.VolunteerImport(); //objVolunteer.ImportVolunteerInformation(ds); foreach (DataRow dRow in ds.Tables[0].Rows) { Volunteer objVol = new Volunteer(); objVol.FirstName = dRow["First name"].ToString(); objVol.LastName = dRow["Last name"].ToString(); objVol.SignUpPartyId = dRow["Attendee #"].ToString(); if (dRow["Date"] != null) objVol.SignUpDate = (Convert.ToDateTime(dRow["Date"])); Contact objContact = new Contact(); objContact.Email = dRow["Email"].ToString(); objContact.PhoneNumber = dRow["Cell Phone"].ToString(); //var updateVol = db.Volunteers.Where(n => n.SignUpPartyId == objVol.SignUpPartyId); var id = (from Volunteer in db.Volunteers.Where(n => n.SignUpPartyId == objVol.SignUpPartyId) select Volunteer.VolunteerId).SingleOrDefault(); if ((id != null) && (id > 0)) { Volunteer volunteer = db.Volunteers.Find(Int32.Parse(id.ToString())); db.Volunteers.Remove(volunteer); } var volResult = db.Volunteers.Add(objVol); var contactResult = db.Contacts.Add(objContact); ContactLink obj = new ContactLink() { Volunteer = volResult, Contact = contactResult }; //bool bValid = false; //foreach (Volunteer vol in db.Volunteers) //{ // var valid = db.Volunteers.Select(n => vol.SignUpPartyId); // if (vol.SignUpPartyId != objVol.SignUpPartyId) // { // bValid = true; // break; // } //} //if (!(Helpers.VolunteerHelper.hasVolunteer(objVol, db.Volunteers.ToList()))) //{ // var volResult = db.Volunteers.Add(objVol); // var contactResult = db.Contacts.Add(objContact); // ContactLink obj = new ContactLink() // { // Volunteer = volResult, // Contact = contactResult // }; //} //else //{ // if (ModelState.IsValid) // { // db.Entry(objVol).State = EntityState.Modified; // db.Entry(objVol).CurrentValues.SetValues(objVol); // } //} } db.SaveChanges(); } catch (Exception ex) { ViewBag.Message = ex.Message; } finally { if ((objConn != null)) { objConn.Close(); } } } return View(db.Volunteers.ToList()); }
public ActionResult Edit(Volunteer volunteer) { if (ModelState.IsValid) { db.Entry(volunteer).State = EntityState.Modified; db.SaveChanges(); return RedirectToAction("Index"); } return View(volunteer); }