I'm binding a menu from a database. In the SQL query, I am passing
where userid="System.Web.HttpContext.Current.Session["uid"].ToString()"
It is working, but for every login it is the same.
What can I do, please?
I'm binding a menu from a database. In the SQL query, I am passing
where userid="System.Web.HttpContext.Current.Session["uid"].ToString()"
It is working, but for every login it is the same.
What can I do, please?
How are you setting that Session uid in the first place?
are you really setting it? can you trace using a line by line debug and see since you click the Login (button?) where and with witch value are you setting that Session?
The code snippet you have seems a little off in that you have wrapped everything in quotes the line should be along the lines of
C#
string sql = "select a,b,c from XXX where userid=" + System.Web.HttpContext.Current.Session["uid"].ToString();
VB
Dim sql As String = "select a,b,c from XXX where userid=" & System.Web.HttpContext.Current.Session("uid").ToString()
This is presuming that the session entry uid has already been set.
Although considering the title of the post, while this is a seperate class it may be better to pass the values in the constructor of the class or set them on properties of the instantiated object rather than accessing the session from within the class! This would allow better testing of the code if you need to test somewhere other than in a web app such as a unit test.
HTH
OneSHOT
One possible problem is:
Looking at the example code you gave, you have the call to the current.session in quotes which means you are passing the string
System.Web.HttpContext.Current.Session["uid"].ToString()
in the where clause which means nothing in sql.
You need to append the value from the call to current.session to the sql string.
try...
where userid='" & System.Web.HttpContext.Current.Session("uid").ToString() & "'"
** edit ** OneSHOT gave the same answer while I was writing mine... :)
If you are trying to share Session data between both classic ASP and ASP.Net then it is not very easy, but is doable.
See the following MSDN page for full details: http://msdn.microsoft.com/en-us/library/aa479313.aspx