protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { // Read the connection string from Web.config string connectionString = ConfigurationManager.ConnectionStrings["RB_RecipeBook"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectionString)) { try { // Get data about users from the database SqlDataAdapter adapterUsers = new SqlDataAdapter("SELECT idUser, UserName FROM RB_User", conn); DataTable storeTableUsers = new DataTable(); // table to store the sql command adapterUsers.Fill(storeTableUsers); // Populate dropdownlist SubmittedByDropDownList.DataSource = storeTableUsers; SubmittedByDropDownList.DataTextField = "UserName"; SubmittedByDropDownList.DataValueField = "idUser"; SubmittedByDropDownList.DataBind(); // Add extra item "All" SubmittedByDropDownList.Items.Insert(0, new ListItem("All Users")); // Get data about categories from the database SqlDataAdapter adapterCategories = new SqlDataAdapter("SELECT idCategory, CategoryName FROM RB_Category", conn); DataTable storeTableCategories = new DataTable(); // table to store the sql command adapterCategories.Fill(storeTableCategories); // Populate dropdownlist CategoriesDropDownList.DataSource = storeTableCategories; CategoriesDropDownList.DataTextField = "CategoryName"; CategoriesDropDownList.DataValueField = "idCategory"; CategoriesDropDownList.DataBind(); // Add extra item "All" CategoriesDropDownList.Items.Insert(0, new ListItem("All Categories")); // Get data about ingredients from the database SqlDataAdapter adapterIngredients = new SqlDataAdapter("SELECT idIngredient, IngredientName FROM RB_Ingredient", conn); DataTable storeTableIngredients = new DataTable(); // table to store the sql command adapterIngredients.Fill(storeTableIngredients); // Populate dropdownlist IngredientsDropDownList.DataSource = storeTableIngredients; IngredientsDropDownList.DataTextField = "IngredientName"; IngredientsDropDownList.DataValueField = "idIngredient"; IngredientsDropDownList.DataBind(); // Add extra item "All" IngredientsDropDownList.Items.Insert(0, new ListItem("All Ingredients")); } catch (Exception ex) { MessageLabel.Text = "ERROR: " + ex.Message; } } } }
protected void Page_Load(object sender, EventArgs e) { // Bind the gridview of the ingredients to the list of ingredients List <Ingredient> newListOfIngredients = (List <Ingredient>)Application["ingredients"]; //newListOfIngredients.Clear(); ingredientsGridView.DataSource = newListOfIngredients; ingredientsGridView.DataBind(); if (!Page.IsPostBack) { // Populate dropdown list for hours of preparation/cooking PrepareTimeHoursDropDownList.Items.Insert(0, new ListItem("--Select--")); for (int i = 1; i < 26; i++) { string value = (i - 1).ToString(); PrepareTimeHoursDropDownList.Items.Insert(i, new ListItem(value)); } // Populate dropdown list for minutes of preparation/cooking PrepareTimeMinutesDropDownList.Items.Insert(0, new ListItem("--Select--")); for (int i = 1; i < 61; i++) { string value = (i - 1).ToString(); PrepareTimeMinutesDropDownList.Items.Insert(i, new ListItem(value)); } ////// Populate dropdown list for ingredients and measure units: // Read the connection string from Web.config string connectionString = ConfigurationManager.ConnectionStrings["RB_RecipeBook"].ConnectionString; using (SqlConnection conn = new SqlConnection(connectionString)) { try { /////////////IngredientWebUserControl0 // Get data about ingredients from the database SqlDataAdapter adapterIngredients = new SqlDataAdapter("SELECT idIngredient, IngredientName FROM RB_Ingredient", conn); DataTable storeTableIngredient = new DataTable(); // table to store the sql command adapterIngredients.Fill(storeTableIngredient); // Populate dropdownlist IngredientWebUserControl0.DDLName.DataSource = storeTableIngredient; IngredientWebUserControl0.DDLName.DataTextField = "IngredientName"; IngredientWebUserControl0.DDLName.DataValueField = "idIngredient"; IngredientWebUserControl0.DDLName.DataBind(); // Add default item //IngredientWebUserControl0.DDLName.Items.Insert(0, new ListItem("--Select ingredient--")); // Get data about ingredients from the database SqlDataAdapter adapterUnits = new SqlDataAdapter("SELECT idUnit, UnitName FROM RB_MeasureUnit", conn); DataTable storeTableUnits = new DataTable(); // table to store the sql command adapterUnits.Fill(storeTableUnits); // Populate dropdownlist IngredientWebUserControl0.DDLUnit.DataSource = storeTableUnits; IngredientWebUserControl0.DDLUnit.DataTextField = "UnitName"; IngredientWebUserControl0.DDLUnit.DataValueField = "idUnit"; IngredientWebUserControl0.DDLUnit.DataBind(); // Add default item //IngredientWebUserControl0.DDLUnit.Items.Insert(0, new ListItem("--Select measure unit--")); // Get data about users from the database SqlDataAdapter adapterUsers = new SqlDataAdapter("SELECT idUser, UserName FROM RB_User", conn); DataTable storeTableUsers = new DataTable(); // table to store the sql command adapterUsers.Fill(storeTableUsers); // Populate dropdownlist SubmittedByDropDownList.DataSource = storeTableUsers; SubmittedByDropDownList.DataTextField = "UserName"; SubmittedByDropDownList.DataValueField = "idUser"; SubmittedByDropDownList.DataBind(); } catch (Exception ex) { MessageLabel.Text = "ERROR: " + ex.Message; } } ///// Select item corresponding to the logged in user MembershipUser currentUser = Membership.GetUser(); string userName = currentUser.UserName; SqlConnection con; SqlCommand comm; SqlDataReader reader; con = new SqlConnection(connectionString); // Create command comm = new SqlCommand( "SELECT idUser FROM RB_User WHERE UserName = @UserName", con); // Add the idUser parameter comm.Parameters.Add("UserName", SqlDbType.NVarChar); comm.Parameters["UserName"].Value = userName; try { // Open the connection con.Open(); reader = comm.ExecuteReader(); reader.Read(); SubmittedByDropDownList.SelectedValue = reader.GetValue(0).ToString(); SubmittedByDropDownList.Enabled = false; reader.Close(); } catch { MessageLabel.Text = "Error: user not found"; } finally { // Close the connection con.Close(); } } }