Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 29th, 2012, 01:36 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile filter data based on logged in user's Id using EntityDataSource

Greetings to all,
I have a table named DailyCollection. On a page I have GridView, DetailsView and EntityDataSource. Both data bound controls are bound to EntityDataSource.

While inserting record I am saving logged in user's Id in table DailyCollection, but while retrieving records in GridView I have no Idea about how to filter data based on logged in user's id which is stored with every record in DailyCollection table.
So how I write where clause for EntityDataSource so that it filters data based on logged in user's Id?

Please suggest

Thanks
 
Old July 30th, 2012, 10:55 AM
Authorized User
 
Join Date: Jul 2012
Posts: 21
Thanks: 1
Thanked 2 Times in 2 Posts
Default

sophia,

A query is always performed on an Entity object or model, not really on the EntityDataSource. The easiest way to do what you're attempting is add a Where clause to the EntityDataSource similar to below and store the parameter specified in a Session object.

HTML Code:
<WhereParameters>
   <asp:SessionParameter Name="UserID" SessionField="UserID" />
</WhereParameters>
From there, in the Page_Load event of the code-behind, you might write something like this:

Code:
Session["UserID"] = [user_id_from_data_table]
Happy coding!
 
Old July 30th, 2012, 12:03 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi, I am getting error "For the EntityDataSource, either Type or DbType (but not both) must be specified for each Parameter."

If I type DbType="Guid" then I get another error "'DailyCollection' is not a member of type 'ASPNETDBModel.DailyCollection' in the currently loaded schemas. Near simple identifier, line 6, column 4."

Please tell me what is the issue?
I am using this code.

Code:
<asp:EntityDataSource ID="EntityDataSource1" runat="server" 
        ConnectionString="name=ASPNETDBEntities" 
        DefaultContainerName="ASPNETDBEntities" EnableDelete="True" 
        EnableFlattening="False" EnableInsert="True" EnableUpdate="True" 
        EntitySetName="Clients" oninserting="EntityDataSource1_Inserting" 
        EntityTypeFilter="" Select="" Where="it.DailyCollection.UserId=@userId">
        <WhereParameters>
                <asp:SessionParameter Name="userId" SessionField="UserID" DbType="Guid" />
        </WhereParameters>
    </asp:EntityDataSource>
And code behind's Page_Load is

Code:
protected void Page_Load(object sender, EventArgs e)
    {
        Session["UserID"] = Membership.GetUser().ProviderUserKey;
     }
 
Old July 30th, 2012, 01:02 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Ok I got it working,
I was making a mistake.
A little difference in code is required
Code:
Where="it.aspnet_Users.UserId=@userId">
 
Old July 30th, 2012, 01:20 PM
Authorized User
 
Join Date: Jul 2012
Posts: 21
Thanks: 1
Thanked 2 Times in 2 Posts
Default

Quote:
Originally Posted by sophia View Post
Ok I got it working,
I was making a mistake.
A little difference in code is required
Glad you got it working.





Similar Threads
Thread Thread Starter Forum Replies Last Post
store logged in user id in database from class file sophia ASP.NET 4 General Discussion 10 March 17th, 2012 06:56 PM
Displaying data based on user logged in Spider Visual Web Developer 2008 23 May 19th, 2010 01:44 PM
Web Service Get List ID with VBSCRIPT to filter on for UpdateListItems ismailc SharePoint Development 2 January 27th, 2010 08:32 AM
Setting logged in UserName to Session ID Admiral1701 ASP.NET 2.0 Professional 10 December 1st, 2006 10:35 AM
Filter Based Upon Logged In User LandOfToz BOOK: Professional SQL Server Reporting Services ISBN: 0-7645-6878-7 0 August 31st, 2004 12:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.