Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/272.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 系统堆栈溢出异常无限循环_C#_Asp.net_Sql Server - Fatal编程技术网

C# 系统堆栈溢出异常无限循环

C# 系统堆栈溢出异常无限循环,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我收到System.Stack.Overflow.Exception,并显示一条消息: 确保没有无限循环或无限递归 但是我不知道这个代码有什么问题 public string Projects() { using (SqlConnection con = new SqlConnection(str)) { string htmlStr = ""; SqlCommand cmd = new SqlCom

我收到System.Stack.Overflow.Exception,并显示一条消息:

确保没有无限循环或无限递归

但是我不知道这个代码有什么问题

    public string Projects()
    {
        using (SqlConnection con = new SqlConnection(str))
        {
            string htmlStr = "";
            SqlCommand cmd = new SqlCommand("Sp_oldprojects", con);
            cmd.Parameters.AddWithValue("@AgenId", Session["AgencyId"].ToString());
            cmd.CommandType = CommandType.StoredProcedure;
            con.Open();
            SqlDataReader reader = cmd.ExecuteReader();
            while (reader.Read())
            {
                int ProjId = reader.GetInt32(0);
                string ProjTitle = reader["ProjTitle"].ToString();

                htmlStr += "<tr><td>" + ProjId + "</td>" +  ProjTitle + "<td><a href=\"OldProjView.aspx?pId=" + ProjId + "\" class=\"btn btn-small\" title=\"Detailed View\"><i class=\"icon-search\"></i></td>" + "</tr>";
            }

            return htmlStr;
        }
    }
调用堆栈中的消息: [外部代码]
已超过Visual Studio支持的最大堆栈帧数

你就快到了,你只是缺少一个读者;在你的while循环之后

 public string Projects()
{
    using (SqlConnection con = new SqlConnection(str))
    {
        string htmlStr = "";
        SqlCommand cmd = new SqlCommand("Sp_oldprojects", con);
        cmd.Parameters.AddWithValue("@AgenId", Session["AgencyId"].ToString());
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            int ProjId = reader.GetInt32(0);
            string ProjTitle = reader["ProjTitle"].ToString();

            htmlStr += "<tr><td>" + ProjId + "</td>" +  ProjTitle + "<td><a href=\"OldProjView.aspx?pId=" + ProjId + "\" class=\"btn btn-small\" title=\"Detailed View\"><i class=\"icon-search\"></i></td>" + "</tr>";
        }
        reader.Close();
        return htmlStr;
    }
}

看看哪个将通过DataReader

缺少两个using块,应该在循环中使用StringBuilder进行字符串连接。但我没有看到递归或无限循环。您的读卡器有多少行?您是否检查了存储过程本身,确定它不是问题的根源;或者它是什么索引错误发生在哪一行?我在这里没有看到任何消耗无限堆栈空间的东西。这个屏幕截图比以往任何时候都更清楚地表明,问题不在您放在问题顶部的代码中。您至少需要找到错误所在的实际代码段,以便给我们一个回答的机会。另外,请注意,不要将sp_uu用作命名存储过程的前缀。因为OP知道如何使用using语句,SqlDataReader是一次性的。在reader上使用using语句如何?同意@J.C,我只是添加了似乎缺少的部分。至于它将如何解决这个问题,我假设不处理读取器将导致无限递归。这是OP发行的一部分。抱歉,用户3811350,希望这是答案。发帖时在我的手机旁,因此无法测试。
 public string Projects()
{
    using (SqlConnection con = new SqlConnection(str))
    {
        string htmlStr = "";
        SqlCommand cmd = new SqlCommand("Sp_oldprojects", con);
        cmd.Parameters.AddWithValue("@AgenId", Session["AgencyId"].ToString());
        cmd.CommandType = CommandType.StoredProcedure;
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            int ProjId = reader.GetInt32(0);
            string ProjTitle = reader["ProjTitle"].ToString();

            htmlStr += "<tr><td>" + ProjId + "</td>" +  ProjTitle + "<td><a href=\"OldProjView.aspx?pId=" + ProjId + "\" class=\"btn btn-small\" title=\"Detailed View\"><i class=\"icon-search\"></i></td>" + "</tr>";
        }
        reader.Close();
        return htmlStr;
    }
}