This code worked in my demo page but after transfering it to my work page its not insertig anymore its showing
UNDEFINDED
<form id="form1" runat="server">
<div>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updResult" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:HiddenField ID="hfUserName" runat="server" />
<asp:DataList ID="dlBooks" runat="server" DataKeyName="Id" RepeatLayout="Table" OnItemDataBound="dlBooks_ItemDataBound">
<HeaderTemplate>
<tr>
<th>
<asp:Label Text="Book Pics" runat="server" />
</th>
<th>
<asp:Label ID="Label1" Text="BookName" runat="server" />
</th>
<th>
<asp:Label ID="Label2" Text="Favorite Book" runat="server" />
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="FavouriteID" Style="display: none;" runat="server" />
<asp:HiddenField ID="hfPostId" Value='<%#Eval("ID") %>' runat="server" />
<asp:Label ID="Label3" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "SendDate","{0:MMM ddd,yyyy}" ) %>' Font-Size="Smaller" ForeColor="Gray" />
</td>
<td>
<asp:Label ID="lblbookName" Text='<%#Eval("Name")%>' runat="server" />
</td>
<td>
<asp:LinkButton ID="btnAdd" CssClass="GLYPHICON GLYPHICON-USD"
runat="server" Font-Size="Large" />
<asp:Label ID="lblBOOKCOUNT" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:DataList>
</ContentTemplate>
</asp:UpdatePanel>
<br />
<br />
<asp:LinkButton ID="LinkButton1" Text="Logout" runat="server" />
</div>
<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.postId = $(this).closest('tr').find('[id*=hfPostId]').val();
obj.icon = $(this)[0].className;
obj.bookName = $(this).closest('tr').find('[id*=lblbookName]').html();
obj.favouriteId = $(this).closest('tr').find('[id*=ID]').html() != "" ? $(this).closest('tr').find('[id*=ID]').html() : "0";
var rowIndex = $(this).closest('tr')[0].rowIndex;
$.ajax({
type: "POST",
url: "LIKESTEST.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>
</form>
</body>
</html>
code
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.Populatebooks();
}
}
private void Populatebooks()
{
// string userId = Session["userName"].ToString();
// string userId = Session["userName"].ToString();
// string str = ConfigurationManager.ConnectionStrings["conn"].ConnectionString;
string Populatebooks = "GetUserPOSTS";
using (SqlConnection con = new SqlConnection(constring))
{
con.Open();
using (SqlCommand cmd = new SqlCommand(Populatebooks, con))
{
cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.AddWithValue("@Name", Request.QueryString["Id"].ToString());
cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
// cmd.Parameters.AddWithValue("@Id", Id);
//lblpost.Text = TextBixcomment.Text.Replace(Environment.NewLine, "<BR />");
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
ViewState["DataTable"] = dt;
dlBooks.DataSource = dt;
dlBooks.DataBind();
}
}
}
[System.Web.Services.WebMethod]
public static List<string> OnFavoriteBook(string icon, string bookName, int favouriteId, string userName, string postId)
{
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(@PostId,@UserName,@BookName,@BookCount)", con))
{
cmd.Parameters.AddWithValue("@PostId", postId);
// cmd.Parameters.AddWithValue("@UserName", Session["userName"]);
cmd.Parameters.AddWithValue("@UserName", HttpContext.Current.Session["userName"].ToString());
cmd.Parameters.AddWithValue("@BookName", bookName);
cmd.Parameters.AddWithValue("@BookCount", 1);
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;
}
private static string GetBookCounts(string bookName, string lblbookCount, SqlConnection con)
{
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)
{
lblbookCount = dt.Rows[0]["BookCount"].ToString() != "0" ? dt.Rows[0]["BookCount"].ToString() : "";
}
}
return lblbookCount;
}
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).CssClass = "glyphicon glyphicon-save-file";
(e.Item.FindControl("FavouriteID") as Label).Text = !string.IsNullOrEmpty(row["FavouriteID"].ToString()) ? row["FavouriteID"].ToString() : "0";
}
}
}
}
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() != "0" ? dt.Rows[0]["BookCount"].ToString() : "";
}
}
}
}
}
}
Please can you help to run it on your own side