?
Solved

.NET Username parameter in an object datasource

Posted on 2009-04-18
15
Medium Priority
?
1,363 Views
Last Modified: 2013-11-26
Hi ! I have this object Datasource which needs to receive a username parameter. I am unsuccessful in trying to pass User.Identity.Name into the VS.NET datasource object. Any solutions ? No errors, it just never retrieves it even though I'm logged. Thanks
<asp:ObjectDataSource ID="odsClimbs" runat="server" DeleteMethod="Delete" 
        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetDataByUserNo" 
        TypeName="ClimbsTableAdapters.ClimbsTableAdapter" UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_ClimbNo" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="UserNo" Type="String" />
            <asp:Parameter Name="PeakNo" Type="Int32" />
            <asp:Parameter Name="PeakDate" Type="DateTime" />
            <asp:Parameter Name="OrderDay" Type="Int32" />
            <asp:Parameter Name="Canister" Type="String" />
            <asp:Parameter Name="Weather" Type="String" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="CommentsAMC" Type="String" />
            <asp:Parameter Name="Original_ClimbNo" Type="Int32" />
        </UpdateParameters>
        <SelectParameters>
            <asp:Parameter DefaultValue="User.Identity.Name" Name="UserNo" Type="String" />
        </SelectParameters>
        <InsertParameters>
            <asp:Parameter Name="UserNo" Type="String" />
            <asp:Parameter Name="PeakNo" Type="Int32" />
            <asp:Parameter Name="PeakDate" Type="DateTime" />
            <asp:Parameter Name="OrderDay" Type="Int32" />
            <asp:Parameter Name="Canister" Type="String" />
            <asp:Parameter Name="Weather" Type="String" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="CommentsAMC" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>

Open in new window

0
Comment
Question by:GPSAddict
  • 6
  • 5
  • 4
15 Comments
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24175258
try this.

<SelectParameters>
            <asp:Parameter DefaultValue="<%=HttpContext.Current.User.Identity.Name%>" Name="UserNo" Type="String" />
        </SelectParameters>
0
 

Author Comment

by:GPSAddict
ID: 24175312
Hi ! I put the below code in my page load event and it returns my username, just like User.Identity.Name did. However, when passed through my datasource, it won't work...Is it because I'm using a dataset ?!
MsgBox(HttpContext.Current.User.Identity.Name)

Open in new window

0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24175489
If GiftsonDJohn's suggestion doesn't work then try...

C#
odsClimbs.SelectParameters["UserNo"].DefaultValue = User.Identity.Name;

VB
odsClimbs.SelectParameters("UserNo").DefaultValue = User.Identity.Name;

... in your code behind in the event that does the select operation, BEFORE the Select() is fired.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24175505
Regarding this...

--> MsgBox(HttpContext.Current.User.Identity.Name)

... it works because of the context of the Page_Load event in which it was called. There is no such context in the HTML formatting of the ObjectDataSource control, so it interprets "Current.User.Identity.Name" to be a simple string. You have to get it to act in some way, either in an Eval() [which I'm not sure will work, btw] or in the proper event in your code behind.
0
 

Author Comment

by:GPSAddict
ID: 24175696
Alright please take a look at my code below....Now I get an error : ObjectDataSource 'odsClimbs' could not find a non-generic method 'GetDataByUserNo' that has no parameters. Any ideas ? Thanks
    Protected Sub odsClimbs_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles odsClimbs.Selecting
        If User.Identity.Name <> "" Then
            odsClimbs.SelectParameters("UserNo").DefaultValue = User.Identity.Name
        End If
    End Sub
======================================================================
<asp:ObjectDataSource ID="odsClimbs" runat="server" DeleteMethod="Delete" 
        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetDataByUserNo" 
        TypeName="ClimbsTableAdapters.ClimbsTableAdapter" UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_ClimbNo" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="UserNo" Type="String" />
            <asp:Parameter Name="PeakNo" Type="Int32" />
            <asp:Parameter Name="PeakDate" Type="DateTime" />
            <asp:Parameter Name="OrderDay" Type="Int32" />
            <asp:Parameter Name="Canister" Type="String" />
            <asp:Parameter Name="Weather" Type="String" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="CommentsAMC" Type="String" />
            <asp:Parameter Name="Original_ClimbNo" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="UserNo" Type="String" />
            <asp:Parameter Name="PeakNo" Type="Int32" />
            <asp:Parameter Name="PeakDate" Type="DateTime" />
            <asp:Parameter Name="OrderDay" Type="Int32" />
            <asp:Parameter Name="Canister" Type="String" />
            <asp:Parameter Name="Weather" Type="String" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="CommentsAMC" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>

Open in new window

0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24176151
You have to declare the Select Parameter. You have missed it.
    Protected Sub odsClimbs_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) Handles odsClimbs.Selecting
        If User.Identity.Name <> "" Then
            odsClimbs.SelectParameters("UserNo").DefaultValue = User.Identity.Name
        End If
    End Sub
