locked
SQLDataReader (invalid attempt to read when no data is present) RRS feed

  • Question

  • User2064370778 posted

    I'm starting to learn .NET (3.5) with c# as the coding language, and i'm a little stuck as to why the query is not able to return data from a table, that i know exists.

    Im using Visual Web Developer 2008, and i've used the wizard to generate a quick login feature.  The initial script was able to login successfully, validating the user details with those entered in the textboxes..

    I've now altered the table to include and additional column "Level".  It's associated with the user account.  So based on the level of the account range (1-10) different options appear to the user on the main page.  

    Below is the code that i am using for the login validation, it's an edit; of the functioning login wizard that is supplied by VWB2008.

    The connection string is working correctly, which is stored in the web.config file.

    The code is as below with the problem highlighted in bold.

    The error returns "System.InvalidOperationException: Invalid attempt to read when no data is present.".  I've checked and double checked the case sensitivity, but to no avail.  Is the error indicating that the user account cannot be located, or that there is no column/data in the matching account?

    protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
        {
            string username = Login1.UserName;
            string pwd = Login1.Password;
            string strConn;
            string sqlUserName;
            strConn = WebConfigurationManager.ConnectionStrings["ConnectionLogin"].ConnectionString;
            SqlConnection Conn = new SqlConnection(strConn);

            try
            {  
                sqlUserName = "SELECT Level FROM tblLogin ";
                sqlUserName += " WHERE (Username ='" + username + "')";
                sqlUserName += " AND (Password ='" + pwd + "')";
                SqlCommand com = new SqlCommand(sqlUserName, Conn);
                Conn.Open();
                SqlDataReader myReader = com.ExecuteReader();

                   if (myReader.HasRows)
                    {
                        Session["UserAuthentication"] = username;
                        Session["uLevel"] = myReader["Level"];
                        Session.Timeout = 25;
                        myReader = null;
                        Conn.Close();
                        Response.Redirect("Main.aspx");
                    }
                    else
                    {
                        Session["UserAuthentication"] = "";
                    }
        }

    }

    Thanks for any help,

    Wednesday, January 26, 2011 11:49 AM

Answers

  • User-577741185 posted

      if (myReader.HasRows)

    Use: while (myReader.Read())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 26, 2011 12:26 PM

All replies

  • User-577741185 posted

      if (myReader.HasRows)

    Use: while (myReader.Read())

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, January 26, 2011 12:26 PM
  • User2064370778 posted

    that fixed it, many thanks!

    Wednesday, January 26, 2011 1:36 PM