Click here to Skip to main content
15,394,715 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
How to reset the counter to zero in ASP.NET?

Dear Friends,

I have a ASP.NET button with the name RESET and a grid view below this button.
When I click on this reset button it should clear one column in the grid and it should set all the numbers to 0 (Zero).

In my database I have table called Linkcounter with fields as Counter and Links.
For example: this is my database table
Counter  Links
12       Home
15       About Us
12       Our Services
3        Contact Us

When I click on the reset button the counter values should reset to 0 (Zero).

Please help.

This is my code:
protected void Page_Load(object sender, EventArgs e)

        if (!IsPostBack)


    private void LoadGridCounterData()
        SqlConnection con = new SqlConnection(_connString);
        SqlCommand cmd = new SqlCommand("Select Counter,link from linkcounter", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataSet ds = new DataSet();
        GVCounter.DataSource = ds;

This is code am facing problem (reset button code):
protected void ResetImgBtn_Click(object sender, ImageClickEventArgs e)
        SqlConnection con = new SqlConnection(_connString);

        SqlCommand cmd = new SqlCommand();
        DataSet ds = new DataSet();
       cmd.CommandText = "Update LinkCounter Set Counter=0";
        cmd.Connection = con;
        GVCounter.DataSource = ds;
     // GVCounter.DataBind();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Successfully Counters Reset to Zero');", true);
Updated 30-Dec-12 20:49pm

1 solution

You are loading the data in page load. The event fires AFTER page load. Load the data in prerender, which fires AFTER the button, and it will work fine.
Software Engineer 892 31-Dec-12 2:57am
no, its not working. if i put page load code in prerender Gridview is not displaying.
and if i put the pageload code both in prerender and pageload...then also its not working..Please help am fresher
Christian Graus 31-Dec-12 14:07pm
I suggest you stop using the word 'fresher'. It has no meaning in English. On this site, it tends to mean 'I am a non programmer, being paid to write code. I took your job away through outsourcing and now I want you to do the work for me'. My advice is correct. However, as it is a postback, you need to reload your data ( because you changed it ). Get rid of the 'IsPostback' check, and in general, read a book on ASP.NET and try to UNDERSTAND what your code does, instead of copying it blindly off the web. Also, learn to use the debugger, it would have shown you what is happening, in every step of this process.
Software Engineer 892 31-Dec-12 3:34am
anybody there , please solve.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900