Friday, March 16, 2012

Membership and using session

Dears,

I'm new to ASP.Net 2.0 and I have two databases ASPNETDB.MDF, which came from membership, and db.mdf which contain the following fields: UserName, Name, Address.

I tried to use the membership and the session to move the username e.g "John" to another page and diplay his data in gridview by using the username, but all what I got is a blank page !!

your cooperation is appreciated.

Once the user is logged in, you can retrieve their username this way:
Context.User.Identity.Name

Then you can use that against your other DB to get the info, assuming the user's information is in there. You may also want to consider adding your additional tables to the ASPNET.mdf so you only need to manage a single database and connection string.

Hope this helps.


avanslaars,

The problem is when I try to retrive the data from aspnet.mdf itself, I got a blank page !! although the connection string is correct

Please check my code if there is any mistakes

<

asp:SqlDataSourceID="SqlDataSource2"runat="server"ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString2 %>"

SelectCommand="SELECT [UserName],Email FROM [vw_aspnet_MembershipUsers] WHERE ([UserName] = @.UserName)">

<SelectParameters>

<asp:SessionParameterName="UserName"SessionField="UserName"Type="String"/>

BTY your blog is so cool, thank you.


Have you added the UserName to a session variable when logging the user in? By default, the ASP.NET Membership services uses a cookie to store the user information. The query itself looks fine so my guess is either the session variable "UserName" doesn't exist or it exists but has a value in it that does not match anything in the membership store. You could add your parameterin the code behind using the syntax from my last reply as the value and that would ensure that you had the currently logged in user's username for your query.

I'm glad you like my blog, as soon as I finish my current project, I hop to free up some more time and start writing again since the content is starting to get a bit older than I would like. If you subscribe through squeet (form on the blog) you will recieve new posts by email when I start blogging again.

Hope this helps.


I did, I tested it by this code:

Response.Write(Context.User.Identity.Name);

Would you please help me and show me this code, it will drive me crazy.

I'm not sure if the code is correct, and I can't pass the session variable too :(

Thank you


Would you please reply !!!

really I don't ow to do it !!


Sorry I have been pretty heavily involved in a project and can't get to the forums everyday. Anyway, The code that I sent "Response.Write(Context.User.Identity.Name);" and you tested does not get the UserName from the session. If you have not explicity added the UserName value to your own session variable, it isn't there. Try Adding the username to the session using this code before you reach the page with your query but after the user has logged in:

Session.Add("UserName",Context.User.Identity.Name);

You can then access the session variable in your adapter or test it like you did with the other value like but use Session["UserName"] this time.

0 comments:

Post a Comment