Click here to Skip to main content
15,881,938 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:
C#
protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            LoadGridCounterData();
        }

    }


    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();
        da.Fill(ds);
        GVCounter.DataSource = ds;
        GVCounter.DataBind();
    }

This is code am facing problem (reset button code):
C#
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;
        con.Open();
        cmd.ExecuteNonQuery();
        con.Close();
        GVCounter.DataSource = ds;
     // GVCounter.DataBind();

        ScriptManager.RegisterStartupScript(this, this.GetType(), "RunCode", "javascript:alert('Successfully Counters Reset to Zero');", true);
    }
Posted
Updated 6-Jun-23 5:01am
v3

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.
 
Share this answer
 
Comments
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.
C#
protected void ResetImgBtn_Click(object sender, ImageClickEventArgs e)
{
    // ...
    // do the reset counter to zero
    // ...
    
    // Refresh the GridView
    LoadGridCounterData();
}
 
Share this answer
 
Comments
Richard Deeming 6-Jun-23 11:52am    
I'm guessing the OP isn't still looking for an answer 10½ years later. :)
adriancs 6-Jun-23 21:15pm    
yeah, I'm not aware that this question is already 10+ years old. by the way, @AndréKraak why did you update a question of 10 years old :)
Richard Deeming 7-Jun-23 3:41am    
That's a known bug; if you look at the version history[^], you'll see that André updated the question back in December 2012.

The "updated" timestamp shows the last update for a question or any solution, including already-deleted spam solutions.

And since yours is solution #83, there have been 81 spam solutions to this question deleted over the years. :)
adriancs 7-Jun-23 5:04am    
Cool. Thanks for the info, Richard :D

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