异常数组索引超出范围 [英] exception-array index out of bound

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

问题描述

我为登录页面编写了以下代码:

I have written the following code for a login page:

SqlConnection con=new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=MLM;Integrated Security=True");
SqlCommand com=new SqlCommand("select UserName,Login_Password from RegistrationForm",con); 

con.Open();
SqlDataReader rs = com.ExecuteReader(); 

while (rs.Read()) 
{
   if ((rs[0].ToString() == T1.Text) && (rs[31].ToString()== T2.Text))
   {
      string s1 = T1.Text;
      Response.Redirect("HOME.aspx?UserName=" + s1);
      return;
   }
   else 
   {
      Response.Write("Unsuccessful Login");
      T2.Text = "";
      T2.Focus();
   }
}
rs.Close();
com.ExecuteNonQuery();
con.Close(); 


我得到的错误是数组索引超出范围....但是当我使用rs [1]而不是rs [31]时,错误就像登录失败.


The error I get is Array index out of bound....but when I take rs[1] instead of rs[31], then error is like an unsuccessful login.

推荐答案

manjusha写道:
manjusha s wrote:

.bt,当我使用rs [1]而不是rs [31]时,错误就像登录失败

.bt when i take rs[1] instead of rs[31],then error is like unsuccessful login



好吧,这就是当您输入错误密码时您的代码应该说的话. rs [2];这是发生的情况,

在这里检查数据库中是否存在用户名和密码组合



Well, that''s what your code is supposed to say when you type a wrong password. This is what happens, whit rs[2];

Here you check whether username and password combination are present in the database

if ((rs[0].ToString() == T1.Text) && (rs[31].ToString()== T2.Text))

,如果它们不存在,则计算机将执行else-part ;

If they don''t exist, then the computer will execute the else-part;

else<br />{<br />Response.Write("Unsuccessful Login");<br /><br />T2.Text = "";<br />T2.Focus();<br />}<br />

有您的消息.使用数据库中的密码不应该发生这种情况.

现在,当您仅将索引号更改为31时,您的代码就会损坏;该查询中没有31个字段,因此它尝试从内存中读取不存在的字段.

There is your message. Shouldn''t happen when you use a password that is in the database.

Now, when you simply change the number of the index to 31, your code becomes broken; there aren''t 31 fields in that query, so it tries to read from memory that ain''t there.

select UserName,Login_Password from RegistrationForm

返回两个字段,而不是31.

returns two fields, not 31.


你好,
我同意eddy vluggen的话,他说的没错,也请记住您正在使用.net,尽管通过查看您的代码,我觉得您使用c#肯定会检查数组的边界,而不像c边界检查不是他们的工作,因此请确保您的rs [31]存在...

谢谢与问候
Radix:rose:
hello,
i agree with eddy vluggen what he said is right also keep this in mind that you are using .net although by looking at ur code i feel ur using c# which will definitely check of the bounds of array, it''s not like c where bounding checking is not their so make sure that rs[31] of yours is present...

Thanks & Regards
Radix:rose:


sqldatareader RS应该具有31列,否则将发生异常.您只需传递列名即可检查值.
The sqldatareader RS Should have 31 columns otherwise exception will occur. you can simply pass on the column name to check the values.


这篇关于异常数组索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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