Tuesday, 31 July 2012

SHOW COUNTER ON WEBSITE


STEP1
create a table Counter(CounterID bigint primary key identity,CounterName bigint,Updatedon datetime)
and insert a value
insert into Counter values(0,1);

STEP2
now create a procedure as

CREATE proc Counter_acteon
as
Declare @counter bigint
set @counter=(select top(1) CounterNumber from counter order by updatedon desc)
set @counter=@counter+1
insert Counter (counternumber) values (@counter)
SELECT RIGHT(replicate('0',7)+convert(varchar,@counter),8) as counter

STEP3

and open website where you want to add a counter
add a label control as

<asp:Label ID="lbl_counter" runat="server" ForeColor="White" 
                                    BackColor="Black" Font-Bold="True" Font-Size="Large" BorderStyle="Ridge"></asp:Label>

STEP4
write code on page load

public partial class _Default : System.Web.UI.Page
{

    cscounter csd = new cscounter();
    protected void Page_Load(object sender, EventArgs e)
    {
        DataTable dt = new DataTable();
        dt =csd.setcounter();
        lbl_counter.Text = dt.Rows[0][0].ToString();
    }
}

STEP5
then add a Class File and code following
public class cscounter
{
    public cscounter()
    {
    
    }

    private int _counter;


public int Counter
{
  get { return _counter; }
  set { _counter = value; }
}

public DataTable  setcounter()
{
    SqlConnection con = new SqlConnection("Data Source=SERVER2008R2\\ONLINE24X7; User ID=sa; Password=stream@1234; Initial Catalog=BSAWEBSITE;");
    SqlCommand cmd = new SqlCommand();
    cmd.CommandText = "sp_select";
    cmd.CommandType = CommandType.StoredProcedure;

    DataTable dt = new DataTable();
    cmd.Connection = con;
    con.Open();
    SqlDataAdapter datadp = new SqlDataAdapter(cmd);
    datadp.Fill(dt);
  
    return dt;
}
}

No comments:

Post a Comment