• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1391
  • Last Modified:

.NET Username parameter in an object datasource

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
GPSAddict
Asked:
GPSAddict
  • 6
  • 5
  • 4
1 Solution
 
GiftsonDJohnCommented:
try this.

<SelectParameters>
            <asp:Parameter DefaultValue="<%=HttpContext.Current.User.Identity.Name%>" Name="UserNo" Type="String" />
        </SelectParameters>
0
 
GPSAddictAuthor Commented:
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
 
Kelvin McDanielSr. Developer/ConsultantCommented:
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
Cloud Class® Course: Microsoft Office 2010

This course will introduce you to the interfaces and features of Microsoft Office 2010 Word, Excel, PowerPoint, Outlook, and Access. You will learn about the features that are shared between all products in the Office suite, as well as the new features that are product specific.

 
Kelvin McDanielSr. Developer/ConsultantCommented:
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
 
GPSAddictAuthor Commented:
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
 
GiftsonDJohnCommented:
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
 
GPSAddictAuthor Commented:
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
 
GiftsonDJohnCommented:
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
 
GiftsonDJohnCommented:
One important thing. Is you website security setting configured not to use Anonymous access?
0
 
GPSAddictAuthor Commented:
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
 
GiftsonDJohnCommented:
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
 
Kelvin McDanielSr. Developer/ConsultantCommented:
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
 
GPSAddictAuthor Commented:
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
 
GPSAddictAuthor Commented:
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
 
Kelvin McDanielSr. Developer/ConsultantCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: CompTIA Healthcare IT Tech

This course will help prep you to earn the CompTIA Healthcare IT Technician certification showing that you have the knowledge and skills needed to succeed in installing, managing, and troubleshooting IT systems in medical and clinical settings.

  • 6
  • 5
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now