Solved

Set AccessDataSource Parameter based on User.Identity.Name

Posted on 2010-11-16
12
1,055 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying 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

I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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