How do i display Count column along side with glyphicon glyphicon-save-file after button click.
On the solution you gave on this very link below i want to display the count column that shows 1 after button is clicked
http://www.aspforums.net/Threads/147830/How-do-i-insert-Favorites-items-in-another-table-and-join-Table-to-an-already-mergered-tables-proced/
see diagram below
UserName | BookName | Count
------------------------------------------------
mic33 SeeMan 1
---------------------------------------------
I have done the insert but to select it is the issue
Default2.aspx Page
HTML
<form id="form1" runat="server">
<div>
<asp:DataList ID="dlBooks" runat="server" RepeatColumns="4" OnItemDataBound="dlBooks_ItemDataBound">
<HeaderTemplate>
<table class="table" border="1">
<tr>
<td>
<asp:Label Text="Book Pics" runat="server" />
</td>
<td>
<asp:Label ID="Label1" Text="BookName" runat="server" />
</td>
<td>
<asp:Label ID="Label2" Text="Favorite Book" runat="server" />
</td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="FavoriteId" Text="0" Visible="false" runat="server" />
<asp:Label ID="lblId" Text='<%#Eval("Id") %>' Visible="false" runat="server" />
<asp:ImageButton ID="Imagebooks" OnClick="OnBookClick" ImageUrl='<%# "Images/" + Eval("Name") %>'
Width="80px" Height="50px" runat="server" />
</td>
<td>
<asp:Label ID="lblbookName" Text='<%#Eval("Name")%>' runat="server" />
</td>
<td>
<%-- <asp:Label ID="LabICON" runat="server" CssClass="glyphicon glyphicon-usd" Font-Size="Large"
ForeColor="#003366"></asp:Label>
<asp:Label ID="LabISAVEICON" runat="server" CssClass="glyphicon glyphicon-save-file"
Font-Size="Large" ForeColor="#003366"></asp:Label>--%>
<asp:LinkButton ID="btnAdd" CssClass="glyphicon glyphicon-usd" OnClick="OnfavoriteBook"
runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:DataList>
</div>
<br />
<br />
<asp:LinkButton Text="Logout" OnClick="OnLogOut" runat="server" />
</form>
C# CODE
---------------------------------------
private string constring = ConfigurationManager.ConnectionStrings["constring"].ToString();
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Populatebooks();
}
}
private void Populatebooks()
{
string userId = Session["UserName"].ToString();
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [dbo].[Files]", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
dlBooks.DataSource = dt;
dlBooks.DataBind();
}
}
}
protected void OnfavoriteBook(object sender, EventArgs e)
{
DataListItem item = (sender as LinkButton).NamingContainer as DataListItem;
string bookName = (item.FindControl("lblbookName") as Label).Text;
int favoriteId = Convert.ToInt32((item.FindControl("FavoriteId") as Label).Text);
using (SqlConnection con = new SqlConnection(constring))
{
if ((item.FindControl("btnAdd") as LinkButton).CssClass.ToUpper() == "GLYPHICON GLYPHICON-USD")
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Dim_favorite values(@UserName,@BookName)", con))
{
cmd.Parameters.AddWithValue("@UserName", Session["UserName"]);
cmd.Parameters.AddWithValue("@BookName", bookName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
else
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Dim_favorite WHERE FavoriteId=@FavoriteId", con))
{
cmd.Parameters.AddWithValue("@FavoriteId", favoriteId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
this.Populatebooks();
}
protected void dlBooks_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string bookName = (e.Item.FindControl("lblbookName") as Label).Text;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Dim_favorite WHERE UserName='" + Session["UserName"] + "'", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
if (bookName == row["BookName"].ToString())
{
//(e.Item.FindControl("btnAdd") as LinkButton).Text = "Added";
(e.Item.FindControl("btnAdd") as LinkButton).CssClass = "glyphicon glyphicon-save-file";
(e.Item.FindControl("FavoriteId") as Label).Text = row["FavoriteId"].ToString();
}
}
}
}
}
}
}
protected void OnBookClick(object sender, EventArgs e)
{
DataListItem item = (sender as ImageButton).NamingContainer as DataListItem;
int bookId = Convert.ToInt32((item.FindControl("lblId") as Label).Text);
int favoriteId = Convert.ToInt32((item.FindControl("FavoriteId") as Label).Text);
Response.Redirect("AddedItemPage.aspx?bookid=" + bookId + "&favoriteId=");
}
protected void OnLogOut(object sender, EventArgs e)
{
Response.Redirect("Login.aspx");
}