如何解决System.IndexOutOfRangeException:retryattempts?这是什么意思? [英] how to solve System.IndexOutOfRangeException: retryattempts ? and what does it mean?

查看:101
本文介绍了如何解决System.IndexOutOfRangeException:retryattempts?这是什么意思?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

public void Authenticate(string Username,string Password)

{

string Encryptpassword = FormsAuthentication.HashPasswordForStoringInConfigFile(txtpassword.Text,SHA1);

string cs = ConfigurationManager.ConnectionStrings [DBCS]。ConnectionString;

使用(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 =帐户已锁定请联系管理员;

}

否则if(retryattempts> 0)

{

int Attemptsleft =(4 - retryattempts);

Label1.Text =无效的用户名或密码+ Attemptsleft.ToString()+Attemptsleft;

}



else if(Convert.ToBoolean(dr [Authenticated]))

{

FormsAuthentication.RedirectFromLoginPage(txtuname.Text,CheckBox1。已检查);

}



其他

{

Label1.Text =无效的用户名/密码;

}

}

}

//此行我得到异常是什么意思?

int retryattempts = Convert.ToInt32(dr [retryattempts]);

请提前帮助我谢谢



和stroed程序是



创建程序sptblUserAuthentication

@Username nvarchar(50) ,

@Password nvarchar(50)

As

开始

声明@Accountlocked int

声明@Count int

声明@Retrycount位

结束

- 声明已完成 -

选择@Accountlocked =从tblUsers中锁定其中用户名= @用户名



if(@Accountlocked = 1)

开始

选择1作为Accountlocked,0作为RetryAttempts,0作为Authenticated

结束

否则

开始

- 检查用户名和密码是否匹配 -

选择COUNT(用户名)来自tblUsers用户名= @用户名和密码= @密码

- 如果找到匹配 -

if(@Count = 1)

开始

- 重置值 -

更新tblUsers设置RetryAttempts = 0其中Username = @ Username

选择0作为RetryAttempts,1 as Authenticated,0 as Accountlocked

结束

否则

开始

- 如果找不到匹配 -

选择@Retrycount = IsNull(RetryAttempts,0)来自

tblUsers其中用户名= @用户名



设置@ Retrycount = @Retrycount + 1

if(@Retrycount< = 3)

开始

- 如果重试尝试没有完成 -

更新tblUsers设置RetryAttempts = @Retrycount Where Username = @用户名

选择0作为帐户锁定,0作为已验证,@ Retrycount作为RetryAttempts

结束

否则

开始

- 如果重试尝试没有完成 -

更新tblUsers设置RetryAttempts = @ Retrycount,Islocked = 1,LockDatetime = GETDATE()其中Username = @ Username

选择1作为账户锁定,0作为认证,0作为RetryAttempts

结束

结束

结束

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

推荐答案

您好,



System.IndexOutOfRangeException异常表示您正在访问具有无效索引的数组或容器。也就是索引指的是一个不存在的项目。在您的代码中,
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,
int retryattempts = Convert.ToInt32(dr["retryattempts"]);

检查该索引的dr是否存在,否则您将获得此异常。



另请参阅: http ://msdn.microsoft.com/en-us/library/system.indexoutofrangeexception%28v=vs.110%29.aspx [ ^ ]



问候,

Praneet Nadkar

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


这篇关于如何解决System.IndexOutOfRangeException:retryattempts?这是什么意思?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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