public void DeleteStudentRecord(Models.StudentInformation_Result studentcontactperson) { //List<StudentInformation_Result> students = new List<StudentInformation_Result>(); //using (SqlConnection myCon = new SqlConnection("Data Source=DESKTOP-50L3KE8\\SQLEXPRESS;Initial Catalog=DB_Class;Integrated Security=True")) //{ // SqlCommand cmd = new SqlCommand("Delete FROM Student where StudentID = " + studentcontactperson.StudentID); // cmd.Connection = myCon; // myCon.Open(); // cmd.ExecuteNonQuery(); // myCon.Close(); //} DB_ClassEntities db_dlt = new DB_ClassEntities(); var students = db_dlt.Students.FirstOrDefault(s => s.StudentID == studentcontactperson.StudentID); if (students != null) { db_dlt.Students.Remove(students); db_dlt.SaveChanges(); } }
public void AddStudentInfo(Models.StudentInformation_Result studentcontactperson) { using (SqlConnection myCon = new SqlConnection("Data Source=DESKTOP-BQRJ2ID;Initial Catalog=DB_Class;Integrated Security=True")) { SqlCommand cmd = new SqlCommand("Student_Info", myCon); cmd.CommandType = System.Data.CommandType.StoredProcedure; cmd.Parameters.Add("@first_name", SqlDbType.VarChar).Value = studentcontactperson.first_names; cmd.Parameters.Add("@middle_name", SqlDbType.VarChar).Value = studentcontactperson.middle_names; cmd.Parameters.Add("@last_name", SqlDbType.VarChar).Value = studentcontactperson.last_names; cmd.Parameters.Add("@birthdate", SqlDbType.Date).Value = studentcontactperson.birthdate; cmd.Parameters.Add("@contact", SqlDbType.VarChar).Value = studentcontactperson.contact; cmd.Parameters.Add("@Gender", SqlDbType.VarChar).Value = studentcontactperson.Gender; cmd.Parameters.Add("@contact_email", SqlDbType.VarChar).Value = studentcontactperson.contact_emails; cmd.Parameters.Add("@LocalAddress", SqlDbType.VarChar).Value = studentcontactperson.LocalAddress; cmd.Parameters.Add("@PermanentAddress", SqlDbType.VarChar).Value = studentcontactperson.PermanentAddress; cmd.Parameters.Add("@Adharcard", SqlDbType.BigInt).Value = studentcontactperson.AdharNumber; try { myCon.Open(); cmd.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } SqlCommand cmd1 = new SqlCommand("Contact_Person_Info", myCon); cmd1.CommandType = System.Data.CommandType.StoredProcedure; cmd1.Parameters.Add("@first_name", SqlDbType.VarChar).Value = studentcontactperson.first_name; cmd1.Parameters.Add("@last_name", SqlDbType.VarChar).Value = studentcontactperson.last_name; cmd1.Parameters.Add("@middle_name", SqlDbType.VarChar).Value = studentcontactperson.middle_name; cmd1.Parameters.Add("@contact_number1", SqlDbType.VarChar).Value = studentcontactperson.contact_number1; cmd1.Parameters.Add("@contact_number2", SqlDbType.VarChar).Value = studentcontactperson.contact_number2; cmd1.Parameters.Add("@contact_email", SqlDbType.VarChar).Value = studentcontactperson.contact_email; cmd1.Parameters.Add("@RelationWith_Student", SqlDbType.VarChar).Value = studentcontactperson.RelationWith_Student; cmd1.Parameters.Add("@Address", SqlDbType.VarChar).Value = studentcontactperson.Address; try { //myCon.Open(); cmd1.ExecuteNonQuery(); } catch (Exception ex) { throw ex; } finally { myCon.Close(); myCon.Dispose(); } } }