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 tostring 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
waiting for the best answer
thanks in advance

VulpesPosted Aug 21, 2014, 8:11 PM
Abdullaziz Said saidPosted Aug 23, 2014, 3:10 PM
VulpesPosted Aug 22, 2014, 7:14 PM
Are you doing any authentication anywhere?
Abdullaziz Said saidPosted Aug 22, 2014, 12:40 PM
all pages
BTW this code in the admin masterpage :'(
VulpesPosted Aug 22, 2014, 9:44 AM
So basically we just checked if that was the case and only redirected to that page if it wasn't.
Abdullaziz Said saidPosted Aug 22, 2014, 9:16 AM
thanks in advance
Abdullaziz Said saidPosted Aug 21, 2014, 6:18 PM
This webpage has a redirect loop
VulpesPosted Aug 21, 2014, 4:21 PM
with this: