简体   繁体   中英

IndexOutOfRange exception in SqlDataReader in asp.net c#

I am fed up with this error. At the time of login I am authenticating user and then loading some important information in session variables. I hosted my application on IIS and client computers use the application through IP address. Here is my c# code...

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");
        }        
    }

Not every time this error occurs but sometimes when multiple computers access the application this error comes on following line

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

Note:- Once this error occurs it resolve only when the server computer restart.

I believe wrapping your code in the stored procedure with a transaction would fix your problem.

BEGIN TRY
    BEGIN TRANSACTION CheckLogin 

        --Existing SQL code

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

All stored procedures called from a web application should use transactions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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