count website visiter


  • source

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
            <h1>Count website visiters</h1>
        </div>
        <div>

            <asp:Label ID="Label1" runat="server" style="font-size: xx-large" Text="Web Visiters   :"></asp:Label>
&nbsp;<asp:Label ID="Label2" runat="server" style="font-size: x-large" Text="Label"></asp:Label>

        </div>
    </form>
</body>
</html>


  • C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
    SqlConnection con;
    SqlCommand com;
    SqlDataAdapter da;
    DataTable dt;
    string str = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\alex\source\repos\webvisiter-count\webvisiter-count\App_Data\Database.mdf;Integrated Security=True";
    protected void Page_Load(object sender, EventArgs e)
    {
        faindview();
        updatecounter();

    }
    private void faindview()
    {
        SqlConnection con = new SqlConnection(str);
        con.Open();
        com = new SqlCommand("select * from data", con);
        com.ExecuteNonQuery();
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = com;
        DataSet ds = new DataSet();
        da.Fill(ds);
        Label2.Text = "Website View Counter : " + ds.Tables[0].Rows[0]["visitercount"].ToString();
        con.Close();

    }
    private void updatecounter()
    {
        SqlConnection con = new SqlConnection(str);
        com = new SqlCommand("UPDATE data set visitercount=visitercount+1", con);
        con.Open();
        com.ExecuteNonQuery();
        con.Close();

    }
}

NOTE : firest you need to enter 0 value in side your data base other wise you get error msg No row selected 

Comments

Popular posts from this blog

Search Record From Table Using jQuery in Asp.net core MVC

How To insert Radio Button Data in DataBase -- MVC

Toolbox Webparts Controls