3 Replies - 629 Views - Last Post: 03 August 2010 - 11:05 AM Rate Topic: -----

#1 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

var value resets in another method

Posted 03 August 2010 - 03:08 AM

In one method, I have an int sum which holds the sum of other int values.

When I call another method, it seems to reset the value of my int sum to 0?

Its in a gridview RowDataBound method that it's originally assigned the total sum value.

pasting revelant bits..

int sum = 0;
...
...
...

  protected void GridView_A_RowDataBound(object sender, GridViewRowEventArgs e)
        {
                checkTotal();
                
                if (sum != 0 && sum != 100 || sum > 100)
                {
                    LabelErr.Text = "ERROR: Columns must sum to 100. Please recheck values.";
                }
    }

protected void checkTotal()
         {
             sum = syL + syC + syN;
         }


 protected void Button_Lock_Click(object sender, EventArgs e)
         {
             if (sum == 0)
             {
                 LabelErr.Text = "Cannot finalize.";

             }
else {
 conn.Open();
                 sqlCommand.ExecuteScalar();
                 conn.Close();
}



Is This A Good Question/Topic? 0
  • +

Replies To: var value resets in another method

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: var value resets in another method

Posted 03 August 2010 - 04:38 AM

Are you doing this across postbacks? If so, then this is your problem...

int sum = 0;



On EVERY postback, all initialization code(like above) is executed along with the Page_Load method(and then the corresponding event). Therefore, on every postback, sum gets set to 0 before any of the other events/methods get executed.

To persist data across postbacks, you can use the Session.

private void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        if (Session["sum"] == null)
            Session.Add("sum", 0);
    }
}

protected void GridView_A_RowDataBound(object sender, GridViewRowEventArgs e)
{
    int sum = checkTotal();
                
    if (sum != 0 && sum != 100 || sum > 100)
    {
         LabelErr.Text = "ERROR: Columns must sum to 100. Please recheck values.";
    }

    // puts the sum into the session
    Session["sum"] = sum;
}

// return the sum from this method
protected int checkTotal()
{
     return syL + syC + syN;
}


protected void Button_Lock_Click(object sender, EventArgs e)
{
    // gets the sum from the session.
    int sum = int.Parse(Session["sum"].ToString());

    if (sum == 0)
    {
         LabelErr.Text = "Cannot finalize.";
    }
    else 
    {
         conn.Open();
         sqlCommand.ExecuteScalar();
         conn.Close();
    }
}


Was This Post Helpful? 1
  • +
  • -

#3 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: var value resets in another method

Posted 03 August 2010 - 05:46 AM

thanks :D
Was This Post Helpful? 0
  • +
  • -

#4 megglz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 11
  • View blog
  • Posts: 414
  • Joined: 22-August 08

Re: var value resets in another method

Posted 03 August 2010 - 11:05 AM

just coming back to say that this worked :) thanks again!!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1