Jake Gaston
Jake Gaston

Reputation: 211

ASP.net with C# List increment on postback

I have an odd problem. In my program, a user manipulates dropdown boxes and presses an ADD button to fill in a listbox with the selected items. Eventually, I send these items to a MYSQL database with an INSERT command. When the user is hitting the ADD button, the items are added to a generic List and the listbox, and on postback everything seems fine and I can add stuff all day long. However, when I hit the Submit/save button, to send the data to the server, the list keeps copying the added data over and over again to itself on every postback no matter what causes the postback. I can see the result in the lstbox, and I can see the List.count going up.

I have no code in my page_load section. I do use a session property thing to hold the List data though.

public List<string> activityProp
    {
        get
        {
            if (HttpContext.Current.Session["codelist"] == null)
            {
                HttpContext.Current.Session["codelist"] = new List<string>();
            }
            return HttpContext.Current.Session["codelist"] as List<string>;
        }
        set
        {
            HttpContext.Current.Session["codelist"] = value;
    }

}

and here is the code for the button. The sqlsetter() method just has a generic SQL command structure in it with an insert string. It is only fired from this button.

protected void cmdSave_Click(object sender, EventArgs e)
    {
        //this will use the 'test' class to do a rule check on the day
        test therules = new test();
        bool comp = therules.check(dayProp, timeProp, activityProp);

        if (comp == true)
        {
            lblComp.Text = "You're ok!";
            lblComp.ForeColor = Color.Green;
            lblError.Text = "";
            SQLdaysetter();
        }
        else
        {   
            string zeerror = therules.getError();
            lblComp.Text = "You're not ok!";
            lblError.Text = "at " + zeerror;
            lblComp.ForeColor = Color.Red;
            lblError.ForeColor = Color.Red;
        }

    }

SQL stuff:

using (MySqlConnection conn = new    MySqlConnection(ConfigurationManager.ConnectionStrings["theConnectionString"].ConnectionString))
            {
                conn.Open();
                using (MySqlCommand cmdIns = new MySqlCommand(sqlstate, conn))
                {
                    cmdIns.ExecuteNonQuery();
                    cmdIns.Dispose();

                }
                conn.Close();
            }

Maybe this stuff isn't even related to the problem, any ideas where to look?

Upvotes: 0

Views: 564

Answers (1)

Kris Krause
Kris Krause

Reputation: 7326

I had some things in the OnInit event and evidently they were getting run every time the page reloaded. I'm still not sure why

Jake, take 10 minutes and read up on the Page Life Cycle (especially the events section). It will save you valuable time in the future.

http://msdn.microsoft.com/en-us/library/ms178472.aspx

Upvotes: 1

Related Questions