Solved

Set AccessDataSource Parameter based on User.Identity.Name

Posted on 2010-11-16
12
1,052 Views
Last Modified: 2012-05-10
I have a Drop Down List that loads values based on who the User is that has accessed the page.

I have created a Drop Down List that uses the following AccessDataSource.....
    <asp:AccessDataSource ID="PermittedAccessList_DS" runat="server"
        DataFile="~/App_Data/OutcomesDB.mdb"
        SelectCommand="SELECT tlkp_TableSecurity.TableSecurity FROM (tlkp_TableSecurity INNER JOIN tlkp_UserSecurity ON tlkp_TableSecurity.ID = tlkp_UserSecurity.TABLE) WHERE (Username = ?) ORDER BY tlkp_TableSecurity.TableSecurity">
        <SelectParameters>
            <asp:Parameter Name="UserName" Type="string" DefaultValue="" />
        </SelectParameters>
    </asp:AccessDataSource>

I have put a Response.Write on the Page to display the Username based on User.Identity.Name. It is working correctly.

IN the Code behind, I have the following......

Private Sub DDLTables_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDLTables.DataBound
     Dim sUserID As String = User.Identity.Name
     PermittedAccessList_DS.SelectParameters(0).DefaultValue = sUserID
End Sub

This does NOT work. The Drop Down List comes back empty everytime. What am I missing?
0
Comment
Question by:DLockwood
  • 6
  • 5
12 Comments
 
LVL 25

Expert Comment

by:Rouchie
ID: 34153561
If its hard coded into your ASPX page then try this:
		PermittedAccessList_DS.SelectParameters("UserName").DefaultValue = sUserID

Open in new window


Otherwise remove it from the page and do it in code behind:

		Dim p As New Parameter
		p.Type = Data.DbType.String
		p.DefaultValue = sUserID
		PermittedAccessList_DS.SelectParameters.Add(p)

Open in new window

0
 
LVL 25

Expert Comment

by:Rouchie
ID: 34153580
Sorry I think I might have misread your question.  Can you please clarify exactly what you are needing to do with the dropdownlist?  IF you need to access user.identity.name you could always retrieve it earlier and save it into a page variable, which will then be available within the subroutine:

	Protected Property _UserID As String
		Get
			Return ViewState("userid")
		End Get
		Set(ByVal value As String)
			ViewState("userid") = value
		End Set
	End Property

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		_UserID = User.Identity.Name
	End Sub

	Private Sub DDLTables_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles DDLTables.DataBound
		Dim sUserID As String = _UserID
		PermittedAccessList_DS.SelectParameters(0).DefaultValue = sUserID
	End Sub

Open in new window

0
 

Author Comment

by:DLockwood
ID: 34154306
Rouchie,

I will try some combination of your suggestions and see if I can get it to work.

It seems like I am playing around with the correct code but it doesn't work. I know I am accessing the User.Identity.Name value as I can display it on the page as a test. This value just never makes it into the Select Parameter and then into the query. I am clearly missing something somewhere that is keeping it from getting into the Parameter.

I am kind of stuck on where it should go in the code behind. Should I put it in the Page_Load Event? If I put it in the event "DDLTables_DataBound" do I need to explicitly list that event in the code for the Drop Down List? Something like OnDataBound = "DDLTables_DataBound".

Ideas?
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 34154552
Can you tell me exactly what the outcome of your code (pseudo-code if necessary!) needs to be?  Then, I'll write it for you :-)
0
 
LVL 17

Expert Comment

by:ToAoM
ID: 34154775
Instead of using DataBound, you need to set your parameters in the Selecting event.

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.selecting(v=VS.80).aspx
0
 

Author Comment

by:DLockwood
ID: 34155200
Rouchie,

The outcome is simple.

One drop down list that contains items that match the User that is visiting the site.

So, Drop Down Select Statement has a where clause "Username = ?" and this is a parameter.
I simply need to get the user and then apply the where statement so the Drop Down Loads with only that specific users values.

Should be SIMPLE but it doesn;t work.

Maybe I need to put code in Selecting Event like ToAoM says. I will try that.

ToAoM do I need to add the event to my Drop Down?
0
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
LVL 25

Expert Comment

by:Rouchie
ID: 34155689
Well whatever you do it needs to be done outside of DDLTables.DataBound.  Do not use this event because this event is fired AFTER the drop down list gets populated.  However, you can access the values of the dropdownlist at any time after databinding, so best to put it all in one external subroutine that you might call from Page_Load, or put the whole lot in Page_Load directly.
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 34155819
I don't use the AccessDataSource, but after a chunk of online reading, you might want to try something like this:

	Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
		If Not Page.IsPostBack Then
			Dim p As New Parameter
			p.Type = Data.DbType.String
			p.DefaultValue = User.Identity.Name
			PermittedAccessList_DS.SelectParameters.Add(p)
			DDLTables.DataSource = PermittedAccessList_DS.Select(DataSourceSelectArguments.Empty)
			DDLTables.DataBind()
		End If
	End Sub

Open in new window

0
 

Author Comment

by:DLockwood
ID: 34159725
NOTHING works and I am getting a little frustrated to say the least.

I cannot even hard code the string value in the code bhind. It just doesn't work.

Any ideas anyone?
0
 

Accepted Solution

by:
DLockwood earned 0 total points
ID: 34160168
WOW - I figured out the problem.

How stupid is this?

Cannot use a complex query with a join. When I change the Select Command Query to be a simple statement "Select [Table], [Username] from [tlkp_UserSecurity] WHERE (Username = ?) ORDER BY[Table]" then everything works fine.

No error message. No nothing. Just didn't work. What a waste of time.
0
 
LVL 25

Expert Comment

by:Rouchie
ID: 34162010
>> Cannot use a complex query with a join.

I've not seen that reported before.  I notice in your SQL command you have used a reserved word (TABLE) as a column name, which will cause problems.

http://support.microsoft.com/default.aspx?scid=kb;en-us;286335

Also you might try a LEFT JOIN instead of INNER so that the query still returns the user when no securities exist.

SELECT tlkp_TableSecurity.TableSecurity FROM (tlkp_TableSecurity LEFT JOIN tlkp_UserSecurity ON tlkp_TableSecurity.ID = tlkp_UserSecurity.TABLE) WHERE (Username = ?) ORDER BY tlkp_TableSecurity.TableSecurity
0
 

Author Closing Comment

by:DLockwood
ID: 34186465
Answered my own question.
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

708 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now