在SqlDataAdapter.Fill()上未处理StackOverflowException

时间:2012-01-10 01:19:44

标签: c# asp.net sql

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;
    }

1 个答案:

答案 0 :(得分:3)

首先,我认为问题可能出在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];
        }
    }
}