简体   繁体   English

SqlDataAdapter.Fill()上未处理的StackOverflowException

[英]StackOverflowException unhandled on SqlDataAdapter.Fill()

StackOverflowException was unhandled. 未处理StackOverflowException。 I need help on this. 我需要帮助。 I get the error on the line 我在网上得到错误

adp.Fill(ds)

Also I'm not sure why, but I can't remove throw , it says not all codes returning a value. 另外我不确定为什么,但是我不能删除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;
    }

First, I think the issue is probably in MasterCust. 首先,我认为问题可能出在MasterCust中。 I think that however that is defined may be causing your issues. 我认为,但是定义可能会导致您的问题。 If you update your question on how this is defined, that may shed some additional light. 如果您更新有关如何定义的问题,则可能会带来一些其他启示。

Second, you have a lot of extraneous code that could be confusing the issue. 其次,您有很多无关的代码可能会使这个问题感到困惑。 Here is what I think that you need to do to pare it down the bare minimum: 我认为这是您需要做的事情,以将其降低到最低限度:

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

Then your business access class: 然后是您的企业访问权限类:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM