public List<Recipe> GetRelatedRecipes() { List<Recipe> newRec = new List<Recipe>(); try { conn.Open(); SqlCommand sqlcmd = new SqlCommand("GetRecipeTypes", conn); sqlcmd.CommandType = CommandType.StoredProcedure; DataTable table = new DataTable(); SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd); adapter.Fill(table); SqlDataReader reader = sqlcmd.ExecuteReader(); reader.Read(); foreach (DataRow row in table.Rows) { Recipe rec = new Recipe( row["RecipeName"].ToString(), int.Parse(row["FK_Quality"].ToString()), bool.Parse(row["IsApproved"].ToString())); newRec.Add(rec); } conn.Close(); } catch (Exception) { throw new ArgumentException("GetRecipeType failed!"); } return newRec; }
public List<Recipe> ReadRecipe() { // Instantiate List of Recipes newRec. List <Recipe> newRec = new List<Recipe>(); //open connection conn.Open(); SqlCommand sqlcmd = new SqlCommand("RetrieveRecipe", conn); // Define sqlcmd as a StoredProcedure. sqlcmd.CommandType = CommandType.StoredProcedure; // Datatable recipeTable is created. DataTable recipeTable = new DataTable(); // sqlDataAdapter adapter is instantiated and sql is used as input parameter SqlDataAdapter adapter = new SqlDataAdapter(sqlcmd); // Adapter is filled with recipeTable DataTable as param. adapter.Fill(recipeTable); // Datareader reader instantiated SqlDataReader reader = sqlcmd.ExecuteReader(); reader.Read(); // Foreach loop that loops through all rows in our DataTable and adds to newRec (list of recipes) foreach (DataRow row in recipeTable.Rows) { Recipe recipe = new Recipe( int.Parse(row["Recipe_ID"].ToString()), row["RecipeName"].ToString(), int.Parse(row["FK_Quality"].ToString()), bool.Parse(row["IsApproved"].ToString())); //row["Description"].ToString()); newRec.Add(recipe); } // Connection is closed conn.Close(); return newRec; }