Hi micah,
First you need to get the UserId from the login page and store it in Session like below
protected void OnLogin(object sender, EventArgs e)
{
using (SqlConnection con = new SqlConnection(constring))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM [User] WHERE UserName=@UserName AND Password=@Password", con))
{
cmd.Parameters.AddWithValue("@Username", txtUserName.Text.Trim());
cmd.Parameters.AddWithValue("@Password", txtPassword.Text.Trim());
con.Open();
SqlDataReader idr = cmd.ExecuteReader();
if (idr.Read())
{
Session["UserID"] = idr["ID"].ToString();
Session["UserName"] = idr["UserName"].ToString();
Response.Redirect("Default2.aspx");
}
con.Close();
}
}
}
and you need to add column in Dim Favourite table with below query
ALTER TABLE Dim_favorite
ADD UserId INT
and keep in mind once you have added the UserId Column then the already present values in Dim Favourite table will be null for the UserId column like below
favoriteId |
UserName |
BookName |
BookCount |
UserId |
97 |
John |
Tulips.jpg |
1 |
NULL |
98 |
John |
Jellyfish.jpg |
1 |
NULL |
99 |
Peter |
Tulips.jpg |
1 |
NULL |
100 |
Peter |
Jellyfish.jpg |
1 |
NULL |
101 |
test |
Jellyfish.jpg |
1 |
NULL |
104 |
test |
Hydrangeas.jpg |
1 |
NULL |
115 |
test |
Koala.jpg |
1 |
NULL |
122 |
test |
Desert.jpg |
1 |
NULL |
123 |
test |
Lighthouse.jpg |
1 |
NULL |
once above alteration is done then you need to access the UserId Session like below and need to modify your jquery code like below
<div>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/json2/20130526/json2.min.js"></script>
<script type="text/javascript">
$(function () {
$('[id*=btnAdd]').click(function () {
var obj = {};
$('[id*=hfUserName]').val('<%= Session["UserName"].ToString() %>');
obj.userName = $('[id*=hfUserName]').val();
obj.userId = '<%= Session["UserID"].ToString() %>';
obj.icon = $(this)[0].className;
obj.bookName = $(this).closest('tr').find('[id*=lblbookName]').html();
obj.favouriteId = $(this).closest('tr').find('[id*=FavoriteId]').html() != "" ? $(this).closest('tr').find('[id*=FavoriteId]').html() : "0";
var rowIndex = $(this).closest('tr')[0].rowIndex;
$.ajax({
type: "POST",
url: "Default2.aspx/OnfavoriteBook",
data: JSON.stringify(obj),
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var className = response.d[0];
var bookCount = response.d[1];
$('[id*=dlBooks] tr').eq(rowIndex).find('[id*=lblBOOKCOUNT]').html(bookCount);
$('[id*=dlBooks] tr').eq(rowIndex).find('[id*=btnAdd]')[0].className = className;
},
failure: function (response) {
alert(response.d);
},
error: function (response) {
alert(response.d);
}
});
return false;
});
});
</script>
</div>
and your code behind code will look like below
[System.Web.Services.WebMethod]
public static List<string> OnFavoriteBook(string icon, string bookName, int favouriteId, string userName, string userId)
{
string bookIcon = "glyphicon glyphicon-save-file";
List<string> values = new List<string>();
string lblbookCount = string.Empty;
string message = string.Empty;
using (SqlConnection con = new SqlConnection(constring))
{
if (icon.ToUpper() == "GLYPHICON GLYPHICON-USD")
{
using (SqlCommand cmd = new SqlCommand("INSERT INTO Dim_favorite values(@UserName,@BookName,@BookCount,@UserId)", con))
{
cmd.Parameters.AddWithValue("@UserName", userName);
cmd.Parameters.AddWithValue("@BookName", bookName);
cmd.Parameters.AddWithValue("@BookCount", 1);
cmd.Parameters.AddWithValue("@UserId", userId);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
lblbookCount = GetBookCounts(bookName, lblbookCount, con);
bookIcon = "glyphicon glyphicon-save-file";
}
else
{
using (SqlCommand cmd = new SqlCommand("DELETE FROM Dim_favorite WHERE UserName = @UserName AND BookName = @BookName ", con))
{
cmd.Parameters.AddWithValue("@UserName", userName);
cmd.Parameters.AddWithValue("@BookName", bookName);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
lblbookCount = GetBookCounts(bookName, lblbookCount, con);
bookIcon = "glyphicon glyphicon-usd";
}
}
values.Add(bookIcon);
values.Add(lblbookCount);
return values;
}