public WishedArt GetSpecific(string custId, string artpieceId) { using (SqlConnection con = DBUtil.BuildConnection()) { con.Open(); SqlCommand Cmd; Cmd = new SqlCommand("SELECT * FROM WishedArt WHERE ([CUSTID] = @CUSTID) AND ([ARTPIECEID] = @ARTPIECEID)", con); Cmd.Parameters.AddWithValue("@CUSTID", custId); Cmd.Parameters.AddWithValue("@ARTPIECEID", artpieceId); using (SqlDataReader Dr = Cmd.ExecuteReader()) { if (Dr.Read()) { /* method thanks to Ron C - https://stackoverflow.com/a/41041029 */ // int i = 0; //return new Customer(Dr.GetString(i++), Dr.GetString(i++), Dr.GetString(i++), Dr.GetString(i++), Dr.GetString(i++), (byte[]) Dr["PasswordSalt"], Dr.GetString(i++)); // method thanks to Andy Edinborough & Cosmin - https://stackoverflow.com/a/5371281 WishedArt wishedArt = new WishedArt( (int)Dr["WishIndex"], (string)Dr["ArtpieceId"], (string)Dr["CustId"] ); Dr.Close(); con.Close(); return(wishedArt); } } con.Close(); return(null); } }
public void Delete(WishedArt WishedArt) { using (SqlConnection con = DBUtil.BuildConnection()) { con.Open(); SqlCommand Cmd = new SqlCommand("DELETE FROM WishedArt WHERE WishIndex = @WishIndex", con); Cmd.Parameters.AddWithValue("@WishIndex", WishedArt.WishIndex); Cmd.ExecuteNonQuery(); con.Close(); } }
private void LoadBt() { // Get customer from session Customer customer = (Customer)Net.GetSession("customer"); // Show the buttons status if (customer != null) { // Make buttons visible btnAddToWishlist.Visible = true; if (artpiece.IsForSale) { btnAddToCart.Visible = true; } WishedArtDao dao = new WishedArtDao(); wish = dao.GetSpecific(customer.Id, Net.GetQueryStr("id")); // If wish already exists, show Added to Wishlist if (wish != null) { btnAddToWishlist.Text = "ADDED TO WISHLIST"; } bool found = false; List <Order_Artwork> oaList = (List <Order_Artwork>)Net.GetSession("oaList"); if (oaList != null) { // Loop through order list in session to see if this artpiece already added to cart foreach (Order_Artwork oa in oaList) { if (oa.ArtpieceId.ToLower() == Net.GetQueryStr("id").ToLower()) { found = true; } Quick.Print(oa.ArtpieceId.ToLower() + " == " + Net.GetQueryStr("id")); } } if (found) { btnAddToCart.Text = "ADDED TO CART"; } else { btnAddToCart.Text = "ADD TO CART"; } } }
// crud functions public void Add(WishedArt WishedArt) { using (SqlConnection con = DBUtil.BuildConnection()) { con.Open(); SqlCommand Cmd = new SqlCommand("INSERT INTO WishedArt(ArtpieceId, CustId)" + "VALUES(@ArtpieceId, @CustId)", con); Cmd.Parameters.AddWithValue("@ArtpieceId", WishedArt.ArtpieceId); Cmd.Parameters.AddWithValue("@CustId", WishedArt.CustId); Cmd.ExecuteNonQuery(); con.Close(); } }
public void Update(WishedArt WishedArt) { using (SqlConnection con = DBUtil.BuildConnection()) { // SqlCommand Cmd = DBUtil.GenerateSql("UPDATE Customer SET ... WHERE ([CustomerId] = @CustomerId)"); SqlCommand Cmd = new SqlCommand("UPDATE WishedArt " + "SET ArtpieceId = @ArtpieceId" + ", CustId = @CustId" + " WHERE WishIndex = @WishIndex", con ); con.Open(); Cmd.Parameters.AddWithValue("@WishIndex", WishedArt.WishIndex); Cmd.Parameters.AddWithValue("@ArtpieceId", WishedArt.ArtpieceId); Cmd.Parameters.AddWithValue("@CustId", WishedArt.CustId); Cmd.ExecuteNonQuery(); con.Close(); } }
protected void btnAddToWishlist_Click(object sender, EventArgs e) { // Get customer from session Customer customer = (Customer)Net.GetSession("customer"); // Redirect if not logged in if (customer == null) { Net.Redirect("~/ Pages / LoginRegister.aspx"); } else { if (wish == null) // If havent added yet { wish = new WishedArt(); //Make new wish wish.CustId = customer.Id; wish.ArtpieceId = Net.GetQueryStr("Id"); // Perform insert operation WishedArtDao dao = new WishedArtDao(); dao.Add(wish); // Change button label to tell user already added to wishlist btnAddToWishlist.Text = "Added to Wishlist"; } else // If already added { // If already added, should already be loaded during page load WishedArtDao dao = new WishedArtDao(); // Delete wish in DB dao.Delete(wish); // Change button label to let user know is no longer in wishlist btnAddToWishlist.Text = "Add to Wishlist"; } } }