hi i want to add one more Count query to this previous solution, but the query count i want to add will fetch total count from another table called USERPost.
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(ShareId) as TotalCount FROM USERPost WHERE ShareId='" + shareId + "'", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
(e.Item.FindControl("SHARECOUNT") as Label).Text = dt.Rows[0]["TotalCount"].ToString() != "0" ? dt.Rows[0]["TotalCount"].ToString() : "";
}
}
previouse complete code
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,@BookCount)", con))
{
cmd.Parameters.AddWithValue("@UserName", Session["UserName"]);
cmd.Parameters.AddWithValue("@BookName", bookName);
cmd.Parameters.AddWithValue("@BookCount", 1);
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("lblBOOKCOUNT") as Label).Text = (Convert.ToInt32(row["BookCount"].ToString()) + Convert.ToInt32(row["BookCount"].ToString())).ToString();
(e.Item.FindControl("btnAdd") as LinkButton).CssClass = "glyphicon glyphicon-save-file";
(e.Item.FindControl("FavoriteId") as Label).Text = row["FavoriteId"].ToString();
}
}
}
}
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(bookName) as BookCount FROM Dim_favorite WHERE BookName='" + bookName + "'", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
(e.Item.FindControl("lblBOOKCOUNT") as Label).Text = dt.Rows[0]["BookCount"].ToString();
}
}
}
}
}
//i want to add one more count query here to fetch total count from table USERPost
using (SqlCommand cmd = new SqlCommand("SELECT COUNT(ShareId) as TotalCount FROM USERPost WHERE ShareId='" + shareId + "'", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.Rows.Count > 0)
{
(e.Item.FindControl("SHARECOUNT") as Label).Text = dt.Rows[0]["TotalCount"].ToString() != "0" ? dt.Rows[0]["TotalCount"].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");
}