locked
Invalid attempt to call Read when reader is closed RRS feed

  • Question

  • User-1029010542 posted

    Everytime I run the page , im getting : 

    Invalid attempt to call Read when reader is closed , the strange thing is that the table is filled with the data , meaning that the everything worked OK , i was able to retrieve the data from one table and write it to another

    but why am i getting this error ... , below is my code in C# 

    protected void Page_Load(object sender, EventArgs e)
        {
            string ipaddress;
            ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
            if (ipaddress == "" || ipaddress == null)
            ipaddress = Request.ServerVariables["REMOTE_ADDR"];
            
    
            string ora = ipaddress;
            sqlconnet.Open();
    
    
            cmd = new SqlCommand("SELECT tbl_IPs.userid, tbl_IPs.IP, users.FirstName, users.LastName, users.userid AS useridO FROM tbl_IPs INNER JOIN users ON tbl_IPs.userid = users.userid WHERE (tbl_IPs.IP = @ora)", sqlconnet);
    
            cmd.Parameters.AddWithValue("@ora", ora);
    
         
            readIP = cmd.ExecuteReader();
    
    
            while (readIP.Read())
            {
    
                useridlabel.Text = Convert.ToString(readIP[0]);         
                Session["naf"] = (int)readIP["userid"];
                Session["LoginTime"] = DateTime.Now.ToString("dd/MM/yyyy");
                Session["naf"] = (int)readIP["userid"];
                Session["FN"] = (string)readIP["FirstName"];
                Session["LastName"] = (string)readIP["LastName"];
                Label1.Text = Convert.ToString(Session["naf"]);
                Session["IsAuthenticated"] = true;
                
                
                
        
               
                SqlCommand insertinfoIP = new SqlCommand("INSERT INTO [tbl_userinfo] ([FirstName],[LastName], " +
               "[IP],[logintime],[userid]) " +
               "VALUES (@FirstName,@LastName,@IP,@logintime,@userid)", sqlconnet);
    
    
                insertinfoIP.Parameters.Add("@FirstName", SqlDbType.NVarChar).Value = Session["FN"];
                insertinfoIP.Parameters.Add("@LastName", SqlDbType.NVarChar).Value = Session["LastName"];
                insertinfoIP.Parameters.Add("@IP", SqlDbType.NVarChar).Value = ipaddress;
                insertinfoIP.Parameters.Add("@logintime", SqlDbType.DateTime).Value = System.DateTime.Now.ToString();
                insertinfoIP.Parameters.Add("@userid", SqlDbType.Int).Value = Session["naf"];
    
    
    
                readIP.Close();
                insertinfoIP.ExecuteNonQuery();
                sqlconnet.Close();
               
            }

     

    Thursday, April 2, 2015 2:52 AM

Answers

  • User-821857111 posted

    Try moving readIP.Close() to after the ExecuteNonQuery call. If that doesn't, is readIP used elsewhere? It's not declared in the Page_Load event handler.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 3:24 AM
  • User-1716253493 posted

    I guess you should not close connection inside while{...}

    //open
    while(..)
    {
    
    
    }
    //close
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 3:44 AM

All replies

  • User-821857111 posted

    Try moving readIP.Close() to after the ExecuteNonQuery call. If that doesn't, is readIP used elsewhere? It's not declared in the Page_Load event handler.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 3:24 AM
  • User-1716253493 posted

    I guess you should not close connection inside while{...}

    //open
    while(..)
    {
    
    
    }
    //close
    

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, April 2, 2015 3:44 AM
  • User-821857111 posted

    I guess you should not close connection inside while{...}

    Yes. Ahem. That would cause it.

    Thursday, April 2, 2015 4:12 AM