//function that sets the form to the information of the selected primary key private void BindData(int menuItemID) { //if the class if doesn't equal -1, set the page up as an update if (menuItemID != -1) { //make the button say btnAddUpdate.Text = "Update"; //instantiate a new class of the form type, passing in the routeData primary se256_Dobachesky.MenuItem aItem = new se256_Dobachesky.MenuItem(menuItemID); //if the data exists, fill the form with it's information if (aItem != null) { txtName.Text = aItem.item_name; txtDescription.Text = aItem.item_desc; txtAllergens.Text = aItem.item_allergens; txtPrice.Text = aItem.item_price.ToString(); ddlCategory.SelectedValue = aItem.cat_id.ToString(); ddlMenu.SelectedValue = aItem.menu_id.ToString(); chkGlutenFree.Checked = aItem.item_gluten_free; chkIsActive.Checked = aItem.item_active; } } //if the class does equal -1, set the form up as an add else { btnAddUpdate.Text = "Add"; } }
protected void btnAddUpdate_Click(object sender, EventArgs e) { //instantiate a new class se256_Dobachesky.MenuItem aMenuItem; //if the routedata information exists, finish the class by passing in the primary key id if (RouteData.Values["menuItemId"] != null) { aMenuItem = new se256_Dobachesky.MenuItem(Convert.ToInt32(RouteData.Values["menuItemId"].ToString())); } //if the routedata information does not exist, finish the class with a blank class else { aMenuItem = new se256_Dobachesky.MenuItem(); } //set the object's properties to be equal to the information on the form aMenuItem.menu_id = Convert.ToInt32(ddlMenu.SelectedValue.Trim()); aMenuItem.cat_id = Convert.ToInt32(ddlCategory.SelectedValue.Trim()); aMenuItem.item_name = txtName.Text.Trim(); aMenuItem.item_desc = txtDescription.Text.Trim(); aMenuItem.item_allergens = txtAllergens.Text.Trim(); aMenuItem.item_price = Convert.ToDecimal(txtPrice.Text.Trim().ToString()); aMenuItem.item_gluten_free = Convert.ToBoolean(chkGlutenFree.Checked.ToString()); aMenuItem.item_active = Convert.ToBoolean(chkIsActive.Checked.ToString()); //if the primary key exists go forward with the update if (aMenuItem.item_id > 0) { //run the update method, and if it fails display an error message in a label if (se256_Dobachesky.MenuItem.UpdateItem(aMenuItem)) { Response.Redirect("/Admin/Menu-Items"); } else { lblMessage.Text = "Menu item update failed!"; } } //if the primary key does not exist it is because the record does not exist yet, go forward with the add else { //run the insert method, and if it fails display an error message in a label if (se256_Dobachesky.MenuItem.InsertItem(aMenuItem)) { Response.Redirect("/Admin/Menu-Items"); } else { lblMessage.Text = "Menu item insert failed!"; } } }
public static bool UpdateItem(se256_Dobachesky.MenuItem aItem) { //start out with the boolean value at false bool blnSuccess = false; //create sql connection object that gets connection string from web.config SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["SE256_CS"].ConnectionString); //create sql command as stored procedure SqlCommand cmd = new SqlCommand("menu_items_update", conn); //set the command as a stored procedure cmd.CommandType = CommandType.StoredProcedure; //add the parameters to be used in the update method cmd.Parameters.Add("@item_id", SqlDbType.Int).Value = aItem.item_id; cmd.Parameters.Add("@menu_id", SqlDbType.Int).Value = aItem.menu_id; cmd.Parameters.Add("@cat_id", SqlDbType.Int).Value = aItem.cat_id; cmd.Parameters.Add("@item_name", SqlDbType.VarChar).Value = aItem.item_name; cmd.Parameters.Add("@item_desc", SqlDbType.VarChar).Value = aItem.item_desc; cmd.Parameters.Add("@item_allergens", SqlDbType.VarChar).Value = aItem.item_allergens; cmd.Parameters.Add("@item_price", SqlDbType.Decimal).Value = aItem.item_price; cmd.Parameters.Add("@item_gluten_free", SqlDbType.Bit).Value = aItem.item_gluten_free; cmd.Parameters.Add("@item_active", SqlDbType.Bit).Value = aItem.item_active; //try to open the connection and run the command, then set the boolean value to true try { conn.Open(); cmd.ExecuteNonQuery(); blnSuccess = true; } //prepare the error as a string and set boolean value to false catch (Exception e) { e.ToString(); blnSuccess = false; } //close the connection finally { conn.Close(); } //return the boolean containing information as to of if the operation was a success or not return(blnSuccess); }