Click here to Skip to main content
12,503,679 members (57,620 online)
Rate this:
 
Please Sign up or sign in to vote.
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");
                }
            }
           
                
        }
        
       
    }

so can you plz help me with this error :( thanks in advanced


#Improving the question
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");
//Response.Redirect("../Access_Denied.aspx");
}
else
{
Response.Redirect("../Access_Denied.aspx");
//Response.Redirect("../admincp/Default.aspx");
}
}

i enter infinite loop :( i hate my self i can't focus
waiting for the best answer
thanks in advance
Posted 20-Aug-14 15:36pm
Updated 20-Aug-14 16:53pm
v2
Comments
Wes Aday 20-Aug-14 22:08pm
   
Your reader needs to read before it will point to valid data, if any.
Abdullaziz Said 21-Aug-14 13:52pm
   
any solution or any help plz
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

if (reader.HasRows)

{
reader.read();
if (reader
[ "user_type" ] == "1" )
  Permalink  
Comments
Abdullaziz Said 20-Aug-14 22:52pm
   
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");
//Response.Redirect("../Access_Denied.aspx");
}
else
{
Response.Redirect("../Access_Denied.aspx");
//Response.Redirect("../admincp/Default.aspx");
}
}
i enter infinite loop :( i hate my self i can't focus
waiting for the best answer
thanks in advance
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

if you have more than 1 result, you can do

SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
  // read data
}
  Permalink  
Comments
Abdullaziz Said 21-Aug-14 18:27pm
   
the same error
This webpage has a redirect loop

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web02 | 2.8.160919.1 | Last Updated 22 Aug 2014
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100