protected void saveButton_Click(object sender, EventArgs e) { UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; CookbookDatabaseEntities dbcon = new CookbookDatabaseEntities(); SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\CookbookDatabase.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"); SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From CookbookRecipes where RecipeName = '" + recipeName + "'", con); DataTable dt = new DataTable(); sda.Fill(dt); if ((dt.Rows[0][0].ToString() == "0") || (dt.Rows[0][0].ToString() == "1")) { // Get remaining ingredients from text box if (ingredientsTextBox.Text != "") { ingredients += ingredientsTextBox.Text; } // Get remaining instructions from text box if (instructionsTextBox.Text != "") { instructions += instructionsTextBox.Text; } // If the user doesn't enter a recipe name if (recipeNameTextBox.Text.Equals("")) { NameLabel.Text = "*Enter a recipe name"; NameLabel.Visible = true; } else { recipeName = recipeNameTextBox.Text; CookbookRecipe newRecipe = new CookbookRecipe { RecipeId = recipeId, RecipeName = recipeName, Ingredients = ingredients, Instructions = instructions, UserId = (int)Session["UserID"] }; // remove the old recipe dbcon.CookbookRecipes.Attach(originalRecipe); dbcon.CookbookRecipes.Remove(originalRecipe); // replace the old recipe with the newly edited recipe (with the same id as before) dbcon.CookbookRecipes.Add(newRecipe); dbcon.SaveChanges(); Response.Redirect("Recipepage.aspx"); } } }
/* For when user wants to save a recipe */ protected void saveButton_Click(object sender, EventArgs e) { UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; CookbookDatabaseEntities dbcon = new CookbookDatabaseEntities(); SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\CookbookDatabase.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"); SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From CookbookRecipes where RecipeName = '" + nameTextBox.Text + "' and Ingredients = '" + ingredientsTextBox.Text + "'", con); DataTable dt = new DataTable(); sda.Fill(dt); // save the recipe as long as it doesn't already exist in the database if (!(dt.Rows[0][0].ToString() == "1")) { string recipeName = ""; // Get remaining ingredients from the text box if (ingredientsTextBox.Text != "") { ingredients += ingredientsTextBox.Text; } // Get remaining instructions from the text box if (instructionsTextBox.Text != "") { instructions += instructionsTextBox.Text; } // If the user doesn't enter a recipe name if (nameTextBox.Text.Equals("")) { NameLabel.Text = "*Enter a recipe name"; NameLabel.Visible = true; } else { recipeName = nameTextBox.Text; CookbookRecipe newRecipe = new CookbookRecipe { RecipeId = (int)DateTime.Now.ToFileTime(), RecipeName = recipeName, Ingredients = ingredients, Instructions = instructions, UserId = (int)Session["UserID"] }; // add new recipe to database dbcon.CookbookRecipes.Add(newRecipe); dbcon.SaveChanges(); Response.Redirect("Recipepage.aspx"); } } }
protected void SubmitButton_Click(object sender, EventArgs e) { UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; CookbookDatabaseEntities dbcon = new CookbookDatabaseEntities(); if (IsPostBack) { Validate(); if (IsValid) { string username = usernameTextBox.Text; string password = passwordTextBox.Text; dbcon.CookbookUsers.SqlQuery($"SELECT * FROM CookbookUsers WHERE Username = {username} AND Password = {password}"); SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\CookbookDatabase.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"); // SDA contains how many usernames and passwords are the same. SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From CookbookUsers where Username = '******' and Password = '******'", con); // usertable contains a row of information from cookbook users SqlDataAdapter usertableadapter = new SqlDataAdapter("Select * From CookbookUsers where Username = '******' and Password = '******'", con); //puts the whole row into this table DataTable userTable = new DataTable(); DataTable dt = new DataTable(); usertableadapter.Fill(userTable); sda.Fill(dt); // if the user exists in the database then log them in if (dt.Rows[0][0].ToString() == "1") { // save sessions of the currently logged in user Session["Username"] = usernameTextBox.Text; Session["Password"] = passwordTextBox.Text; Session["UserID"] = userTable.Rows[0][0]; dbcon.SaveChanges(); Response.Redirect("Recipepage.aspx"); } // if the user doesn't exist in database, show error message else { Label1.Visible = true; } } } }
protected void createAccountButton_Click(object sender, EventArgs e) { //makes all error labels not visible each time it runs through the method invalidPasswordLabel.Visible = false; invalidEmailLabel.Visible = false; UsernameLabel.Visible = false; UnobtrusiveValidationMode = System.Web.UI.UnobtrusiveValidationMode.None; CookbookDatabaseEntities dbcon = new CookbookDatabaseEntities(); //connects to database SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\CookbookDatabase.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"); //paste this code in the line above ^^^^^^ SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\CookbookDatabase.mdf;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework"); //Counts username and email in database SqlDataAdapter sda = new SqlDataAdapter("Select Count(*) From CookbookUsers where Username = '******' or Email = '" + emailTextBox.Text + "'", con); DataTable dt = new DataTable(); sda.Fill(dt); //checks if username or email is already in database if (dt.Rows[0][0].ToString() == "1") { UsernameLabel.Visible = true; } else if (dt.Rows[0][0].ToString() == "2") { UsernameLabel.Visible = true; } else { //verifies email format Regex regex = new Regex(@"^([\w\.\-]+)@([\w\-]+)((\.(\w){2,3})+)$"); string email = emailTextBox.Text; Match match = regex.Match(email); if (match.Success) { //verifies password is not blank if (passwordTextBox.Text != "") { // verifies passwrd is at least 8 characters in length if (!(passwordTextBox.Text.Length < 8)) { string username = usernameTextBox.Text; string password = passwordTextBox.Text; CookbookUser newUser = new CookbookUser { //User newUser = new User(); UserId = (int)DateTime.Now.ToFileTime(), Email = email, Username = username, Password = password }; //adds user to database dbcon.CookbookUsers.Add(newUser); dbcon.SaveChanges(); Response.Redirect("Recipepage.aspx"); } else { //error message if password is left blank passwordLengthLabel.Visible = true; } } else { //error message if password is left blank invalidPasswordLabel.Visible = true; } } else { //error message if email is in invalid format invalidEmailLabel.Visible = true; } } }