Click here to Skip to main content
15,889,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
C#
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
C#
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
C#
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
Updated 20-Aug-14 16:53pm
v2
Comments
[no name] 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

if (reader.HasRows)

{
reader.read();
if (reader
[ "user_type" ] == "1" )
 
Share this answer
 
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
if you have more than 1 result, you can do

C#
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read()) {
  // read data
}
 
Share this answer
 
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900