I have two pages Item page and landing page
Now on landing page i have the display of all books with their names and button add
On the item page i have the display of a very item with picture, name, and a button showing whither it has been added or not added, if the it
has been added into my favorite table the button added will display but if not the button Add will display.
So now i tried this code but it didnt work
static string username = string.Empty;
static string Fusername = string.Empty;
private string constring = ConfigurationManager.ConnectionStrings["conn"].ToString();
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ToString());
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
if (this.Page.User.Identity.IsAuthenticated)
{
this.GetUserDetail();
}
}
}
private void GetUserDetail()
{
// string userId = Session["UserId"].ToString();
username = this.Page.User.Identity.Name;
//string userId = Session["UserId"].ToString();
string getADPOST = "GetUSERPRO2";
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(getADPOST, con))
{
cmd.CommandType = CommandType.StoredProcedure;
{
cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
cmd.Parameters.AddWithValue("@BOOKUserName", Session["userName"]);
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
FormView1BOOK.DataSource = dt;
FormView1BOOK.DataBind();
}
}
}
}
protected void People(object sender, EventArgs e)
{
DataListItem item = (sender as LinkButton).NamingContainer as DataListItem;
// int bookId = Convert.ToInt32((item.FindControl("lblId") as Label).Text);
// string Name = ((item.FindControl("lblname") as Label).Text);
//
string usernameF = ((item.FindControl("lblId") as Label).Text);
using (SqlConnection con = new SqlConnection(constring))
{
if ((item.FindControl("btnLike") as LinkButton).Text.ToUpper() == "ADD")
using (SqlCommand cmd = new SqlCommand("INSERT INTO FAVORITES values(@UserName,@BOOKUserName,@UserStatus,@Count)", con))
{
// cmd.Parameters.AddWithValue("@UserId", Session["UserId"]);
// cmd.Parameters.AddWithValue("@BookId", bookId);
cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
cmd.Parameters.AddWithValue("@BOOKUserName", Session["userName"]);
cmd.Parameters.AddWithValue("@UserStatus", "TRUE");
cmd.Parameters.AddWithValue("@Count", 1);
// cmd.Parameters.AddWithValue("@Status", status);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
else
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM FAVORITES WHERE BOOKUserName=@BOOKUserName", con))
{
cmd.Parameters.AddWithValue("@BOOKUserName", username);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
this.GetUserDetail();
}
}
protected void FormView1BOOK_ItemDataBound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
string usernameBOOK = (e.Item.FindControl("lblId") as Label).Text;
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT f.BOOKUserName AS username,fav.Name,'ADDED' [Status] FROM FAVORITES f INNER JOIN User3 fav On f.UserName = fav.UserName WHERE fav.UserName=@BOOKUserName", con))
{
DataTable dt = new DataTable();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
cmd.Parameters.AddWithValue("@BOOKUserName", Session["userName"]);
sda.Fill(dt);
foreach (DataRow row in dt.Rows)
{
if (usernamefollow == row["username"].ToString())
{
(e.Item.FindControl("btnLike") as LinkButton).Text = "Following";
(e.Item.FindControl("UserName") as Label).Text = row["UserName"].ToString();
// (e.Item.FindControl("BOOKUserName") as Label).Text = row["BOOKUserName"].ToString();
// (e.Item.FindControl("lblmark") as Label).Text = "";
}
}
}
}
}
PLEASE NOTE / the string like this cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
is helping to show the right item by fetching the username of the book when you click on the name of the book on the landig page which
will redirect to item page and the when the username is fetched by the string
cmd.Parameters.AddWithValue("@UserName", Request.QueryString["Id"].ToString());
it will display the right item.
I got the example here
http://www.aspforums.net/Threads/141357/Add-record-to-Database-if-Button-text-is-Add-and-Delete-record-form-Database-if-Button-text-is-Added-using-C-in-ASPNet/
But i tried to modify it to suit my question but didnt work