I have modified your code please check.
HTML
<div>
<asp:Button Text="Like/UnLike" runat="server" ID="btnLikeUnLike" OnClick="btnLike_Click" />
<br />
<asp:DataList runat="server" ID="dlCount">
<ItemTemplate>
<table>
<tr>
<td>
UserId:
</td>
<td>
<asp:Label ID="Label1" Text='<%#Eval("USERID") %>' runat="server"></asp:Label>
</td>
</tr>
<tr>
<td>
Count:
</td>
<td>
<asp:Label ID="Label2" Text='<%#Eval("COUNTSTATUS") %>' runat="server"></asp:Label>
<%--<asp:Label ID="lblCountStatus" Text='<%#Eval("COUNTSTATUS").ToString() == "0"? "Like": "UnLike" %> '
runat="server"></asp:Label>--%>
</td>
</tr>
</table>
</ItemTemplate>
</asp:DataList>
</div>
Code
string constrr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BindCountUserWise(2);//int.Parse(Request.QueryString["Id"].ToString())
}
}
protected void btnLike_Click(object sender, EventArgs e)
{
follow(2);//int.Parse(Request.QueryString["Id"].ToString())
BindCountUserWise(2);//int.Parse(Request.QueryString["Id"].ToString())
}
private void BindCountUserWise(int id)
{
using (SqlConnection conn = new SqlConnection(constrr))
{
SqlCommand cmdd = new SqlCommand("SELECT * FROM IncrementDecrement WHERE USERID = '" + id + "'", conn);
conn.Open();
cmdd.CommandType = CommandType.Text;
SqlDataAdapter da = new SqlDataAdapter(cmdd);
DataTable dt = new DataTable();
da.Fill(dt);
dlCount.DataSource = dt;
dlCount.DataBind();
conn.Close();
}
}
private void follow(int Id)
{
using (SqlConnection con = new SqlConnection(constrr))
{
SqlCommand cmd = new SqlCommand("SELECT USERID,ISNULL(COUNTSTATUS,0) COUNTSTATUS FROM IncrementDecrement WHERE USERID = '" + Id + "'", con);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
int countStatus = int.Parse(dr["COUNTSTATUS"].ToString());
using (SqlConnection con1 = new SqlConnection(constrr))
{
SqlCommand cmd1 = new SqlCommand("UPDATE IncrementDecrement SET COUNTSTATUS = @COUNTSTATUS WHERE USERID = '" + Id + "'", con1);
cmd1.Connection = con1;
con1.Open();
cmd1.CommandType = CommandType.Text;
if (countStatus == 0)
{
cmd1.Parameters.AddWithValue("@COUNTSTATUS", 1);
}
else
{
cmd1.Parameters.AddWithValue("@COUNTSTATUS", 0);
}
cmd1.ExecuteNonQuery();
con1.Close();
}
}
}
else
{
using (SqlConnection conn = new SqlConnection(constrr))
{
SqlCommand cmdd = new SqlCommand("INSERT INTO IncrementDecrement VALUES (@USERID, @COUNTSTATUS )");
cmdd.Connection = conn;
conn.Open();
cmdd.CommandType = CommandType.Text;
cmdd.Parameters.AddWithValue("@USERID", Id);
cmdd.Parameters.AddWithValue("@COUNTSTATUS", 1);
cmdd.ExecuteNonQuery();
conn.Close();
}
}
con.Close();
}
}
Screenshot