locked
Error: System.IndexOutOfRangeException RRS feed

  • Question

  • User1734666507 posted

    Hi

    I have a Login page which contains a textbox, a dropdownlist and a button.

    Upon the click event of the button, the text in the textbox and value of the selected dropdownlist will be written and stored into a database called Login.

    I need these 2 values to be carried through a session state and retrieve it anytime i need it.

    This is the code:

    protected void Button1_Click(object sender, EventArgs e)
    {
            string Name = TextBox1.Text;
            string Class = DropDownList1.SelectedItem.Text;
    
            //Connection for Insert
            SqlConnection connIns = new SqlConnection();
            connIns.ConnectionString = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
    
            SqlCommand cmdIns = new SqlCommand();
            cmdIns.CommandText = "INSERT INTO Login(Name, Class) VALUES ('" + Name + "','" + Class + "')";
            cmdIns.Connection = connIns;
            cmdIns.Parameters.AddWithValue("Name", Name);
            cmdIns.Parameters.AddWithValue("Class", Class);
            connIns.Open();
    
            int result = cmdIns.ExecuteNonQuery();
    
            connIns.Close();
    
    
            //Connection for Session
            SqlConnection connSess = new SqlConnection();
            connSess.ConnectionString = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
    
            connSess.Open();
            SqlCommand cmdSess = new SqlCommand("SELECT Name, Class FROM Login", connSess);
            SqlDataReader dr = cmdSess.ExecuteReader();
    
            while (dr.Read())
            {
                string dbName = dr["TextBox1"].ToString();
                string dbClass = dr["DropDownList1"].ToString();
    
                if (TextBox1.Equals(dbName) && DropDownList1.Equals(dbClass))
                {
                //  Session["timeStart"] = DateTime.Now.ToString();
                    Session["SessName"] = TextBox1;
                    Session["SessClass"] = DropDownList1;
                    Response.Redirect("FirstPage.aspx");
                }
    
                dr.Close();
                connSess.Close();
            }
        }
    
    
    The code is fine but after building and running the solution, i get this error:
    System.IndexOutOfRangeException: TextBox1
    Line 77: string dbName = dr["TextBox1"].ToString(); 

    Any help is greatly appreciated!


    Sunday, August 21, 2011 10:46 PM

Answers

  • User-573138384 posted

    Still it doesn't. You are selecting the same values what you are inserting. So why don't you just put the values in session while insert it self. This doesn't need any select from DB. I changed it. Have a look...

            protected void Button1_Click(object sender, EventArgs e)
            {
                string Name = TextBox1.Text;
                string Class = DropDownList1.SelectedItem.Text;
    
                SqlConnection connIns = new SqlConnection();
                SqlCommand cmdIns = new SqlCommand();
    
                try
                {
                    connIns.ConnectionString = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
    
    
                    cmdIns.CommandText = "INSERT INTO Login(Name, Class) VALUES (@Name, @Class)";
                    cmdIns.Connection = connIns;
                    cmdIns.Parameters.AddWithValue("@Name", Name);
                    cmdIns.Parameters.AddWithValue("@Class", Class);
                    connIns.Open();
    
                    cmdIns.ExecuteNonQuery();
    
                    Session["SessName"] = Name;
                    Session["SessClass"] = Class;
    
                    Response.Redirect("FirstPage.aspx");
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (connIns.State != System.Data.ConnectionState.Closed)
                    {
                        connIns.Close();
                    }
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2011 12:10 AM
  • User-573138384 posted

    The same way how you assigning them. But, Session returns object. Type cast it your type. In your case its sting so...

     string name = Session["SessName"].ToString();
     string class =
    Session["SessClass"].ToString();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2011 5:07 AM

All replies

  • User-573138384 posted

    SqlCommand cmdSess = new SqlCommand("SELECT Name, Class FROM Login", connSess);
    SqlDataReader dr = cmdSess.ExecuteReader();
    while (dr.Read())
    {
        string dbName = dr["TextBox1"].ToString();
        string dbClass = dr["DropDownList1"].ToString();

    See you select query. You are selecting Name and class form dataBase. So your result set have these two columns only. But see whay you are reading from data reader. You are trying to read TextBox1 and DropDownList1 columns from reult where it doesn't exists. Yo uhave only name and class. Chnage them to valid column names...


        string dbName = dr["Name"].ToString();
        string dbClass = dr["Class"].ToString();

    Sunday, August 21, 2011 11:15 PM
  • User1734666507 posted

    I changed the respective codes and i got this error!

    Invalid attempt to call Read when reader is closed.

    System.InvalidOperationException: Invalid attempt to call Read when reader is closed.

    Line 75: while (dr.Read()) 

     Must i open a dr before using the while loop?

    Sunday, August 21, 2011 11:30 PM
  • User-573138384 posted

    Your code is all juggled up. First you are inserting then you are selecting the same. Obviously that would be same. You have while loop and redirecting. What are you exactly trying to do? If you can clearly explain I can assist you better!

    Sunday, August 21, 2011 11:46 PM
  • User1734666507 posted

    Im so sorry for the confusion. Ok basically i was thinking of this

    1) Insert (Name and Class) into database

    2) Retrieve the values from the database and create a session using the values retrieved (the session is created in the if statement in the while loop).

    does these make any sense?

    Sunday, August 21, 2011 11:52 PM
  • User-573138384 posted

    Still it doesn't. You are selecting the same values what you are inserting. So why don't you just put the values in session while insert it self. This doesn't need any select from DB. I changed it. Have a look...

            protected void Button1_Click(object sender, EventArgs e)
            {
                string Name = TextBox1.Text;
                string Class = DropDownList1.SelectedItem.Text;
    
                SqlConnection connIns = new SqlConnection();
                SqlCommand cmdIns = new SqlCommand();
    
                try
                {
                    connIns.ConnectionString = ConfigurationManager.ConnectionStrings["pubsConnectionString"].ConnectionString;
    
    
                    cmdIns.CommandText = "INSERT INTO Login(Name, Class) VALUES (@Name, @Class)";
                    cmdIns.Connection = connIns;
                    cmdIns.Parameters.AddWithValue("@Name", Name);
                    cmdIns.Parameters.AddWithValue("@Class", Class);
                    connIns.Open();
    
                    cmdIns.ExecuteNonQuery();
    
                    Session["SessName"] = Name;
                    Session["SessClass"] = Class;
    
                    Response.Redirect("FirstPage.aspx");
                }
                catch (Exception)
                {
                    throw;
                }
                finally
                {
                    if (connIns.State != System.Data.ConnectionState.Closed)
                    {
                        connIns.Close();
                    }
                }
            }
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2011 12:10 AM
  • User1734666507 posted

    Thanks mate! seems to be working fine. Anyway, do you know how do i go about callling the session on other pages?

    Monday, August 22, 2011 3:04 AM
  • User-573138384 posted

    The same way how you assigning them. But, Session returns object. Type cast it your type. In your case its sting so...

     string name = Session["SessName"].ToString();
     string class =
    Session["SessClass"].ToString();
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, August 22, 2011 5:07 AM
  • User1734666507 posted

    Thanks mate you've been a great help! Have no problems doing the subsequent pages =)

    Monday, August 22, 2011 5:32 AM