public int Save(Student aStudent) { SqlConnection aConnection = new SqlConnection(connectionString); string query = string.Format(@"INSERT INTO Student VALUES('{0}','{1}','{2}','{3}','{4}','{5}')", aStudent.Name,aStudent.Age,aStudent.Address,aStudent.ContactNo,aStudent.Department,aStudent.Marks); SqlCommand command = new SqlCommand(query, aConnection); aConnection.Open(); int x = command.ExecuteNonQuery(); aConnection.Close(); return x; }
public bool IsStudentNameExists(Student aStudent) { bool isStudentExists = false; SqlConnection connection = new SqlConnection(connectionString); string query = "Select Name From Student where Name='" + aStudent.Name + "'"; SqlCommand command = new SqlCommand(query, connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { isStudentExists = true; break; } reader.Close(); connection.Close(); return isStudentExists; }
public string SaveStudent(Student aStudent) { if (aGAteway.IsStudentNameExists(aStudent)) { return "Student Exists"; } else { if (aGAteway.Save(aStudent)>0) { return "SuccessFully Saved"; } else { return "Save Failed"; } } }
public List<Student> ShowAllStudents() { SqlConnection connection=new SqlConnection(connectionString); List<Student>studentList=new List<Student>(); string query = "select * From Student order By Id DESC"; SqlCommand command=new SqlCommand(query,connection); connection.Open(); SqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Student aStudent=new Student(); aStudent.Id = int.Parse(reader.GetValue(0).ToString()); aStudent.Name = reader.GetValue(1).ToString(); aStudent.Age = int.Parse(reader.GetValue(2).ToString()); aStudent.Address = reader.GetValue(3).ToString(); aStudent.ContactNo = reader.GetValue(4).ToString(); aStudent.Department = reader.GetValue(5).ToString(); aStudent.Marks = int.Parse(reader.GetValue(6).ToString()); studentList.Add(aStudent); } reader.Close(); connection.Close(); return studentList; }
protected void searchButton_Click(object sender, EventArgs e) { string name = nameTextBox.Text; studentGridView.DataSource = aManager.ShowStudentsByName(name); studentGridView.DataBind(); aStudent= aManager.ShowStudentsByName(name).FirstOrDefault(); ageTextBox.Text = aStudent.Age.ToString(); addressTextBox.Text = aStudent.Address; contactTextBox.Text = aStudent.ContactNo; deptTextBox.Text = aStudent.Department; TextBox.Text = aStudent.Marks.ToString(); isUpdateMode = true; saveButton.Text = "Update"; }