Tracking UserID (New to this also)

Discussion in 'ASP.NET 2.0' started by dmann, Mar 30, 2006.

  1. I'm trying to put together an online, per user inventory system where the user traks personal inventory, similar to some of the Wine Inventory websites that are available.I want to be able to base my queries and other SQL statements based on the current user, but I don't know how to grab the UserID from session when they log in. I've got the basic pages up and running on the live server, and I can create users and roles, but I'm struggling with creating a FormView that will allow the user to search their records based on a full or partial query built on some or all of the entries in the textboxes... Can someone give me a few pointers here? Thanks! Dennis
     
  2. In the MembershipUser class there is the ProviderUserKey property. This should get the UserID uniqueidentifier value.

    Membership.GetUser().ProviderUserKey.ToString()

    Aristotle

    DiscountASP.NET
    www.DiscountASP.NET
     
  3. Thank you, I appreciate the help, and pardon my innocence, where would I run that? in web.config? or on page_load downstream from a successful login?


    Dennis
     
  4. That command returns a string, you run in wherever you are going to query the user name. You can also use User.Identity.Name


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  5. OK, I thought I had a solution, LOL... the Membership.GetUser().ProviderUserKey.ToString() command works great, and I can write the value to a label and confirm it visually, but when I try and pass the value in a SQL query it balks on me.Labels aren't bindable, and ifI through it into a variable and reference the variableI get aSQL error because thevariable name is not a column name....


    I want to grab that UserID uniquevalue and use it to insert/select/edit/delete records in my tables, of in a DataSet... am I going about this the hard way? I was trying to stay cookieless


    Dennis
     
  6. you should be able to just shove that into a query

    Dim user As String = Membership.GetUser().ProviderUserKey.ToString()

    Dim query As String = "INSERT INTO ... (user) VALUES (" & user & ")"

    etc..


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  7. Thank you for your continued patience, I guess we all have to start somewhere, HUH? LOL!

    Actually, I'm trying to use the Formview on .NET Webform, and Ive got the default set to INSERT. When the user hits the page I wanted to grab that UserID value and pass it into the INSERT statement, so that when the user inserts their records it gets tagged with their ID...I grabbed the ID and passed it to a Label on the page so I could see what I was working with and then I tried setting up the INSERT statement in the Configure Data Source dialog steps like this:

    INSERT INTO Projects(ProjectID, ProjectTitle, ContactID, AuthorLastName, AuthorFirstName, SurfaceItem, PublicationID, ColorScheme, Theme, UserID) VALUES (@ProjectID, @ProjectTitle, @ContactID, @AuthorLastName, @AuthorFirstName, @SurfaceItem, @PublicationID, @ColorScheme, @Theme, ' Label1.Text ')

    It did create the records, but instead of passing the value of the text from Label1, it actually passed the String 'Label1.Text' into the record, so that's what it returns on a query... So, How do I get there from here?
    Dennis
     
  8. [​IMG]It seems like .Net Framework 2.0 is kinda different from .Net Framework 1.1.....

    umm.....I haven't seen this kinda error before...


    Any help? I 'd like to know the answer too...[​IMG]

    Jason
    Technical Support/IT Admin/MIS Developer
    Maxtop Enterprises Inc.
    http://www.maxtop.ca</CODE>
     
  9. dmann said...
    Thank you for your continued patience, I guess we all have to start somewhere, HUH? LOL!

    Actually, I'm trying to use the Formview on .NET Webform, and Ive got the default set to INSERT. When the user hits the page I wanted to grab that UserID value and pass it into the INSERT statement, so that when the user inserts their records it gets tagged with their ID...I grabbed the ID and passed it to a Label on the page so I could see what I was working with and then I tried setting up the INSERT statement in the Configure Data Source dialog steps like this:

    INSERT INTO Projects(ProjectID, ProjectTitle, ContactID, AuthorLastName, AuthorFirstName, SurfaceItem, PublicationID, ColorScheme, Theme, UserID) VALUES (@ProjectID, @ProjectTitle, @ContactID, @AuthorLastName, @AuthorFirstName, @SurfaceItem, @PublicationID, @ColorScheme, @Theme, ' Label1.Text ')

    It did create the records, but instead of passing the value of the text from Label1, it actually passed the String 'Label1.Text' into the record, so that's what it returns on a query... So, How do I get there from here?
    Dennis

    As my understanding , your SQL Statement is wrong....
    What you want to do here is to extract value of the label1.

    Label1.Text will literally extract the label text as a string value.

    On the other hand, singal quote (') is used to declare text string value. Since Label1.Text is already a text string ....if you enclose it with a (' ') , you are telling your SQL db ...."Hey, SQL....take Label1.Text as a string value"

    That's why you got this problem

    To solve, just get rid of the singal quotes around Label1.Text should do just fine[​IMG] [/quote]

    Jason
    Technical Support/IT Admin/MIS Developer
    Maxtop Enterprises Inc.
    http://www.maxtop.ca</CODE>
     
  10. Or should i say...try the following modified SQL


    "INSERT INTO Projects(ProjectID, ProjectTitle, ContactID, AuthorLastName, AuthorFirstName, SurfaceItem, PublicationID, ColorScheme, Theme, UserID) VALUES (@ProjectID, @ProjectTitle, @ContactID, @AuthorLastName, @AuthorFirstName, @SurfaceItem, @PublicationID, @ColorScheme, @Theme," &amp; Label1.Text &amp; ");"

    Jason
    Technical Support/IT Admin/MIS Developer
    Maxtop Enterprises Inc.
    http://www.maxtop.ca</CODE>
     
  11. You can bind to a label control, try something along these lines

    <asp:Label ID="UserID" Text="<%# Membership.GetUser().UserID %>" runat="server"/>

    in the <DataSource> block setup the insert command as so:
    INSERT INTO Projects(ProjectID, ProjectTitle, ContactID, AuthorLastName, AuthorFirstName, SurfaceItem, PublicationID, ColorScheme, Theme, UserID) VALUES (@ProjectID, @ProjectTitle, @ContactID, @AuthorLastName, @AuthorFirstName, @SurfaceItem, @PublicationID, @ColorScheme, @Theme, @UserID)

    finally ensure the <InsertParameters> block within the <DataSource> block contains
    <asp:parameter Name="UserID">


    the runtime should take care of all the appropriate bindings. I hope this works for you. A great resource is the msdn library which fully documents the entire .net API and includes a lot of samples:
    http://msdn.microsoft.com/library/default.asp?url=/library/en-us/netstart/html/cpframeworkref_start.asp?frame=true
     
  12. Well, When I tried passing in Label1.Text as a value without any quotes or operators I got this error when I tried to run the page:

    The name "Label1.Text" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
    </o:p>
    And when I try ? &amp; Label1.Text &amp; ? I get this:

    The name "&amp; Label1.Text&amp;" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
    </o:p>
    So I?m still kind of stuck? anybody have any suggestions? I?m not in love with this approach; I just don?t have the experience to approach it any other way? How does a shopping cart app write the customer?s ID into their records? I can?t use SessionID because it will change with every session, but I do have the new out of the box Membership class that comes with VS2005</o:p>
     
  13. No Luck, the error was a A call to Bind was not well formatted please refer to documentation for the correct parameters to Bind. It seems that the only thing the formview wants to work with is the objects inside the Formview itself, and since the Formview is created off of the specific tables that are referenced in the Data Scource, it won't accept any input from any other objects. I could use a select statement to get the UserID value out of the asp_Membership table, I just can't fiqure out how to tell it which field I'm looking for (ie "Get me all the (or insert a new) records from XTable where the UserID is the same as the User that's logged into this session)
    Dennis
     
  14. Got it!


    Protected Sub FormView1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles FormView1.Load


    Dim UserNameTextBox As TextBox = CType(FormView1.FindControl("UserNameTextBox"), TextBox)


    UserNameTextBox.Text = Membership.GetUser.ProviderUserKey.ToString


    End Sub
    placed in the code behind file for a FormView1 onLoad event....
     
  15. Ya, I think you will have to use that method for binding to a textbox.


    I see you are using FindControl, UserNameTextbox doesn't exist in your namespace?


    Joel Thoms
    DiscountASP.NET
    http://www.DiscountASP.NET
     
  16. It's my understanding that to touch a control inside a Formview, Gridview, or Detailsview you have to use FindControl to access any of the associated controls. I'm new to this, so there may be many ways to approach this, but this was the only way I found. Now all I have to do is figure out how to make it persistant, I can get the value in the textbox, but once I fire off the Insert button it's gone. I thought a postback would reload the page...


    Dennis
     
  17. Hi

    Sorry to revive an old thread but this approach would work for me but I just can't seem to get the value into the database!

    Can anyone help with the Update and SQL entries and the text label

    I have the membership ID in the text field using this




    <asp:TextBox ID="UserNameTextBox" runat="server" Width="300px" ></asp:TextBox>





    and my sql reads like this


    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ClubSiteDB %>"


    SelectCommand="SELECT id,Company,Memberid,description,cat1,cat2,photo,location, Small_description, address1, county, tel,website FROM Directory WHERE (id = @id)"


    InsertCommand="INSERT INTO [Directory]([Company],[description],[Small_description],[website],[tel],[location],[address1],[county],[cat1],[cat2],[photo],[Memberid) VALUES(@Company,@description,@Small_description,@website, @tel, @location, @address1, @county ,@cat1, @cat2,@photo,@UserNameTextBox.Text)"


    UpdateCommand="UPDATE Directory SET company = @company, description = @description, Memberid = @UserNameTextBox.Text, cat1 = @cat1, cat2 = @cat2, photo = @photo, location = @location, Small_description = @Small_description, address1 = @address1, county = @county, tel = @tel ,website = @website WHERE (id = @id)"


    DeleteCommand="Delete from Directory where id=@id" OldValuesParameterFormatString="{0}">








    <SelectParameters>


    <asp:QueryStringParameter Name="id" QueryStringField="DirID" />


    </SelectParameters>


    <UpdateParameters>


    <asp:parameter Name="company" />
    <asp:parameter Name="description" />
    <asp:parameter Name="Small_description" />
    <asp:parameter Name="website" />
    <asp:parameter Name="tel" />
    <asp:parameter Name="location" />
    <asp:parameter Name="id" />
    <asp:parameter Name="address1" />
    <asp:parameter Name="county" />
    <asp:parameter Name="cat1" />
    <asp:parameter Name="cat2" />
    <asp:parameter Name="photo" />
    <asp:parameter Name="UserNameTextBox.Text" />





    </UpdateParameters>


    <InsertParameters>


    <asp:parameter Name="company" />
    <asp:parameter Name="description" />
    <asp:parameter Name="Small_description" />
    <asp:parameter Name="website" />
    <asp:parameter Name="tel" />
    <asp:parameter Name="location" />
    <asp:parameter Name="id" />
    <asp:parameter Name="address1" />
    <asp:parameter Name="county" />
    <asp:parameter Name="cat1" />
    <asp:parameter Name="cat2" />
    <asp:parameter Name="photo" />
    <asp:parameter Name="UserNameTextBox.Text" />


    </InsertParameters>


    <DeleteParameters>


    <asp:QueryStringParameter Name="id" QueryStringField="DirID" />
     

Share This Page