任何想法为什么我的GetCompanies()函数不起作用?

时间:2022-09-18 12:39:33

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

它所做的就是输出'CHECK',我输入它以确保它实际上是在击中函数...(首先调用Proceed()然后调用GetCompanies())。

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/>");
        }
    }

}

6 个解决方案

#1


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

看起来你在不同的回发期间调用了这两种方法。如果是这种情况,那么您的公司数组将为空,因为您没有持久化(在会话,视图状态等)。

#2


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

GetCompanies()中的循环条件在开始时得到满足,并且永远不会执行任何循环。

#3


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.

每次回发都会重新实例化页面。您在Proceed调用中设置的值仅适用于处理该调用的对象的实例。因此,您再次回发并调用GetCompanies它再次得到total的初始值 - 0 - 并且不认为有任何工作要做。尝试在会话中保留总数并在调用GetCompanies时从那里检索它。确保GetCompanies在使用后将其从会话中删除,以便在不重置的情况下不会重复使用。

#4


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.

我可能在这里遗漏了一些东西,但是在设置“total”变量之前你会调用GetCompanies。所以GetCompanies中的for循环不会循环任何东西,因为“total”仍然设置为0。

#5


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.

在你的编码方式中你应该改进一些东西,但是第一,没有必要有Response.Write子句,带有断点的调试会告诉你究竟是什么问题。

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.

protected关键字是成员访问修饰符。受保护的成员可以从声明它的类中访问,也可以从声明该成员的类派生的任何类中访问。

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

你需要继承这门课吗?我不是这样的。

#6


Use the debugger. What is total set to before that loop?

使用调试器。该循环之前的总设置是多少?

#1


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

看起来你在不同的回发期间调用了这两种方法。如果是这种情况,那么您的公司数组将为空,因为您没有持久化(在会话,视图状态等)。

#2


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

GetCompanies()中的循环条件在开始时得到满足,并且永远不会执行任何循环。

#3


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.

每次回发都会重新实例化页面。您在Proceed调用中设置的值仅适用于处理该调用的对象的实例。因此,您再次回发并调用GetCompanies它再次得到total的初始值 - 0 - 并且不认为有任何工作要做。尝试在会话中保留总数并在调用GetCompanies时从那里检索它。确保GetCompanies在使用后将其从会话中删除,以便在不重置的情况下不会重复使用。

#4


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.

我可能在这里遗漏了一些东西,但是在设置“total”变量之前你会调用GetCompanies。所以GetCompanies中的for循环不会循环任何东西,因为“total”仍然设置为0。

#5


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.

在你的编码方式中你应该改进一些东西,但是第一,没有必要有Response.Write子句,带有断点的调试会告诉你究竟是什么问题。

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.

protected关键字是成员访问修饰符。受保护的成员可以从声明它的类中访问,也可以从声明该成员的类派生的任何类中访问。

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

你需要继承这门课吗?我不是这样的。

#6


Use the debugger. What is total set to before that loop?

使用调试器。该循环之前的总设置是多少?