Invalid attempt to read when no data is present

Aug 21 2014 3:01 PM
hello
i write c# code to check if the user type = 1 depended on value comes from datareader
but i got this error Invalid attempt to read when no data is present

this is my code

 

 

 

 

protected void Page_Load(object sender, EventArgs e)
    {
        string name = "";
        if (Session["usernames"] != "" & Session["usernames"] != null)
        {
            name = Session["usernames"].ToString();
            SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["soom_dbConnectionString"].ConnectionString);
            SqlCommand command = new SqlCommand("SELECT user_type FROM dbo.user_data where user_name='" + name + "'");
            command.Connection = connection;
            command.CommandType = CommandType.Text;
            connection.Open();
            SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
            if (reader.HasRows)
            {
                if (reader["user_type"] == "1")
                {
                    Response.Redirect("../admincp/Default.aspx");
                }
                else
                {
                    Response.Redirect("../Access_Denied.aspx");
                }
            }
           
                
        }
        
       
    }

and use this code also

 

 

string name = ""
if (Session["usernames"] != "" & Session["usernames"] != null)
 { name = Session["usernames"].ToString();
 SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["soom_dbConnectionString"].ConnectionString);
 SqlCommand command = new SqlCommand("SELECT user_type FROM user_data where user_name='"+name+"'");
 command.Connection = connection;
 command.CommandType = CommandType.Text;
 connection.Open(); SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection);
 if (reader.HasRows) { 
  reader.Read(); 
  if (reader["user_type"]== "1"
{ Response.Redirect("../admincp/Default.aspx");
   } else 
{ Response.Redirect("../Access_Denied.aspx");
}

 

 

when i use this code and iam sure that the user_type value is 1 it's redirect me to access_denied.aspx idk Why
and when i change the code to

string name = "";
 if (Session["usernames"] != "" & Session["usernames"] != null)
 { name = Session["usernames"].ToString();
 SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["soom_dbConnectionString"].ConnectionString);
 SqlCommand command = new SqlCommand("SELECT user_type FROM user_data where user_name='"+name+"'");
 command.Connection = connection; 
command.CommandType = CommandType.Text; 
connection.Open(); 
SqlDataReader reader = command.ExecuteReader(CommandBehavior.CloseConnection); 
if (reader.HasRows)
 {   reader.Read(); string type = reader["user_type"].ToString(); 
if (type == "1"
{ Response.Redirect("../admincp/Default.aspx");
 } else
 { Response.Redirect("../Access_Denied.aspx"); 
 
}

i enter infinite loop Frown | :(
waiting for the best answer
thanks in advance

Answers (8)