Hi,
I've got a table that holds user details (such as age, location etc). This table is linked to a user logon details table by the UserID column.
How do I get a datagrid to display the logged on users details? Everything I've read so far uses Username but username isn't a column in my UserDetails table.
I've tried SELECT all from the user details table and using UserID as a session parameter but the datagrid doesn't display so I'm doing something very wrong I think.
The sql statement looks like this:
SELECT UserID, FirstName, Surname, Telephone, Section, Location FROM dbo.aspnet_UserDetails WHERE (UserID = @UserID)
Parameter Source is set to "Session"
SessionField is set to "UserID"Using Visual Web Developer 08 Express
Any advice appreciated.
Thanks
![]() |
0 |
![]() |
Get the UserID of the currently logged-in user like so:
MembershipUser user = Membership.GetUser(); if (user != null) { Guid userID = new Guid(user.ProviderUserKey.ToString()); }
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Thanks for responding, I will give this a go.
![]() |
0 |
![]() |
Sorry I really am new to all this. Where would I put that code? Does it go in the code behind page of the page where I'm placing the datagrid?
Sorry, I know this is really basic stuff
![]() |
0 |
![]() |
hope it works for u
http://quickstart.developerfusion.co.uk/QuickStart/aspnet/doc/ctrlref/login/default.aspx
http://aspnet.4guysfromrolla.com/articles/120705-1.aspx
http://msdn.microsoft.com/en-us/library/ms998347.aspx
mahmood
![]() |
0 |
![]() |
Thanks for the links. I've set up my registration page and my login page and it all works excellently. I tend to arrive at problems though when I try to do something thats not always explained in the walkthroughs and examples, something that seems a little too specific to what I'm trying to do. I'm finding the forums really helpful though as they tend to focus on specific issues
![]() |
0 |
![]() |
If you're using a SqlDataSource, use the Selecting event handler of that control like so:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) { MembershipUser user = Membership.GetUser(); if (user != null) { e.Command.Parameters["@UserID"].Value = user.ProviderUserKey; } }
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Ok thanks for that, yes I'm using a SqlDataSource so I've put that code into the code behind page (converted to VB.Net)
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As SqlDataSourceSelectingEventArgs)
Dim user As MembershipUser = Membership.GetUser()
If user IsNot Nothing Then
e.Command.Parameters("@UserID").Value = user.ProviderUserKey
End If
End SubThe datagrid still doesn't display though - do I need to reconfigure my data source as it is still as follows:
SELECT UserID, FirstName, Surname, Telephone, Section, Location FROM dbo.aspnet_UserDetails WHERE (UserID = @UserID)
Parameter Source is set to "Session"
SessionField is set to "UserID"I've tried different parameters etc but the datagrid doesn't display (presumably because it can't get any values so doesn't display anything)
![]() |
0 |
![]() |
Let me see your SqlDataSource code.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:aspnetusersConnectionString3 %>"SelectCommand="SELECT UserID, FirstName, Surname, Telephone, Section, Location FROM dbo.aspnet_UserDetails WHERE (UserID = @UserID)">
<SelectParameters>
<asp:SessionParameter Name="UserID" SessionField="@UserID" />
</SelectParameters>
</asp:SqlDataSource>
![]() |
0 |
![]() |
Are you really storing your Session variable like so?
Session["@UserID"] = ???
Make sure the SessionField matches what key you're really using for the Session object.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
I wasn't sure what that was doing to be honest - should I be even using that? I added it because I was reading about it on a different site and thought it might help with the problem I'm having.
![]() |
0 |
![]() |
I'm not sure, you tell me. Are you passing in the UserId from another page via the Session object? We really need to see your relevant code front and back to see what's going on.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Basically I've set up the registration pages using the standard Login controls. I've then customised it slightly by adding a new CreateUserWizardStep to capture a few other details. These other details are held in a separate table linked by UserID to the standard aspnet_Users table. To do that I used this:
Protected Sub CreateUserWizard1_CreatedUser1(ByVal sender As Object, ByVal e As System.EventArgs) Handles CreateUserWizard1.CreatedUser Dim UserNameTextBox As TextBox = DirectCast(CreateUserWizardStep1.ContentTemplateContainer.FindControl("UserName"), TextBox)
Dim DataSource As SqlDataSource = DirectCast(CreateUserWizardStep1.ContentTemplateContainer.FindControl("InsertExtraInfo"), SqlDataSource)
Dim User As MembershipUser = Membership.GetUser(UserNameTextBox.Text)
Dim UserGUID As Object = User.ProviderUserKeyDataSource.InsertParameters.Add("UserId", UserGUID.ToString())
DataSource.Insert() End SubI then have a page set up in a folder called MemberPages that only logged in users can access - I believe this is just standard stuff - i.e. I've not done anything special and I've just followed the walkthroughs included in Visual Web Developer Express so I've used the Web Site Admin Tool to deny anonymous access to this folder. Its in this folder where the page sits where I want to display the fields dependant on the logged on UserID.
As I've just followed the walkthrough using the wizards and standard controls I'm not sure what is being passed through. I can write out the logged in username and that displays ok: <asp:LoginName ID="LoginName1" runat="server" />
so the login process works fine and I assumed the LoginName would be held as a session variable. But aswell as LoginName I want to incorporate UserID in order to display the users other details.
Thats really all there is to it as far as coding is concerned. I appreciate your help so far by the way, you've been very patient!
![]() |
0 |
![]() |
You should then be able to retrieve the user's ID via the Membership object as I described previously.
ASPX
<asp:sqldatasource id="SqlDataSource1" runat="server" connectionstring="<%$ ConnectionStrings:aspnetusersConnectionString3 %>" selectcommand="SELECT UserID, FirstName, Surname, Telephone, Section, Location FROM dbo.aspnet_UserDetails WHERE (UserID = @UserID)" onselecting="SqlDataSource1_Selecting"> <selectparameters> <asp:parameter name="UserID" /> </selectparameters> </asp:sqldatasource>CODE-BEHIND
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e) { MembershipUser user = Membership.GetUser(); if (user != null) { e.Command.Parameters["@UserID"].Value = user.ProviderUserKey; } }
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Thanks you're an absolute star, thats working great!
![]() |
0 |
![]() |
Taking this a little further then, how would I go about inserting the UserID value into another table to tie up records to UserID?
So I have Table1 that contains the UserID primary key field (which we played around with earlier via SqlDataSource1) and I also have Table2 that contains User Details (which is now displaying great on my DataGrid) and I now also want Table3 to contain UserID as a foreign key.
So on the page that I have my DataGrid and SqlDataSource1 I also have a form that needs to be able to insert the UserID value into Table3 so Table3 can be referenced by UserID. Is this possible? I have an Insert statement in the code-behind page but I can't figure out the syntax to add UserID to my Insert statement
Thanks.
![]() |
0 |
![]() |
You'd pretty much do it exactly the same way except you'd use the SqlDataSource.Inserting event handler instead. Obtaining the currently logged-in user's ID can be done exactly the same way as I showed you before.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |
Great, thanks again for your help!
![]() |
0 |
![]() |
Thanks for the thread on this, and Ed...thanks for the solution. I was having the exact same issue today. I was using the code to get the UserID, but it wasn't being recognized by my datagrid. "onselecting="SqlDataSource1_Selecting" was what I was missing.
THANKS!
![]() |
0 |
![]() |
im pretty new at this, and am kinda stuck
i got the above example working with c# but am trying to do it in vb as that is what the rest of my site is in but i'am getting nowhere
if someone could help with a vb translation it would be much appreciated .
cheers
![]() |
0 |
![]() |
Hi radface, this is the vb I've been using:
Protected Sub SqlDataSource1_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs)
Dim user As MembershipUser = Membership.GetUser()
If user IsNot Nothing Then
e.Command.Parameters("@UserID").Value = user.ProviderUserKey
End If
End SubAlso, you may find this conversion site helpful:
![]() |
0 |
![]() |
Thankyou
that works perfectly.
will keep that code converter in mind aswell.
could come in very handy in the future.cheers
![]() |
0 |
![]() |
I have a little problem with this...my datagrid comes up but it displays all the data not just for the person logged in.
Got any ideas?
source code:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="userdetails.aspx.cs" Inherits="userdetails" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><
html xmlns="http://www.w3.org/1999/xhtml"><
head runat="server"><title></title></
head><
body> <form id="form1" runat="server"> <div><asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Serial_number" DataSourceID="SqlDataSource1"> <Columns><asp:BoundField DataField="Serial_number" HeaderText="Serial_number" ReadOnly="True" SortExpression="Serial_number" /><asp:BoundField DataField="ESP_date" HeaderText="ESP_date" SortExpression="ESP_date" /><asp:BoundField DataField="Model_number" HeaderText="Model_number" SortExpression="Model_number" /> </Columns> </asp:GridView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" onselecting="SqlDataSource1_Selecting" SelectCommand="SELECT [Serial_number], [ESP_date], [Model_number], [UserId] FROM [product_info]"> <SelectParameters><asp:ControlParameter ControlID="GridView1" Name="UserId" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource>
</div></form>
</
body></
html>
sql code behind:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e){
MembershipUser user = Membership.GetUser();if (user != null){
e.Command.Parameters["@UserId"].Value = user.ProviderUserKey;}
}
Thanks
![]() |
0 |
![]() |
Not sure tbh. I use vb.net and not C# but there doesn't seem to be much difference between the vb and c# code except the Handles clause in vb. This isn't supported in c# as far as I'm aware so I'm not sure what the equivalent would be or whether it should work anyway without a Handles clause. Maybe any c# experts out there can advise.
![]() |
0 |
![]() |
nkair19:
I have a little problem with this...my datagrid comes up but it displays all the data not just for the person logged in.
Got any ideas?
source code:
<%
@ Page Language="C#" AutoEventWireup="true" CodeFile="userdetails.aspx.cs" Inherits="userdetails" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"><
head runat="server"><title></title> </head><
body> <form id="form1" runat="server"> <div><asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="Serial_number" DataSourceID="SqlDataSource1"> <Columns><asp:BoundField DataField="Serial_number" HeaderText="Serial_number" ReadOnly="True" SortExpression="Serial_number" /><asp:BoundField DataField="ESP_date" HeaderText="ESP_date" SortExpression="ESP_date" /><asp:BoundField DataField="Model_number" HeaderText="Model_number" SortExpression="Model_number" /> </Columns> </asp:GridView><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" onselecting="SqlDataSource1_Selecting" SelectCommand="SELECT [Serial_number], [ESP_date], [Model_number], [UserId] FROM [product_info]"> <SelectParameters><asp:ControlParameter ControlID="GridView1" Name="UserId" PropertyName="SelectedValue" /> </SelectParameters> </asp:SqlDataSource>
</div></form> </body>
</
html>
sql code behind:
protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e){
MembershipUser user = Membership.GetUser();if (user != null){
e.Command.Parameters["@UserId"].Value = user.ProviderUserKey;}
}
ThanksFirst off, you should be using a Parameter as opposed to a ControlParameter. Secondly and most importantly, you don't have a WHERE clause in your SQL statement so there's no way to filter your results to just that user.
Thanks, Ed
Microsoft MVP - ASP/ASP.NET
![]() |
0 |
![]() |