Solved

Set AccessDataSource Parameter based on User.Identity.Name

Posted on 2010-11-16
12
1,053 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:Jesse Houwing
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Edureka is one of the fastest growing and most effective online learning sites.  We are here to help you succeed.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

912 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