======================================================================
<asp:ObjectDataSource ID="odsClimbs" runat="server" DeleteMethod="Delete" 
        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" 
        SelectMethod="GetDataByUserNo" 
        TypeName="ClimbsTableAdapters.ClimbsTableAdapter" UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_ClimbNo" Type="Int32" />
        </DeleteParameters>
        <SelectParameters>
            <asp:Parameter Name="UserNo" Type="String" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="UserNo" Type="String" />
            <asp:Parameter Name="PeakNo" Type="Int32" />
            <asp:Parameter Name="PeakDate" Type="DateTime" />
            <asp:Parameter Name="OrderDay" Type="Int32" />
            <asp:Parameter Name="Canister" Type="String" />
            <asp:Parameter Name="Weather" Type="String" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="CommentsAMC" Type="String" />
            <asp:Parameter Name="Original_ClimbNo" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="UserNo" Type="String" />
            <asp:Parameter Name="PeakNo" Type="Int32" />
            <asp:Parameter Name="PeakDate" Type="DateTime" />
            <asp:Parameter Name="OrderDay" Type="Int32" />
            <asp:Parameter Name="Canister" Type="String" />
            <asp:Parameter Name="Weather" Type="String" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="CommentsAMC" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>

Open in new window

0
 

Author Comment

by:GPSAddict
ID: 24176202
No error, but still won't return anything....When I test my query with my username it returns the wanted results...Seems something's not working yet. Anymore ideas ? Thanks
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24176227
Try this.

We shall change the way of passing the value into the object data source.

in Global.asax, in Session_Start function put

Session("UserID")=HttpContext.Current.User.Identity.Name;


then define ObjectDataSource as

   
    <asp:ObjectDataSource ID="odsClimbs" runat="server" DeleteMethod="Delete"
        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}"
        SelectMethod="GetDataByUserNo"
        TypeName="ClimbsTableAdapters.ClimbsTableAdapter" UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_ClimbNo" Type="Int32" />
        </DeleteParameters>
        <SelectParameters>
            <asp:SessionParameter Name="UserNo" SessionField="UserID" Type="String" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="UserNo" Type="String" />
            <asp:Parameter Name="PeakNo" Type="Int32" />
            <asp:Parameter Name="PeakDate" Type="DateTime" />
            <asp:Parameter Name="OrderDay" Type="Int32" />
            <asp:Parameter Name="Canister" Type="String" />
            <asp:Parameter Name="Weather" Type="String" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="CommentsAMC" Type="String" />
            <asp:Parameter Name="Original_ClimbNo" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="UserNo" Type="String" />
            <asp:Parameter Name="PeakNo" Type="Int32" />
            <asp:Parameter Name="PeakDate" Type="DateTime" />
            <asp:Parameter Name="OrderDay" Type="Int32" />
            <asp:Parameter Name="Canister" Type="String" />
            <asp:Parameter Name="Weather" Type="String" />
            <asp:Parameter Name="Comments" Type="String" />
            <asp:Parameter Name="CommentsAMC" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24176231
One important thing. Is you website security setting configured not to use Anonymous access?
0
 

Author Comment

by:GPSAddict
ID: 24176431
Nope...It still uses Anonymous access, I'll input security on later. My login control sits in a master page. Don't know if that is the trouble (login view with a login control) ?
0
 
