发生了'system.argumentoutofrangeexception'类型的例外 [英] An exception of type 'system.argumentoutofrangeexception' occurred

查看:330
本文介绍了发生了'system.argumentoutofrangeexception'类型的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

mscorlib.dll中发生了'System.ArgumentOutOfRangeException'类型的异常但未在用户代码中处理附加信息:索引超出范围。必须是非负数且小于集合的大小。

  public   string  [] GetData( string 查询)
{
// string [] ColumnData = new string [3];
List< string> ColumnData = new List< string>();
con.Open();

尝试
{
SqlCommand cmd = new 的SqlCommand(查询,CON);
SqlDataReader rd = cmd.ExecuteReader();
while (rd.Read())
{
ColumnData [ 0 ] = rd.GetString(rd.GetOrdinal( EmployeeName));
ColumnData [ 1 ] = rd.GetString(rd.GetOrdinal( 移动));
ColumnData [ 2 ] = rd.GetString(rd.GetOrdinal( 地址));
}

}
catch (例外情况)
{
ex.ToString( );
}
最后
{
con.Close();
}
return ColumnData.ToArray();
}





我的尝试:

。问题是没有检测到。

解决方案

Quote:

ArgumentOutOfRange

这个错误意味着您要么尝试访问数组中不存在的元素,要么SQL请求的答案为空,并且您获得的是NULL值而不是预期的对象。

您必须检查您收到预期的对象或数组作为答案。



要确切了解附加内容和位置,请使用调试器。



调试器允许你逐行跟踪执行,检查变量,你会发现有一点它会停止你所期望的。

调试器 - 维基百科,免费的百科全书 [ ^ ]

掌握Visual Studio 2010中的调试 - 初学者指南 [ ^ ]


您需要使用:

 ColumnData.Add(


An exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll but was not handled in user code Additional information: Index was out of range. Must be non-negative and less than the size of the collection.

public string[] GetData(string Query)
        {
            //string[] ColumnData = new string[3];
            List<string> ColumnData = new List<string>();
            con.Open();
           
            try
            {
                SqlCommand cmd = new SqlCommand(Query,con);
                SqlDataReader rd = cmd.ExecuteReader();
                while(rd.Read())
                {
                     ColumnData[0] = rd.GetString(rd.GetOrdinal("EmployeeName"));
                     ColumnData[1] = rd.GetString(rd.GetOrdinal("Mobile"));
                     ColumnData[2] = rd.GetString(rd.GetOrdinal("Address"));
                }
               
            }
            catch (Exception ex)
            {
                 ex.ToString();
            }
            finally
            {
                con.Close();
            }
            return ColumnData.ToArray();
        }



What I have tried:

not going inside while loop. problem is not detecting.

解决方案

Quote:

ArgumentOutOfRange

This error mean that either you try to access an element that does not exist in the array, or the answer to your SQL request was empty and you got a NULL value instead of the expected object.
You must check that you received the expected object or array as answer.

To see exactly what append and where, use the debugger.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Debugger - Wikipedia, the free encyclopedia[^]
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]


You need to use:

ColumnData.Add(string)


这篇关于发生了'system.argumentoutofrangeexception'类型的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