How can I use session in ASP class file?

I am binding a menu from a database. In the SQL query, I am passing

where userid="System.Web.HttpContext.Current.Session["uid"].ToString()"

      

It works, but for every input it is the same.

What can I do please?

0


a source to share


4 answers


For a piece of code that you don't have enough, in that you have wrapped everything in quotes, the string should be in strings

FROM#

string sql = "select a,b,c from XXX where userid=" + System.Web.HttpContext.Current.Session["uid"].ToString();

      

V. B.

Dim sql As String = "select a,b,c from XXX where userid=" & System.Web.HttpContext.Current.Session("uid").ToString()

      



It assumes that the uid of the session has already been set.

While considering the post header while this is a separate class, it might be better to pass values ​​in the constructor of the class or set them in the properties of the object object rather than accessing the session from within the class! This would allow for better code testing if you need to test somewhere other than a web application such as a unit test.

NTN

Oneshot

+1


a source


How do you set this session uid in the first place?



Are you really installing it? can you trace with debug line by line and see how you press the Enter button (button?) where and with witch value you establish this session?

0


a source


One possible problem:

Looking at the code you provided, you have a quoted current.session call, which means you are passing a string

System.Web.HttpContext.Current.Session ["UID"]. ToString ()

in a where clause, which means nothing in sql.

You need to add 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 ... :)

0


a source


If you are trying to exchange session data between classic ASP and ASP.Net, it is not very easy, but doable.

See the following MSDN page for details: http://msdn.microsoft.com/en-us/library/aa479313.aspx

0


a source







All Articles