LVL 14

Expert Comment

by:GiftsonDJohn
ID: 24176652
Are you using Forms Authentication? or WindowsAuthentication?

Your website should be configured not to use Anonymous access. If anonymous access is enabled then the value User.Identity.Name will be always empty
0
 
LVL 11

Expert Comment

by:Kelvin McDaniel
ID: 24177182
You going have to debug the method to see what is being passed. You'll probably have to do this on your local machine (if you don't control the server this is supposed to run on).

Also, the method that the ObjectDataSource calls needs to take UserNo as a parameter in its signature. It looks like you built this using the Designer to create a Table Adapter, so you should update the GetDataByUserNo() method in the Table Adapter to accept a string parameter UserNo.

Try that and let us know what happened.

0
 

Author Comment

by:GPSAddict
ID: 24178651
azarc3: I already tried all of that, it just won't work...
GiftsonDJohn: I use the ASP.NET Web Site Administration Tool, created roles and users but I want anonymous users to be able to see some content.
0
 

Author Comment

by:GPSAddict
ID: 24178675
It works using the below code but god help me (linking the parameter to the textbox), I don't want to use that...
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Me.TextBox1.Text = Page.User.Identity.Name.ToString
    End Sub

Open in new window

0
 
LVL 11

Accepted Solution

by:
Kelvin McDaniel earned 2000 total points
ID: 24194217
WARNING: This is a long post, but I guarantee that it's well worth it.  :)  I had to reason somethings out before I finally figured out what you're describing is the problem. I'm actually facing a similar issue with one of the users I support right now, so this was a timely exercise.

Basis issues:
  1. You want unknown users to be able see database-provided content (Based on comment ID #24178651)
  2. At some point you want LOGGED IN users to be known to the database
  3. You're not able to get the ID of a LOGGED IN user into your query using the markup for the ObjectDataSource

I assume that
  A. You're not having problems with the anonymous users being able to see content
  B. The problem your experiencing is when the anonymous user has successfully logged in and is now known to the website
  C. The website is successfully showing you who the LOGGED IN user is
  D. The ObjectDataSource does not appear to be accepting the known User ID

Based on comment ID #24178675 I see that you're able to successfully retrieve the LOGGED IN user, but you're not successful in providing the User Name to the ObjectDataSource.

*** If all of this is correct then your problem is that you're not using the correct EVENT to push the User ID to the ObjectDataSource. The Select Parameter is being set AFTER odsClimbs has already executed the query.  ***

One issue with using the ObjectDataSource control is that updates to it aren't available to its internal processing UNTIL you explicitly call its methods in Codebehind OR hit the correct event AFTER setting its properties. Doing that can be very confusing because you have to know the order that it's events are fired in WITHIN the events of the Page lifecycle.

Below is a simple strategy to try. I've just tested it against a remote database and it works perfectly given what you've explained as what you're trying to do. The only problem with this strategy is that the ClimbsTableAdapters.ClimbsTableAdapter.GetDataByUserNo() method will fire whenever someone hits the page, logged in or not. If this is an unacceptable performance hit then you'll want to wrap the code in GetDataByUserNo() in an If statement to test String.IsNullOrEmpty(UserNo) and cancel execution if it is true.

<%-- This part goes in the HTML markup file (.aspx); switch out (or put back in) this for what you currently have for the Select Parameters --%>
 
        <SelectParameters>
            <asp:Parameter Name="UserNo" DbType="String" Direction="Input" ConvertEmptyStringToNull="true" Type="String" />
        </SelectParameters>
 
 
'*** This part goes in the Codebehind file (.aspx.vb);
    Protected Sub odsClimbs_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles odsClimbs.Init
        If Not User Is Nothing Then
'NOTE: Make absolutely certain that what you're passing in to the UserNo Parameter 
'        is what the database expects... 
'        -Length of string (if not a VarChar field)
'        -Format (domain\user)
'        -Etc.
            odsClimbs.SelectParameters("UserNo").DefaultValue = User.Identity.Name
        End If
    End Sub

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question