代码之家  ›  专栏  ›  技术社区  ›  Hua Deng

page_load中的调用方法导致adapter.fill()上出现StackOverflowException,但在其他地方调用时没有问题

  •  0
  • Hua Deng  · 技术社区  · 1 年前

    我现在很困惑。 我有多个从sql连接绑定gridview的方法。

    当我从下拉索引中调用这些更改时,它运行良好。

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
      int machineid = int.Parse(DropDownList1.SelectedValue);
      try { 
      connection.Open();
      EquimentBind(machineid);
          
      }
     
      finally { connection.Close(); }
    
    }
    

    但是,当我从page_load调用方法并从URL传递id时,它总是在adapter.fill(dataTable)步骤中给我StackOverflowException。

     protected void Page_Load(object sender, EventArgs e)
        {
      
    
      if (!IsPostBack)
          {
        string station_id = Request.QueryString["station_id"];
       
    
        if (station_id != null)
            {
             
          DropDownList1.Visible = false;
          int machineid = int.Parse(station_id);
          try
          {
            if (connection.State != ConnectionState.Open)
            {
              connection.Open();
            }
            EquimentBind(machineid);
           
          }
    
          finally { connection.Close(); }
        }   
            else
            {
             
              try
                {
                  if (connection.State != ConnectionState.Open) { connection.Open(); }
                  SqlDataAdapter adapter = //My sql command
                  DataTable stalist = new DataTable();
                  adapter.Fill(stalist);
                  DropDownList1.DataSource = stalist;
                  DropDownList1.DataTextField = "name";
                  DropDownList1.DataValueField = "Id";
                  DropDownList1.DataBind();
    
                }
    
                finally { connection.Close(); }
                DropDownList1.SelectedValue = Convert.ToString('0');
    
          
            }
            
           
    
          }
    

    以下是我的数据绑定方法:

     protected void EquimentBind(int machineid)
    {
      
    
     
      SqlCommand Sqlcommand = // my sql command
      SqlCommand command = Sqlcommand;
      SqlDataAdapter sd = new SqlDataAdapter(command);
      DataTable dt1 = new DataTable();
      sd.Fill(dt1);
      GV_Equipments.DataSource = dt1;
      GV_Equipments.DataBind();
      command.ExecuteNonQuery();
      dt1.Clear();
     
    
    }
    

    StackOverflowExec选项总是在sd.Fill(dt1)行引发。

    我做错了什么?

    0 回复  |  直到 1 年前
        1
  •  0
  •   Matt    1 年前

    StackOverflowException可能是因为EquimentBind方法在无限循环中被递归调用。当由于数据绑定过程导致回发而重复触发Page_Load事件时,可能会发生这种情况。

    当station_id为null时,从Page_Load事件中删除数据绑定逻辑,因为这是不必要的,并且会导致递归调用。您可以简化Page_Load事件,如下所示:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string station_id = Request.QueryString["station_id"];
    
            if (station_id != null)
            {
                DropDownList1.Visible = false;
                int machineid = int.Parse(station_id);
                try
                {
                    if (connection.State != ConnectionState.Open)
                    {
                        connection.Open();
                    }
                    EquimentBind(machineid);
                }
                finally
                {
                    connection.Close();
                }
            }
            else
            {
                try
                {
                    if (connection.State != ConnectionState.Open)
                    {
                        connection.Open();
                    }
                    SqlDataAdapter adapter = //My sql command
                    DataTable stalist = new DataTable();
                    adapter.Fill(stalist);
                    DropDownList1.DataSource = stalist;
                    DropDownList1.DataTextField = "name";
                    DropDownList1.DataValueField = "Id";
                    DropDownList1.DataBind();
                }
                finally
                {
                    connection.Close();
                }
                DropDownList1.SelectedValue = "0";
            }
        }
    }
    

    修改DropDownList1_SelectedIndexChanged事件以处理这两种情况,即当从下拉列表更改选择时和从URL加载选择时:

    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int machineid = int.Parse(DropDownList1.SelectedValue);
        try
        {
            connection.Open();
            EquimentBind(machineid);
        }
        finally
        {
            connection.Close();
        }
    }