Click here to Skip to main content
15,893,814 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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:-
C#
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]);
           }
Posted
Comments
Member 11221185 13-Nov-14 1:43am    
Please solve this exception
Sergey Alexandrovich Kryukov 13-Nov-14 1:53am    
In what line? Use the debugger; it will tell you all you need.
—SA
Member 11221185 13-Nov-14 1:57am    
if (ds.Tables[0].Rows.Count > 0) this line i got exception. i used debugger. query fetch the username & password after that debugger stop on this line.
Tomas Takac 13-Nov-14 3:18am    
So there is no table in your dataset. So you need to focus there.

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)
 
Share this answer
 
Comments
Member 11221185 13-Nov-14 2:58am    
Its not solved..when I used first check then compiler directly goes to User Id & password is wrong. but actual user Id & password is correct.
2 check show the same error IndexOutOfRangeException was unhandled by user code.
Quote:
IndexOutOfRangeException was unhandled by user code
That means the index you are trying to access is not available.
C#
if (ds.Tables[0].Rows.Count > 0)
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.
 
Share this answer
 
Comments
Member 11221185 13-Nov-14 3:46am    
See First i am trying to run the application then it was successfully.then i changed in database means Added more field after that its not working And query fetch the username & password from database shown in debugger also.
Then your query must be wrong. First run this query in the Management studio and check what it returns.
Member 11221185 13-Nov-14 5:55am    
Select * from Emp_Roles_Rights where E_User_ID='admin'
and Password='123456' and Block= 0 and E_Delete= 0
this my query which given code earlier. And its run successfully. following output. But not work in Application
100 Admin 123456 123456 1 1 1 1 1 1 1 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 100 2013-03-18 15:58:21.250 1 1 1 1 1 1 1 1 1 1 1
First of all, please change your code to parameterized query. Then debug and see what is the query generated by the code. Then see where exactly it is executing. Database Connection String etc...
Member 11221185 15-Nov-14 2:47am    
That problem Has been Solved. thanks.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900