Link to home
Start Free TrialLog in
Avatar of DLockwood
DLockwood

asked on

Set AccessDataSource Parameter based on User.Identity.Name

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?
Avatar of Rouchie
Rouchie
Flag of United Kingdom of Great Britain and Northern Ireland image

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

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

Avatar of DLockwood
DLockwood

ASKER

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?
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 :-)
Avatar of Jesse Houwing
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
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?
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.
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

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?
ASKER CERTIFIED SOLUTION
Avatar of DLockwood
DLockwood

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>> 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
Answered my own question.