问题描述
嗨
实际上我改变了我的数据库。我在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]);
}
推荐答案
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未被用户代码处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!