Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1060
  • Last Modified:

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?
0
DLockwood
Asked:
DLockwood
  • 6
  • 5
1 Solution
 
RouchieCommented:
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
 
RouchieCommented:
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
 
DLockwoodAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
RouchieCommented:
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
 
Jesse HouwingScrum Trainer | Microsoft MVP | ALM Ranger | ConsultantCommented:
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
 
DLockwoodAuthor Commented:
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
 
RouchieCommented:
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
 
RouchieCommented:
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
 
DLockwoodAuthor Commented:
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
 
DLockwoodAuthor Commented:
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
 
RouchieCommented:
>> 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
 
DLockwoodAuthor Commented:
Answered my own question.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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