Get data based on UserName (ASP)

Capodecina
Permabanned
Joined
31 Dec 2003
Posts
5,172
Location
Barrow-In-Furness
All the information i've found for doing this relies on the UserName being the PK, this isn't the case for the table i'm using. I am however using the default membership provider tables and controls.

I basically just need a way of showing ONLY the data for the user that is logged in using a GridView control.

The SQL query is in the Dataset as:

SELECT ID, WorkCode, WorkDate, Hours
FROM Hours

WHERE UserName = (@username)

Really can't find how to do this :(

I know i've asked a lot of questions on here lately and thanks for helping guys, really struggle to find decent information for this though.

Thanks for any help.
 
Last edited:
tblCodes
CodeID (PK)
CodeName
CodeDescription
CodeType
CodeOwner

tblHours
ID (PK)
WorkCode
WorkDate
Hours
UserName

aspnet_Users
ApplicationID
UserId (PK)
UserName
LoweredUserName
MobileAlias
IsAnonymous
LastActivityDate
 
I don't know how to do what i've said in the first post..... there's absolutely no tutorials on ASP.net for this and it's really starting to wind me up.
 
Whats wrong with the SQL you've put in the first post, looks fine to me?

Or are you just having issues writing the ASP to connect to the SQL Server?

There's nothing wrong with the SQL, i'm not sure how i'm supposed to only get data into a GridView that belongs to that user. As in getting the UserName into the @username Parameter for the SQL.
 
I'm sorry I don't really understand what you want?

I've shown where the UserName is, the SQL query is fine and i'm stuck on how to actually get the parameter into the query from ASP (@username). I want to display this data in a GridView control.

In other words...

Jack logs in, clicks to view hours he has previous logged into the system and he gets a list of the hours he has entered based on his username. He does not get information for anyone else.



:D
 
Not using sessions no, at the moment i'm trying to do it using the following code:

Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim UserName As String = User.Identity.Name
        ObjectDataSource1.SelectParameters("UserName").DefaultValue = UserName

    End Sub

I'm getting the following error when I open the page, which looks like a database issue but I can't see the problem:

Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Line 2129: }
Line 2130: ManHours.HoursDataTable dataTable = new ManHours.HoursDataTable();
Line 2131: this.Adapter.Fill(dataTable);
Line 2132: return dataTable;
Line 2133: }
 
Last edited:
You don't need to do it with code.

Use your aspnet_Users table and databind the Username field to a dropdownlist.
Set the dropdownlist to auto postback.
Use the query with the @Username parameter, with the GridView.
It should ask you where the value for the parameter is stored.
Select ControlParameter and select your dropdownlist.

Take a look here, it will run you through it:

http://www.asp.net/learn/data-access/tutorial-07-vb.aspx

Thanks for that, that's exactly how i'll be doing the page that managers can use to view hours logged by general users.

BUT... for general users it makes no sense to have a drop down menu, they just need to see their own data when they've logged in. Allowing them to Edit/Delete it etc.

Do you follow me?

If it's easier i'll send you the code on MSN or something later and talk then.
 
Thanks KevinCB, good thinking :)

Do you mind me sending you the code on MSN sometime? Still haven't fixed that update problem :|
 
Last edited:
Still not got this working, here's my code at the moment.



Code:
 Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Dim user As MembershipUser = Membership.GetUser()
        Profile.myUserName() = user.UserName.ToString()

    End Sub

I've also added the following code to the web.config file in the system.web section:

Code:
<profile>
      <properties>
        <add name="myUserName" />
      </properties>
    </profile>

To get the @username parameter into the GridView I selected Profile and entered myUserName, code from source below:

<asp:ProfileParameter Name="UserName" PropertyName="myUserName" Type="String" /> When logging in and navigating to the page, it attempts to load then stops, highlighting the following line:



Code:
public class ProfileCommon : System.Web.Profile.ProfileBase {
    
    public virtual string myUserName {
        get {
            return ((string)(this.GetPropertyValue("myUserName")));
        }
        set {
            this.SetPropertyValue("myUserName", value);
        }
    }

It gives me the error, "HttpExeption was unhandled by user code" and below says "Unable to connect to SQL database"



Cheers for any help :)
 
Last edited:
Strange, the reason I ask this is because a few posts up you posted code form your Page_Load event which is in VB, then you posted code from the class ProfileCommon which is in C#.

The code from the class is autogenerated, so somewhere it must think that you are using C#.


I've without a doubt created the site and selected Visual Basic.

Really getting annoyed now and i've got no idea what to do.
 
Cheers i'll check it out at home.

Someone from the ASP.net forum has suggested doing it this way, if you don't mind taking a look i'm not too sure on one part of what he wants me to do, but it does look otherwise fairly simple.

Simple, within the ObjectDataSource.Selecting event, set your UserName parameter like so:

Code:
protected void ObjectDataSource1_Selecting(object sender, ObjectDataSourceSelectingEventArgs e)
{
	e.InputParameters["UserName"] = this.User.Identity.Name;
}

Not seeing your code, it's a little tough to see exactly what you're doing. However, if your using an ObjectDataSource, I'm assuming you have a Parameter set up declaratively within your ObjectDataSource like so:

Code:
<asp:objectdatasource id="ObjectDataSource1" runat="server" onselecting="ObjectDataSource1_Selecting">
	<selectparameters>
		<asp:parameter name="Username" type="String" />
	</selectparameters>
</asp:objectdatasource>

Then, with the Selecting event declared in the ObjectDataSource as above, the Selecting event will fire right before the code within your select method is fired. In this event, we extract the currently logged-in user's username from the Page's IPrincipal object. Your Parameter is then set to this value. With this, your select method will now be passed the appropriate username which can be used to inject into your SQL query or added as a parameter.

I'm not sure where to put the first bit of code really or what he means.
 
Working!

You are indeed a legend, leg-end and allround hero. You've helped me loads with this I can't thank you enough.
 
Back
Top Bottom