Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
public void Authenticate(string Username, string Password)
{
string Encryptpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtpassword.Text, "SHA1");
string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using(SqlConnection con = new SqlConnection(cs))
{
SqlCommand cmd = new SqlCommand("spAuthenticateUsers",con);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Username",txtuname.Text);
cmd.Parameters.AddWithValue("@Password", Encryptpassword);
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
int retryattempts = Convert.ToInt32(dr["retryattempts"]); //
if(Convert.ToBoolean(dr["Accountlocked"]))
{
Label1.Text = "Account locked Please contact Administrator";
}
else if (retryattempts > 0)
{
int Attemptsleft = (4 - retryattempts);
Label1.Text = "Invalid Username or Password" + Attemptsleft.ToString() + "Attemptsleft";
}

else if (Convert.ToBoolean(dr["Authenticated"]))
{
FormsAuthentication.RedirectFromLoginPage(txtuname.Text, CheckBox1.Checked);
}

else
{
Label1.Text = "invalid Username/Password";
}
}
}
// this line iam getting the exception what does it mean?
int retryattempts = Convert.ToInt32(dr["retryattempts"]);
Help me out thanks in advance

and the stroed procedure is

Create Procedure sptblUserAuthentication
@Username nvarchar(50),
@Password nvarchar(50)
As
Begin
Declare @Accountlocked int
Declare @Count int
Declare @Retrycount bit
End
-- Declaration is finished--
Select @Accountlocked = Islocked from tblUsers Where Username=@Username

if(@Accountlocked = 1)
Begin
Select 1 as Accountlocked,0 as RetryAttempts,0 as Authenticated
End
Else
Begin
--Check if the username and password is match--
Select COUNT(Username) from tblUsers Where Username=@Username and Password=@Password
--if match found--
if(@Count = 1)
Begin
--Reset values --
Update tblUsers set RetryAttempts=0 Where Username=@Username
Select 0 as RetryAttempts,1 as Authenticated,0 as Accountlocked
End
Else
Begin
--if match not found--
Select @Retrycount = IsNull(RetryAttempts,0) from
tblUsers Where Username=@Username

Set @Retrycount = @Retrycount + 1
if(@Retrycount <= 3)
Begin
--if retry attempts are not completed--
Update tblUsers Set RetryAttempts = @Retrycount Where Username=@Username
Select 0 as Accountlocked,0 as Authenticated,@Retrycount as RetryAttempts
End
Else
Begin
--if retry attempts are not completed--
Update tblUsers set RetryAttempts=@Retrycount ,Islocked=1,LockDatetime=GETDATE() Where Username=@Username
Select 1 as Accountlocked,0 as Authenticated,0 as RetryAttempts
End
End
End
Posted
Updated 27-Oct-14 2:00am
v2
Comments
Prakriti Goyal 27-Oct-14 6:03am    
int retryattempts = Convert.ToInt32(dr["retryattempts"]);
Check the value of dr["retryattempts"] using quickwatch and let me know what value is coming while debugging the same.
raxhemanth 27-Oct-14 6:19am    
value is 0 when debugging. even for several time it has to increase but not getting increased?don't know why?

Prakriti Goyal 27-Oct-14 6:24am    
Kindly check here also,
SqlDataReader dr = cmd.ExecuteReader();
Quick watch on dr and check its value.
May be the parameters that you are passing @Username and @Password are with different name in the procedure.
raxhemanth 27-Oct-14 6:41am    
sqldatareader dr = cmd.ExecuteReader(); value is null
Prakriti Goyal 27-Oct-14 7:16am    
Your reader is null. Check the procedure once, check its parameters.

1 solution

Hi,

The System.IndexOutOfRangeException exception indicates that you are accessing an array or container with an invalid index. That is the index refers to an item that does not exist. in your code,
C#
int retryattempts = Convert.ToInt32(dr["retryattempts"]);
Check that dr for that index exists, otherwise you wil get this exception.

Also please refer to this : http://msdn.microsoft.com/en-us/library/system.indexoutofrangeexception%28v=vs.110%29.aspx[^]

Regards,
Praneet Nadkar
 
Share this answer
 
Comments
raxhemanth 27-Oct-14 7:05am    
the value of the retryattempts variable is 0 and the datareader value is null
[no name] 27-Oct-14 7:08am    
In that case, retryattempts is having a default value of integer, 0. Check if the stored procedure is returning data from the DB. Reader should not be null.
raxhemanth 27-Oct-14 7:58am    
this is the procedure i have written but i could not find any kind of error may be you can have a look so that you can have an idea to solve the problem aswell
Create Procedure sptblUserAuthentication
@Username nvarchar(50),
@Password nvarchar(50)
As
Begin
Declare @Accountlocked int
Declare @Count int
Declare @Retrycount bit
End
-- Declaration is finished--
Select @Accountlocked = Islocked from tblUsers Where Username=@Username

if(@Accountlocked = 1)
Begin
Select 1 as Accountlocked,0 as RetryAttempts,0 as Authenticated
End
Else
Begin
--Check if the username and password is match--
Select COUNT(Username) from tblUsers Where Username=@Username and Password=@Password
--if match found--
if(@Count = 1)
Begin
--Reset values --
Update tblUsers set RetryAttempts=0 Where Username=@Username
Select 0 as RetryAttempts,1 as Authenticated,0 as Accountlocked
End
Else
Begin
--if match not found--
Select @Retrycount = IsNull(RetryAttempts,0) from
tblUsers Where Username=@Username

Set @Retrycount = @Retrycount + 1
if(@Retrycount <= 3)
Begin
--if retry attempts are not completed--
Update tblUsers Set RetryAttempts = @Retrycount Where Username=@Username
Select 0 as Accountlocked,0 as Authenticated,@Retrycount as RetryAttempts
End
Else
Begin
--if retry attempts are not completed--
Update tblUsers set RetryAttempts=@Retrycount ,Islocked=1,LockDatetime=GETDATE() Where Username=@Username
Select 1 as Accountlocked,0 as Authenticated,0 as RetryAttempts
End
End
End

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