繁体   English   中英

asp.net c#中SqlDataReader中的IndexOutOfRange异常

[英]IndexOutOfRange exception in SqlDataReader in asp.net c#

我受够了这个错误。 在登录时,我正在对用户进行身份验证,然后在会话变量中加载一些重要信息。 我在 IIS 上托管我的应用程序,客户端计算机通过 IP 地址使用该应用程序。 这是我的 C# 代码...

protected void btnLogin_Click(object sender, EventArgs e)
    {
        Session["UserName"] = txtUserName.Text;
        string DefaultYear = GetDefaultFinYear();        
        if (DefaultYear != string.Empty)
        {
            DefaultYear = "connect" + DefaultYear;
            Connections.Init(DefaultYear);
            SqlDataAdapter adp = new SqlDataAdapter();
            SqlDataReader dr = null;
            try
            {
                adp = new SqlDataAdapter("CheckLogin_sp", Connections.Connection[Session["UserName"].ToString()]);
                adp.SelectCommand.Parameters.AddWithValue("@UserName", txtUserName.Text.Trim());
                adp.SelectCommand.Parameters.AddWithValue("@Pwd", txtPassword.Text.Trim());
                adp.SelectCommand.Parameters.AddWithValue("option", "Authenticate".Trim());
                adp.SelectCommand.CommandType = CommandType.StoredProcedure;
                if (Connections.Connection[Session["UserName"].ToString()].State == ConnectionState.Closed)
                {
                    Connections.Connection[Session["UserName"].ToString()].Open();
                }
                dr = adp.SelectCommand.ExecuteReader();

                if (dr.HasRows)
                {
                    while (dr.Read())
                    {                        
                        Session["Name"] = dr[0].ToString();
                        Session["CompanyName"] = dr[1].ToString();
                        Session["UserId"] = dr[2].ToString();
                        Session["Center"] = dr[3].ToString();
                        Session["ClientCode"] = dr[4].ToString();
                        Session["UserImage"] = dr[5].ToString();
                        Session["CurrentDatabase"] = dr[6].ToString();
                        Connections.BillReport = dr[7].ToString();
                        Connections.DuesReport = dr[8].ToString();
                        Connections.GeneralReport = dr[9].ToString();
                        Connections.PendingReport = dr[10].ToString();
                        Connections.RadiologyReport = dr[11].ToString();
                        Connections.HistoReport = dr[12].ToString();
                    }
                    Session["value"] = "admin";
                    Response.Redirect("~/Masters/home.aspx", false);
                }
                else
                {
                    MessageBox.Show("Invalid Password");
                    txtUserName.Text = string.Empty;
                }               
            }
            catch (Exception ex)
            {
                Response.Write(ex.ToString());
            }
            finally
            {
                Connections.Connection[Session["UserName"].ToString()].Close();
                adp.Dispose();
                dr.Close();
                dr.Dispose();        

            }
        }
        else
        {
            MessageBox.Show("Invalid UserName");
        }        
    }

并非每次出现此错误,但有时当多台计算机访问应用程序时,此错误会出现在以下行中

Session["Name"] = dr[0].ToString();

注意:-一旦发生此错误,只有在服务器计算机重新启动时才会解决。

我相信用事务将您的代码包装在存储过程中可以解决您的问题。

BEGIN TRY
    BEGIN TRANSACTION CheckLogin 

        --Existing SQL code

    COMMIT TRANSACTION CheckLogin 
END TRY
BEGIN CATCH
    ROLLBACK TRANSACTION CheckLogin 
END CATCH

从 Web 应用程序调用的所有存储过程都应使用事务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM