Counting Hits on a link and save it to database

Posted by Satyapriyanayak under ASP.NET category on | Points: 40 | Views : 830
Create table test.
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" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "">

<html xmlns="" >
<head runat="server">
<title>Untitled Page</title>
<form id="form1" runat="server">
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">Hits</asp:LinkButton>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>

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);
str = "update test set count='" + Label1.Text + "'";
com = new SqlCommand(str, con);

protected void Page_Load(object sender, EventArgs e)
SqlConnection con = new SqlConnection(strConnString);
str = "select sum(count)from test";
com = new SqlCommand(str, con);
Label1.Text = com.ExecuteScalar().ToString();

Comments or Responses

Login to post response