Click here to Skip to main content
15,892,199 members
Please Sign up or sign in to vote.
4.25/5 (3 votes)
See more:
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(); 

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.
Posted
Updated 18-Feb-11 5:16am
v2

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


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))
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 />
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
returns two fields, not 31.

 
Share this answer
 
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:
 
Share this answer
 
The sqldatareader RS Should have 31 columns otherwise exception will occur. you can simply pass on the column name to check the values.
 
Share this answer
 
I would say do comparison as below.
C#
if ((rs["UserName"].ToString().ToLower() == T1.Text.ToLower())            &&(rs["Login_Password"].ToString()== T2.Text))

The statement is pretty simple.
1. Its readable as well as lets you know, what you are comparing.
2. converts username to lower and then compares. Advantage of this is, user need not to keep username letter cases to keep in mind.
 
Share this answer
 
this will work properly
if ((rs[0].ToString() == T1.Text) && (rs[1].ToString()== T2.Text))
 
Share this answer
 

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