Friday, May 6, 2011

Any ideas why my GetCompanies() function isn't working?

All it does is output 'CHECK' which I put in to make sure it was actually hitting the function... (Proceed() is called first then GetCompanies() after).

using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using Mexico.Data;

public partial class admin_tools_Optimus : System.Web.UI.Page
{
    protected int step = 0;
    protected string[] companies = new string[260];
    protected string[,] courses = new string[260,50];
    protected int total = 0;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Proceed(object sender, EventArgs e)
    {
        DataSet getCompanies = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, CommandType.StoredProcedure, "Companies_All_Get");

        int counter = 0;

        foreach (DataRow dr in getCompanies.Tables[0].Rows)
        {
            lstData.Items.Add(dr["companyid"].ToString() + ": " + dr["companyname"].ToString());
            companies[counter] = dr["companyid"].ToString();
            counter++;
        }
        lblCurrentData.Text = counter.ToString() + " companies ready, click next to get all company courses.";
        total = counter;
        btnNext.Visible = false;
        btnNext1.Visible = true;
    }


    protected void GetCompanies(object sender, EventArgs e)
    {
        Response.Write("CHECK");
        for (int i = 0; i < total; i++)
        {
            DataSet getBundles = SqlHelper.ExecuteDataset(ConfigurationManager.ConnectionStrings["connstr"].ConnectionString, CommandType.StoredProcedure, "CompanyCourses_ByCompanyID_Get_Sav", new SqlParameter("@companyid", companies[i]));

            int counter = 0;

            foreach (DataRow dr in getBundles.Tables[0].Rows)
            {
                courses[i, counter] = dr["bundleid"].ToString();
                counter++;
            }

            string allID = "";

            allID += courses[i, 0];

            for (int ii = 0; ii < counter; ii++)
            {
                allID += "," + courses[i, ii];
            }
            Response.Write(allID + " <br/>");
        }
    }

}
From stackoverflow
  • Use the debugger. What is total set to before that loop?

  • It looks like you're calling the two methods during different postbacks. If that is the case, then your companies array will be empty because you are not persisting it (in session, viewstate, etc.).

    curtisk : bingo! +1, total is zero in the for loop every time
    shogun : thanks a million!
  • The page is instantiated anew on every postback. The value that you set in the Proceed call only applies to the instance of the object that handles that call. Thus, you post back again and GetCompanies is invoked it gets the initial value of total again -- 0 -- and doesn't think there is any work to do. Try persisting the total number in the Session and retrieve it from there when GetCompanies is called. Make sure to have GetCompanies remove it from the session after it is used so that it doesn't reused without being reset.

  • I might be missing something here, but you call GetCompanies before you set the "total" variable. So the for loop inside GetCompanies won't loop over anything since "total" is still set to 0.

  • Your loop condition within GetCompanies() is met at the start and never executes any loops.

  • there are some things that you should improve in your codding-way-to-do-things, but 1st, there is no need to have Response.Write clauses, a debug with breaking points will tell you exactly what the problem is.

    first, stop protecting your global variables, make them private if you don't want to expose them outside your class.

    The protected keyword is a member access modifier. A protected member is accessible from within the class in which it is declared, and from within any class derived from the class that declared this member.

    Do you need to inherit this class? I don't thing so.

0 comments:

Post a Comment