IndexOutOfRangeException未被用户代码处理 [英] IndexOutOfRangeException was unhandled by user code

查看:93
本文介绍了IndexOutOfRangeException未被用户代码处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



实际上我改变了我的数据库。我在Login表中添加了更多字段。现在,当我尝试登录时,它将显示异常..IndexOutOfRangeException未被用户代码处理..如果(ds.Tables [0] .Rows.Count> 0)此行显示了erroe



我的ccode是: -

Hi
Actually I Alter my database. I am adding More fields in Login table. now when i am trying to logged in It will shows exception..IndexOutOfRangeException was unhandled by user code..erroe shows in if (ds.Tables[0].Rows.Count > 0)this line

My ccode is:-

protected void BUT_Login_Submit_Click(object sender, EventArgs e)
   {
       string query = "Select * from Emp_Roles_Rights where E_User_ID='" + TB_Login_User_Name.Text + "' and Password='" + TB_Login_Password.Text + "' and Block= 0 and E_Delete= 0";
       ds = dt.ExecuteDataSet(query);
       if (ds.Tables[0].Rows.Count > 0)
       {
           string passlength = ds.Tables[0].Rows[0]["Password"].ToString();
           byte[] password = System.Text.Encoding.ASCII.GetBytes(ds.Tables[0].Rows[0]["Password"].ToString());
           string pass = " ", pass1 = " ";

           for (int i = 0; i < passlength.Length; i++)
           {

               pass = pass + " " + Convert.ToString(password[i]);
           }
           byte[] con_pas1 = System.Text.Encoding.ASCII.GetBytes(TB_Login_Password.Text.Trim());
           for (int j = 0; j < passlength.Length; j++)
           {
               pass1 = pass1 + " " + Convert.ToString(con_pas1[j]);
           }

推荐答案

你好,

如果(ds.Tables [0]。 Rows.Count> 0)

1.首先检查

ds.Tables.Count> 0

2.然后检查

if(!ReferenceEquals(ds.Tables [0],null)&& ds.Tables [0] .Rows.Count> 0)
Hello ,
for this if (ds.Tables[0].Rows.Count > 0)
1. First check
ds.Tables.Count>0
2. Then check
if(! ReferenceEquals(ds.Tables[0],null) && ds.Tables[0].Rows.Count>0)


Quote:

IndexOutOfRangeException未被用户代码

IndexOutOfRangeException was unhandled by user code

处理>这意味着您尝试访问的索引不可用。

That means the index you are trying to access is not available.

if (ds.Tables[0].Rows.Count > 0)

此处如果 DataSet 为空, Tables [0] 会抛出此异常。



所以,你需要先检查一下查询以及从数据库中获取的内容。还使用参数化查询而不是内联查询,因为它易受SQL注入攻击。

Here if the DataSet is empty, the Tables[0] would throw this exception.

So, you need to first check the query and what it fetches from Database. Also use Parameterized query instead of inline queries, as it is vulnerable to SQL Injection.


这篇关于IndexOutOfRangeException未被用户代码处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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