public bool Add(StudentServices model) { try { _studenteDbContext.Add(model); _studenteDbContext.SaveChanges(); } catch (System.Exception) { return(false); } return(true); }
public async Task <ActionResult <Student> > CreateAsync([Bind("FirstName,LastName,Program")] StudentBase studentBase) { Student stu = new Student { FirstName = studentBase.FirstName, LastName = studentBase.LastName, Program = studentBase.Program }; _context.Add(stu); await _context.SaveChangesAsync(); return(CreatedAtAction(nameof(GetById), new { id = stu.ID }, stu)); }
public IActionResult AddStudent(string name, int classworkAvg) { // create new student object from params StudentModel newStudent = new StudentModel() { name = name, classworkAvg = classworkAvg }; // add new student to StudentDbContext DbSet _context.Add(newStudent); // save changes to database (add record) _context.SaveChanges(); // pass db context to specificed view return(View("ViewAll", _context)); }
public async Task <bool> AddAsync(Student student) { try { _context.Add(student); var newStudent = await _context.SaveChangesAsync(); return(newStudent == 1); } catch (Exception) { return(false); } }
public async Task <IActionResult> AddOrEdit([Bind("Id,FullName,RegistrationNo,DepartmentId,Roll")] Student student) { if (ModelState.IsValid) { if (student.Id == 0) { _context.Add(student); } else { _context.Update(student); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(student)); }
public async Task <IActionResult> AddOrEdit([Bind("Id,Name,Code")] Department department) { if (ModelState.IsValid) { if (department.Id == 0) { _context.Add(department); } else { _context.Update(department); } await _context.SaveChangesAsync(); return(RedirectToAction(nameof(Index))); } return(View(department)); }
private void saveBtn_Click(object sender, EventArgs e) { _dateTime = dateTimePicker1.Value; _dateTime = DateTime.Parse(_dateTime.ToShortDateString()); bool newPresence = GetPresence() == false; if (presences.ContainsKey(absenceTypeBox.Text)) { Presence.Type = presences[absenceTypeBox.Text]; try { using (StudentDbContext db = new StudentDbContext()) { db.Attach(_student); if (newPresence) { _student.Presences.Add(Presence); Presence.Date = _dateTime; Presence.Student = _student; db.Add(Presence); } else { db.Presences.Update(Presence); } db.SaveChanges(); Console.WriteLine(Presence.ToString()); } } catch (Exception exception) { Console.WriteLine(exception); MessageBox.Show("Prezenci nebylo možné uložit.", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); } Close(); } else { MessageBox.Show("Prezence " + absenceTypeBox.SelectedText + " neexistuje!", "Chyba", MessageBoxButtons.OK, MessageBoxIcon.Error); } }
public void postData(Student studentobj) { _db.Add(studentobj); _db.SaveChanges(); }
public async Task <bool> InsertStudentAsync(Student student) { _dbContext.Add(student); return(await _dbContext.SaveChangesAsync() > 0); }
public async Task Create(Student student) { _context.Add(student); await _context.SaveChangesAsync(); }
public bool EnrollStudent(EnrollStudentRequest request) { try { var studies = _context.Studies.Find(request.Studies); var student = _context.Student.Find(request.IndexNumber); //sprawdzanie czy jest dany student if (student == null) { var tmp = new Student(); tmp.IndexNumber = request.IndexNumber; tmp.FirstName = request.FirstName; tmp.LastName = request.LastName; tmp.BirthDate = DateTime.Parse(request.BirthDate); _context.Add <Student>(tmp); _context.Student.Add(tmp); } //sprawdzenie czy sa dane studia if (studies == null) { return(false); } //sprawdzanie czy jest dany enrollment var tmpEnrol = _context.Enrollments.Find(request.Studies); if (tmpEnrol == null) { tmpEnrol = new Enrollment(); tmpEnrol.IdEnrollment = _context.Enrollments.LastOrDefault().IdEnrollment + 1; tmpEnrol.Semester = 1; tmpEnrol.IdStudy = _context.Studies.Find(request.Studies).idStudy; tmpEnrol.StartDate = DateTime.Now; _context.Add <Enrollment>(tmpEnrol); } _context.SaveChanges(); return(true); }catch (Exception ex) { Console.WriteLine(ex.Message); } /* * using (var con = new SqlConnection(dbName)) * { * using (var com = new SqlCommand()) * { * com.Connection = con; * con.Open(); * SqlTransaction transaction = con.BeginTransaction(); * * try * { * // SPRAWDZANIE CZY PODANE STUDIA ISTNIEJA * com.CommandText = "SELECT * FROM Studies WHERE name = name"; * com.Parameters.AddWithValue("name", request.Studies); * com.Transaction = transaction; * SqlDataReader reader = com.ExecuteReader(); * * if (!reader.HasRows) * { * return false; * } * * // DODAWANIE DO ENROLLMENT * com.CommandText = "SELECT * FROM enrollment e INNER JOIN studies s on s.idstudy = e.idstudy WHERE semester = 1 AND name = sname"; * com.Parameters.AddWithValue("sname", request.Studies); * * if (!reader.HasRows) * { * com.CommandText = "INSERT INTO enrollment(idenrollment, semester, idstudy, startdate)" + * "VALUES((select Max(idEnrollment)+1 from enrollment), 1, SELECT idstudy FROM Studies WHERE name = sname), GETDATE())"; * com.Parameters.AddWithValue("sname", request.Studies); * * com.ExecuteNonQuery(); * } * reader.Close(); * * // DODAWANIE NOWEGO STUDENTA * // zmienne sa z @ bo inaczej SQL krzyczal ze mu nie pasuje * com.CommandText = "SELECT * FROM Student WHERE indexnumber = @es"; * com.Parameters.AddWithValue("@es", request.IndexNumber); * reader = com.ExecuteReader(); * * if (!reader.HasRows) * { * reader.Close(); * com.CommandText = "INSERT INTO Student(indexnumber, firstname, lastname, birthdate, idenrollment)" + * "values(@es, @firstname, @lastname, @birthdate, (SELECT idenrollment FROM enrollment e INNER JOIN Studies s on e.idstudy = s.idstudy WHERE semester = 1 AND s.name = @sname))"; * com.Parameters.AddWithValue("@firstname", request.FirstName); * com.Parameters.AddWithValue("@lastname", request.LastName); * com.Parameters.AddWithValue("@birthdate", DateTime.Parse(request.BirthDate)); * * com.ExecuteNonQuery(); * reader.Close(); * } * else * { * Console.WriteLine("insert"); * return false; * } * * transaction.Commit(); * transaction.Dispose(); * * return true; * } * catch (Exception e) * { * try * { * transaction.Rollback(); * } * catch (Exception ex) * { * Console.WriteLine("Error at Rollback! " + ex.Message); * } * * return false; * } * } * } */ return(false); }