protected void GetCategories() { //code lives between brackets and disconnects from db when it is out of them //use EF to connect to the list of categories using (DefaultConnection db = new DefaultConnection()) { //simplified linq query var cats = from c in db.Categories select c; //bind the cats to the grid grdCategories.DataSource = cats.ToList(); grdCategories.DataBind(); } }
protected void GetRecipes() { //code lives between brackets and disconnects from db when it is out of them //use EF to connect to the list of recipes using (DefaultConnection db = new DefaultConnection()) { //simplified linq query var recs = from r in db.Recipes select r; //bind the cats to the grid grdRecipes.DataSource = recs.ToList(); grdRecipes.DataBind(); } }
protected void GetRecipe() { //look up the selected recipe and populate the form using (DefaultConnection db = new DefaultConnection()) { //store the ID in a variable Int32 RecipeID = Convert.ToInt32(Request.QueryString["RecipeID"]); //look up the recipe Recipe objR = (from r in db.Recipes where r.RecipeID == RecipeID select r).FirstOrDefault(); //pre populate our form fields txtName.Text = objR.RecipeName; } }
protected void GetCategory() { //look up the selected category and populate the form using (DefaultConnection db = new DefaultConnection()) { //store the ID in a variable Int32 CategoryID = Convert.ToInt32(Request.QueryString["CategoryID"]); //look up which category Category objC = (from c in db.Categories where c.CategoryID == CategoryID select c).FirstOrDefault(); //pre populate the form fields txtName.Text = objC.CategoryName; } }
protected void grdCategories_RowDeleting(object sender, GridViewDeleteEventArgs e) { //identify the CategoryID from the row that we want to delete Int32 CategoryID = Convert.ToInt32(grdCategories.DataKeys[e.RowIndex].Values["CategoryID"]); //connect using (DefaultConnection db = new DefaultConnection()) { //same as sql command (SELECT * categories WHERE CategoryID= the variable) //select current category into memory based on the ID Category objC = (from c in db.Categories where c.CategoryID == CategoryID select c).FirstOrDefault(); //delete db.Categories.Remove(objC); db.SaveChanges(); //refresh the grid GetCategories(); } }
protected void btnSave_Click(object sender, EventArgs e) { //connect to database using (DefaultConnection db = new DefaultConnection()) { //create a new category in memory Category objC = new Category(); //Int32 CategoryID = 0; //try to find the url if (!string.IsNullOrEmpty(Request.QueryString["CategoryID"])) { //get the ID Int32 CategoryID = Convert.ToInt32(Request.QueryString["CategoryID"]); //see which category it is objC = (from c in db.Categories where c.CategoryID == CategoryID select c).FirstOrDefault(); } //fill the properties of the new category we are adding objC.CategoryName = txtName.Text; //add it if there is no id found call the add function if (String.IsNullOrEmpty(Request.QueryString["CategoryID"])) { db.Categories.Add(objC); } //save the new category db.SaveChanges(); //redirect to the categories list page Response.Redirect("categories.aspx"); } }
protected void btnSave_Click(object sender, EventArgs e) { //connect to database using (DefaultConnection db = new DefaultConnection()) { //create a new recipe in the memory Recipe objR = new Recipe(); //check for the url if (!string.IsNullOrEmpty(Request.QueryString["RecipeID"])) { //get the ID Int32 RecipeID = Convert.ToInt32(Request.QueryString["RecipeID"]); //check recipe objR = (from r in db.Recipes where r.RecipeID == RecipeID select r).FirstOrDefault(); } //fill the properties of the new recipe objR.RecipeName = txtName.Text; //add it - if no id found call the add function if (String.IsNullOrEmpty(Request.QueryString["RecipeID"])) { db.Recipes.Add(objR); } //save new recipe on db db.SaveChanges(); //redirect user to the recipes page Response.Redirect("recipes.aspx"); } }