private bool ReadStudentFromDB(int id, out Student student) { SqlConnection connection; SqlCommand command; SqlDataReader reader; bool result = false; student = null; try { connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString); command = new SqlCommand("SELECT * FROM Student WHERE Id=@Id", connection); command.Parameters.AddWithValue("@Id", id); using (connection) { connection.Open(); reader = command.ExecuteReader(); if (reader.HasRows) { student = new Student( (int)reader["Id"], (string)reader["Name"], (int)reader["Age"] ); result = true; } connection.Close(); } } catch (Exception e) { //Log(e.Message); } return result; } private bool ReadStudentFromDB(string name, out Student student) { SqlConnection connection; SqlCommand command; SqlDataReader reader; bool result = false; student = null; try { connection = new SqlConnection(WebConfigurationManager.ConnectionStrings["StudentConnectionString"].ConnectionString); command = new SqlCommand("SELECT * FROM Student WHERE Name=@Name", connection); command.Parameters.AddWithValue("@Name", name); using (connection) { connection.Open(); reader = command.ExecuteReader(); if (reader.HasRows) { student = new Student( (int)reader["Id"], (string)reader["Name"], (int)reader["Age"] ); result = true; } connection.Close(); } } catch(Exception e) { //Log(e.Message); } return result; }