views:

133

answers:

4

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?

A: 

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?

balexandre
+1  A: 

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

OneSHOT
Session("uid") in VB ;)
balexandre
Well spotted :-)
OneSHOT
A: 

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... :)

I think its quite probable the first part is inside the quote and the Session call is outside ther quote - it is only a partial copy/paste
ck
@ck Good point but if that is the case then the concatenation operators are still missing which would report an obvious syntax error in an IDE which is why i presumed that this wasn't the case.If it was a partial copy/paste from the class i would have expected to seewhere userid=" -)
OneSHOT
A: 

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

samjudson