private void CommandAction(string text, object parameter, bool isProcedure, Action <SqlDataReader> action) { // Create the Command and Parameter objects. SqlConnection connection = new SqlConnection(ContextObj.ConnectionString); SqlCommand command = new SqlCommand(text, connection); command.CommandType = isProcedure ? CommandType.StoredProcedure : CommandType.Text; if (parameter != null) { CommonUtility.AnonymousObjectToHtmlAttributes(parameter).ForEach(p => { command.Parameters.AddWithValue("@" + p.Key, p.Value); }); } try { connection.Open(); SqlDataReader reader = command.ExecuteReader(); action(reader); reader.Close(); } catch (Exception ex) { throw ex; } finally { connection.Close(); } }