c# - 用户代码在 for 循环中未处理 IndexOutOfRangeException

标签 c# asp.net sql-server stringbuilder

我已经研究了这里的其他答案,但没有一个真正适合我的情况。我已经在数据库中添加了一个表,但现在出现此错误。事情是,它在别人的电脑上工作,但在我的电脑上,我收到以下错误。附:我的连接很好(以防你走神):

IndexOutOfRangeException 未被用户代码处理

An exception of type `System.IndexOutOfRangeException` occurred in System.Data.dll but was not handled in user code

Additional information: Cannot find table 0.

有问题的代码是在 for 循环中初始化 int i = 0

这是我的代码

 protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        string strSQLconnection = "Data Source=LOTUS;Initial Catalog=PMRS;Integrated Security=True";
        SqlConnection sqlConn = new SqlConnection(strSQLconnection);

        SqlCommand cmd = new SqlCommand();
        cmd.CommandText = "select * from PMRS.dbo.BOARDMEMBERS order by ORDERID";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConn;

        SqlDataAdapter sa = new SqlDataAdapter();

        try
        {
            sqlConn.Open();
            sa.SelectCommand = cmd;
            sa.Fill(ds);
            int rCount = ds.Tables[0].Rows.Count;
        }
        catch (Exception ex)
        {
            Response.Write(ex);
            sqlConn.Close();
            sqlConn.Dispose();
        }
        finally
        {
            sqlConn.Close();
            sqlConn.Dispose();
        }

        StringBuilder sb = new StringBuilder();

        for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
        {
            if (ds.Tables[0].Rows[i]["ID"].ToString() != "2")
            {
                sb.Append("<div class=\" col-md-6 \">");
                sb.Append("<img src=\"../images/board/" + ds.Tables[0].Rows[i]["IMAGE"].ToString() + "\" /> ");
                sb.Append("<h3>" + ds.Tables[0].Rows[i]["TITLE"].ToString() + "</h3> ");
                sb.Append("<h4>" + ds.Tables[0].Rows[i]["FNAME"].ToString() + " " + ds.Tables[0].Rows[i]["MI"].ToString() + " " + ds.Tables[0].Rows[i]["LNAME"].ToString() + "</h4>");
                sb.Append("<p>" + ds.Tables[0].Rows[i]["BIO"].ToString() + "</p> ");
                sb.Append("<p>" + ds.Tables[0].Rows[i]["SUFFIX_PREFIX"].ToString() + "</p> ");
                sb.Append("</div>");
            }
        }
        Literal1.Text = sb.ToString();
    }

我是一名前端开发人员,再次接触服务器端编程,在这里对我来说很轻松。给宝宝解释一下。哈哈谢谢

最佳答案

您收到错误是因为您试图访问 ds.Tables[0],但 ds.Tables 没有任何元素。在访问 ds.Tables[0] 之前,您必须检查 ds.Tables 是否为 null 以及 ds.Tables 是否有任何元素

StringBuilder sb = new StringBuilder();

if (ds.Tables != null && ds.Tables.Count > 0)
{
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++) 
    {
        ....
    }
    Literal1.Text = sb.ToString();
}
else
{
    // do something when ds.Tables is null or ds.Tables doesn't have any elements
}

关于c# - 用户代码在 for 循环中未处理 IndexOutOfRangeException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27476944/

相关文章:

php - mssql_connect 不会连接同一 SQL Server 上的不同数据库

javascript - 如何从 HTML-PhP 的表中的查询列表中运行特定查询

c# - 使用 C# 如何清理 MSMQ 消息格式以使用 C++ IXMLDOMDocument2

c# - Linq-to-SQL WHERE IN 语句返回的内容超出预期

asp.net - web.config 中的 "non-www to www"和 "https"重写规则,但不是 localhost ASP.NET MVC

asp.net - jqgrid 添加行并将数据发送到 webservice 进行插入

SQL查询获取状态成功的记录

c# - 无法将随机生成的 int 从 Controller 写入 DB

c# - 如何使用 Windows 应用商店 API 安排 "egg timer"?

c# - 从 ConnectionStringSettings 获取用户和密码