H
Hrithik Gajmal
Guest
I am trying to make a wishlist option in my project I followed an online tutorial to implement it and its working just fine. I am using repeater control to show data with an image-button. In this on image button click that book should be added in wishlist and that's happening data is getting stored in the database but after storing in the database in want the button image to change to show a user that it already added. This is whole code behind of that page:
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace EReader.EReader
{
public partial class Books : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\EReader.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true");
private int PageSize = 9;
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
if (!IsPostBack)
{
this.GetCustomersPageWise(1);
}
if (Request.QueryString["addtofavbooks"] != null && Session["LoggedIn"] != null)
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Favourite (username,bookId) values('" + Session["LoggedIn"].ToString() + "'," + Request.QueryString["addtofavbooks"].ToString() + ")";
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Product Added in Wishlist');</script>");
}
if (Session["LoggedIn"] == null)
{
if (Request.QueryString["addtofavbooks"] != null)
{
Response.Write("<script>alert('Login to Your Account First. No Product Added in WishList');</script>");
}
}
else
{
fullnametop.Text = Session["LoggedIn"].ToString();
}
}
protected void books_ItemDataBound(object source, RepeaterItemEventArgs e)
{
if (Session["LoggedIn"] != null)
{
Label bookid = e.Item.FindControl("bookid") as Label;
ImageButton addfavourite = e.Item.FindControl("addfavourite") as ImageButton;
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Favourite where bookId=@bookid and username=@username";
cmd.Parameters.AddWithValue("@bookid", bookid.Text);
cmd.Parameters.AddWithValue("@UserName", Session["LoggedIn"].ToString());
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
addfavourite.ImageUrl = "/app-assets/images/liked.svg";
addfavourite.Enabled = false;
}
else
{
addfavourite.ImageUrl = "/app-assets/images/like.svg";
}
}
else
{
ImageButton addfavourite = e.Item.FindControl("addfavourite") as ImageButton;
addfavourite.ImageUrl = "/app-assets/images/like.svg";
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
System.Web.UI.HtmlControls.HtmlContainerControl edit = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("edit");
System.Web.UI.HtmlControls.HtmlContainerControl delete = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("delete");
if (Session["LoggedIn"] == null || Session["role"].Equals("User"))
{
edit.Visible = false;
delete.Visible = false;
}
else
{
edit.Visible = true;
delete.Visible = true;
}
}
}
protected void books_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Response.Redirect("Books.aspx?addtofavbooks=" + e.CommandArgument.ToString());
}
private void GetCustomersPageWise(int pageIndex)
{
using (SqlCommand cmd = new SqlCommand("GetBooksPageWise", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
IDataReader idr = cmd.ExecuteReader();
books.DataSource = idr;
books.DataBind();
idr.Close();
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
}
private void PopulatePager(int recordCount, int currentPage)
{
double dbPageCount = (double)((decimal)recordCount / Convert.ToDecimal(PageSize));
int pageCount = (int)Math.Ceiling(dbPageCount);
List<ListItem> pages = new List<ListItem>();
if (pageCount > 0)
{
for (int i = 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
if (currentPage == 1)
{
prev.Visible = false;
}
else
{
prev.CommandArgument = (currentPage - 1).ToString();
prev.Visible = true;
}
if (currentPage >= dbPageCount)
{
next.Visible = false;
}
else
{
next.CommandArgument = (currentPage + 1).ToString();
next.Visible = true;
}
}
pager.DataSource = pages;
pager.DataBind();
}
protected void Page_Changed(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
this.GetCustomersPageWise(pageIndex);
}
}
}
In this, as you can see if the image button is clicked and if that book is not in the wishlist table then it will show like.svg image but if it's in some user's wishlist then it should show liked.svg but when I run the project and add a book to wishlist it still shows the like.svg same image. Please help me out in this if possible.
Continue reading...
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
namespace EReader.EReader
{
public partial class Books : System.Web.UI.Page
{
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\EReader.mdf;Integrated Security=True;Connect Timeout=30;MultipleActiveResultSets=true");
private int PageSize = 9;
protected void Page_Load(object sender, EventArgs e)
{
if (con.State == ConnectionState.Open)
{
con.Close();
}
con.Open();
if (!IsPostBack)
{
this.GetCustomersPageWise(1);
}
if (Request.QueryString["addtofavbooks"] != null && Session["LoggedIn"] != null)
{
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "insert into Favourite (username,bookId) values('" + Session["LoggedIn"].ToString() + "'," + Request.QueryString["addtofavbooks"].ToString() + ")";
cmd.ExecuteNonQuery();
Response.Write("<script>alert('Product Added in Wishlist');</script>");
}
if (Session["LoggedIn"] == null)
{
if (Request.QueryString["addtofavbooks"] != null)
{
Response.Write("<script>alert('Login to Your Account First. No Product Added in WishList');</script>");
}
}
else
{
fullnametop.Text = Session["LoggedIn"].ToString();
}
}
protected void books_ItemDataBound(object source, RepeaterItemEventArgs e)
{
if (Session["LoggedIn"] != null)
{
Label bookid = e.Item.FindControl("bookid") as Label;
ImageButton addfavourite = e.Item.FindControl("addfavourite") as ImageButton;
SqlCommand cmd = con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "select * from Favourite where bookId=@bookid and username=@username";
cmd.Parameters.AddWithValue("@bookid", bookid.Text);
cmd.Parameters.AddWithValue("@UserName", Session["LoggedIn"].ToString());
SqlDataAdapter sda = new SqlDataAdapter();
sda.SelectCommand = cmd;
DataSet ds = new DataSet();
sda.Fill(ds);
if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
{
addfavourite.ImageUrl = "/app-assets/images/liked.svg";
addfavourite.Enabled = false;
}
else
{
addfavourite.ImageUrl = "/app-assets/images/like.svg";
}
}
else
{
ImageButton addfavourite = e.Item.FindControl("addfavourite") as ImageButton;
addfavourite.ImageUrl = "/app-assets/images/like.svg";
}
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
System.Web.UI.HtmlControls.HtmlContainerControl edit = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("edit");
System.Web.UI.HtmlControls.HtmlContainerControl delete = (System.Web.UI.HtmlControls.HtmlContainerControl)e.Item.FindControl("delete");
if (Session["LoggedIn"] == null || Session["role"].Equals("User"))
{
edit.Visible = false;
delete.Visible = false;
}
else
{
edit.Visible = true;
delete.Visible = true;
}
}
}
protected void books_ItemCommand(object source, RepeaterCommandEventArgs e)
{
Response.Redirect("Books.aspx?addtofavbooks=" + e.CommandArgument.ToString());
}
private void GetCustomersPageWise(int pageIndex)
{
using (SqlCommand cmd = new SqlCommand("GetBooksPageWise", con))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@PageIndex", pageIndex);
cmd.Parameters.AddWithValue("@PageSize", PageSize);
cmd.Parameters.Add("@RecordCount", SqlDbType.Int, 4);
cmd.Parameters["@RecordCount"].Direction = ParameterDirection.Output;
IDataReader idr = cmd.ExecuteReader();
books.DataSource = idr;
books.DataBind();
idr.Close();
int recordCount = Convert.ToInt32(cmd.Parameters["@RecordCount"].Value);
this.PopulatePager(recordCount, pageIndex);
}
}
private void PopulatePager(int recordCount, int currentPage)
{
double dbPageCount = (double)((decimal)recordCount / Convert.ToDecimal(PageSize));
int pageCount = (int)Math.Ceiling(dbPageCount);
List<ListItem> pages = new List<ListItem>();
if (pageCount > 0)
{
for (int i = 1; i <= pageCount; i++)
{
pages.Add(new ListItem(i.ToString(), i.ToString(), i != currentPage));
}
if (currentPage == 1)
{
prev.Visible = false;
}
else
{
prev.CommandArgument = (currentPage - 1).ToString();
prev.Visible = true;
}
if (currentPage >= dbPageCount)
{
next.Visible = false;
}
else
{
next.CommandArgument = (currentPage + 1).ToString();
next.Visible = true;
}
}
pager.DataSource = pages;
pager.DataBind();
}
protected void Page_Changed(object sender, EventArgs e)
{
int pageIndex = int.Parse((sender as LinkButton).CommandArgument);
this.GetCustomersPageWise(pageIndex);
}
}
}
In this, as you can see if the image button is clicked and if that book is not in the wishlist table then it will show like.svg image but if it's in some user's wishlist then it should show liked.svg but when I run the project and add a book to wishlist it still shows the like.svg same image. Please help me out in this if possible.
Continue reading...