I am developing a website in that i have to count a clicks on a link and increment +1 in database
please help
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Satyapriya NayakPosted Apr 26, 2013, 1:52 PM
Column name as count int
You have to insert 0 in database manually
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="Textbox_gridview.WebForm2" %>
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Textbox_gridview
{
public partial class WebForm2 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str;
SqlCommand com;
protected void LinkButton1_Click(object sender, EventArgs e)
{
Label1.Text = (Convert.ToInt32(Label1.Text)+1).ToString();
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "update test set count='" + Label1.Text + "'";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
}
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str = "select sum(count)from test";
com = new SqlCommand(str, con);
Label1.Text = com.ExecuteScalar().ToString();
con.Close();
}
}
}