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
 
If the problem your getting is that your trying to get data based on a username that isn't unique or distinct, then you need to get away of making that user distinct.

If you've gone into this allowing multiple users to have the same name, then that really is just asking for trouble from the outset surely?
 
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?
 
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
 
Have you run any queries again a database before so we know where to start explaining?

The username of the person logging in, are you storing the username in a session variable?
 
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
 
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.
 
Wardie said:
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.

Righto, never done it before, but just tried it and I have the perfect solution.

Before I go on, have you set any roles in your ASP.NET Membership database to seperate managers from general users?

If not, go add them in, and assign your users to the roles.

Create a new page and drag in a LoginView.
The LoginView allows you to control what is seen when users are logged in or not.
There are two templates 'Anonymous' and 'LoggedIn'.
You can also control what each of your Membership Roles will see.
Click the Edit RolesGroup link, and add in the name of the roles you created.
It should add some new selections in the template dropdown for each role.
To build your page select the role template and drag the gridview into the LoginView and anything else needed on the page for that role.

To make the GridView only show your users details use the profile object, which only requires two lines of code to work.
Go into your web.config file, and add the following in between the system.web tags:
Code:
      <profile>
        <properties>
          <add name="myUserName" />
        </properties>
      </profile>
In the vb code for your aspx page add the following to the Page_Load event:
Code:
        Dim user As MembershipUser = Membership.GetUser()
        Profile.myUserName = user.UserName.ToString()
That's it.
When you create your GridView and it asks for the username parameter, select Profile and type in 'myUserName'.

HTH
 
Last edited:
Thanks KevinCB, good thinking :)

Do you mind me sending you the code on MSN sometime? Still haven't fixed that update problem :|
 
Last edited:
I can tell you what that is now, I get it all the time.

Basically you need to go into IIS, select the folder your website in.
Right click and select Properties.
On the properties window, you should see a button halfway down that says 'Create', click it.

Now it should work.

Edit: That was a sneaky edit :p

Yeah sure, I added you onto my MSN last night, might go on tonight if you wanna send it then.
 
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 :)
 
Back
Top Bottom