SqlDataAdapter.Fill()上未处理StackOverflowException

本文关键字:未处理 StackOverflowException Fill SqlDataAdapter | 更新日期: 2023-09-27 18:20:02

StackOverflowException未处理。我需要帮助。我收到线上的错误

adp.Fill(ds)

我也不知道为什么,但我不能删除throw,它说不是所有的代码都返回一个值。

    string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["dbCustConn"].ToString();
    string cmdStr = "Select * from MainDB";
    public DAL() // default parameter.  Use?
    {
    }
        public DataTable Load() // what is this for? (loads all the records from the database)
        {
            SqlConnection conn = new SqlConnection(connStr);
            //SqlCommand cmd = new SqlCommand(cmdStr, connStr);
            SqlDataAdapter adp = new SqlDataAdapter(cmdStr, connStr);  // SqlDataAdapater?  Load all?
            DataSet ds = new DataSet();
            try
            {
                adp.Fill(ds);
                return ds.Tables[0];
            }
            catch
            {
                throw;
            }
            finally
            {
                ds.Dispose();
                adp.Dispose();
                conn.Close();
                conn.Dispose();
            }
        }

        public DataTable Load() // what is this for? (loads all the records from the database)
        {
            SqlDataAdapter adp = new SqlDataAdapter(cmdStr, connStr);  // SqlDataAdapater?  Load all?
            DataSet ds = new DataSet();
            using(SqlConnection conn = new SqlConnection(connStr))
            {
                adp.Fill(ds);
                return ds.Tables[0];
            }
        }

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindGrid();
        }
    }
    private void BindGrid()
    {
        MasterCust.DataSource = GridDataSource();
        MasterCust.DataBind();
        //DetailCust.DataSource = ds2;
        //DetailCust.DataBind();
    }
    private DataTable GridDataSource()
    {
        BAL p = new BAL();
        DataTable dTable = new DataTable();
        try
        {
            dTable = p.Load();
        }
        catch (StackOverflowException ee)
        {
            string message = ee.Message.ToString();
        }
        finally
        {
            p = null;
        }
        return dTable;
    }

SqlDataAdapter.Fill()上未处理StackOverflowException

首先,我认为问题可能在MasterCust中。我认为,无论定义如何,都可能导致您的问题。如果你更新你关于这是如何定义的问题,这可能会提供一些额外的线索。

其次,您有很多无关的代码,这些代码可能会混淆问题。以下是我认为你需要做的事情,以将其削减到最低限度:

    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            if (!IsPostBack)
            {
                BindGrid();
            }
        } 
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
            // Note that this is for debug purposes only. Production code should log 
            // this exception somewhere so that it can be observed and dealt with
        }
    }
    private void BindGrid()
    {
        MasterCust.DataSource = BAL.Load();
        MasterCust.DataBind();
    }

然后您的业务访问类:

public class BAL
{ 
    private static string connStr = System.Configuration.ConfigurationManager.ConnectionStrings["dbCustConn"].ToString();
    private static string cmdStr = "Select * from MainDB";
    public static DataTable Load() // what is this for? (loads all the records from the database)
    {
        using (var adp = new SqlDataAdapter(cmdStr, connStr))
        {
            var ds = new DataSet();
            adp.Fill(ds);
            return ds.Tables[0];
        }
    }
}