Click here to Skip to main content
Sign Up to vote bad
good
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)
        {
            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):
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 30 Dec '12 - 20:44
Edited 30 Dec '12 - 20:49


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.
  Permalink  
Comments
Software Engineer 892 - 31 Dec '12 - 2:57
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:07
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:34
anybody there , please solve.

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

  Print Answers RSS
Your Filters
Interested
Ignored
     
0 OriginalGriff 355
1 Sergey Alexandrovich Kryukov 338
2 Arun Vasu 315
3 Maciej Los 208
4 Aarti Meswania 180
0 Sergey Alexandrovich Kryukov 9,755
1 OriginalGriff 7,549
2 CPallini 4,018
3 Rohan Leuva 3,362
4 Maciej Los 2,951


Advertise | Privacy | Mobile
Web02 | 2.6.130523.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid