Click here to Skip to main content
15,917,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I am working on a program which should redirect to specific page according to the user logged in.my program consisting of 4 types of users.if the user is of type "user" the program should redirect him to his viewpage.that is he can view only his details.since there are multiple users in the program,each user is identified by his userid which is unique.

following is the code i wrote to redirecting to the user's page using 'Eval'.

'str' is a string which holds the value returned by the function.(i wrote one function to determine the type of user).i am checking if the user is of type "user",then getting the corresponding userid which is unique,from the database and redirecting to view page.here since i used eval.its giving me error in the last line like "Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control."
C#
if (str == "user")
{
  SqlConnection con = new SqlConnection();
  SqlCommand cmd = new SqlCommand();
  con = new SqlConnection(@"Data Source=CSZ-PCS43132\SQLEXPRESS;Initial Catalog=test;Integrated Security=True");
  con.Open();
  var query = "SELECT userid FROM logins2 WHERE loginid = @loginid AND password1 = @password AND usertype = " + str ;
  cmd = new SqlCommand(query, con);
  cmd.Parameters.AddWithValue("@loginid", txtusername .Text );
  cmd.Parameters.AddWithValue("@password",txtpassword .Text );
  cmd.ExecuteNonQuery();  
                
  Response.Redirect("LocalHRviewdetails.aspx?ID="+ Eval(query));
}

Can anyone pls help me?
Posted
Updated 28-Mar-12 20:53pm
v2

1 solution

I don't know which Eval method you are using, but you don't use it on a query string - you should be using the response from the database.
Since you have a SELECT query, which returns a single value, you could use the ExecuteScalar or ExecuteReader function to retrieve the user id, but ExecuteNonQuery will return the number of rows affected - which your code ignores anyway. If you only want the ID, then change:
C#
cmd.ExecuteNonQuery();
Response.Redirect("LocalHRviewdetails.aspx?ID="+ Eval(query));

To:
C#
int userId = cmd.ExecuteScalar();
Response.Redirect("LocalHRviewdetails.aspx?ID="+ userId.ToString());


BTW: Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]
 
Share this answer
 
Comments
amaljosep 29-Mar-12 3:29am    
it works for me.....thanks a lot for ur help...
Monjurul Habib 30-Mar-12 4:48am    
5!

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