Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
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:44pm
Edited 30-Dec-12 20:49pm
v3

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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 at 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 at 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 at 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)

  Print Answers RSS
0 OriginalGriff 474
1 Gihan Liyanage 338
2 ChauhanAjay 180
3 Sergey Alexandrovich Kryukov 163
4 Vinay Mistry 160
0 Sergey Alexandrovich Kryukov 9,011
1 OriginalGriff 7,941
2 CPallini 2,603
3 Richard MacCutchan 2,121
4 Abhinav S 1,928


Advertise | Privacy | Mobile
Web03 | 2.8.140827.1 | Last Updated 31 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100