public HttpResponseMessage CreateStudent(Student student) { var checkid = dbcontext.Students.Where(st_id => st_id.StudentID == student.StudentID).Any(); if (!checkid) { dbcontext.Students.Add(student); try { dbcontext.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, student); response.StatusCode = HttpStatusCode.Created; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return response; } HttpResponseMessage res = Request.CreateResponse(HttpStatusCode.OK, "Not Found"); return res; }
public HttpResponseMessage UpdateStudent(Student student) { if (student != null) { dbcontext.Entry(student).State = EntityState.Modified; } try { dbcontext.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK, student); response.StatusCode = HttpStatusCode.Created; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return response; }
public HttpResponseMessage CreateStudent(Student student) { dbcontext.Students.Add(student); try { dbcontext.SaveChanges(); } catch (Exception ex) { return Request.CreateErrorResponse(HttpStatusCode.NotFound, ex); } HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.Created, student); response.StatusCode = HttpStatusCode.Created; response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); return response; }
private void LoadStudent(HttpPostedFileBase file) { using (var package = new ExcelPackage(file.InputStream)) { var studentworkSheet = package.Workbook.Worksheets["students"]; var studentnoOfRow = studentworkSheet.Dimension.End.Row; for (int rowIterator = 2; rowIterator <= studentnoOfRow; rowIterator++) { Student student = new Student(); student.StudentID = int.Parse(studentworkSheet.Cells[rowIterator, 1].Value.ToString()); student.Firstname = studentworkSheet.Cells[rowIterator, 2].Value.ToString(); student.Lastname = studentworkSheet.Cells[rowIterator, 3].Value.ToString(); student.Phoneno = studentworkSheet.Cells[rowIterator, 4].Value.ToString(); student.Course = studentworkSheet.Cells[rowIterator, 5].Value.ToString(); student.Email = studentworkSheet.Cells[rowIterator, 6].Value.ToString(); dbcontext.Students.Add(student); dbcontext.SaveChanges(); } } }