如何解决错误索引超出数组的界限。 [英] How Do I Solve The Error Index Was Outside The Bounds Of The Array.

查看:1221
本文介绍了如何解决错误索引超出数组的界限。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public string UpdateUserID(string updateID)
    {
        UpdatedBy = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.Substring(12);

        string sql = "select Update_UserID from Cwipuser.CWIP_Variances  ";
        SqlCommand cmd = new SqlCommand(sql, MyCon);
        MyCon.Open();
        SqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
          if (!rdr.IsDBNull(1))
            {
                updateID = rdr.GetString(1);
            }
              updateID = UpdatedBy;
        }
        MyCon.Close();
        return updateID;
    }

推荐答案

尝试给出如下内容



try to give like below

while (rdr.Read())
{
if (!rdr.IsDBNull(0))
{
updateID = rdr.GetString(0);
}
updateID = UpdatedBy;
}


您只从查询中返回一个字段Update_UserID ...因此您必须访问索引0而不是1.



即updateID = rdr.GetString(0)



IsNull函数相同。

$ / $


或者,如果UserIdentity.Name小于13个字符,则子串(12)可能抛出IndexOutOfRangeException(同样,基于0的索引意味着你正在开始从第13个字符开始)
You're returning only one field Update_UserID from the query...therefore you have to access index 0 not 1.

That is updateID = rdr.GetString(0)

Same for IsNull function.


Alternatively, IndexOutOfRangeException could be thrown by Substring(12) if UserIdentity.Name is shorter then 13 characters (again, 0-based index means you're starting from 13th character)


这篇关于如何解决错误索引超出数组的界限。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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