p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   ASP.NET 4 General Discussion (http://p2p.wrox.com/forumdisplay.php?f=561)
-   -   filter data based on logged in user's Id using EntityDataSource (http://p2p.wrox.com/showthread.php?t=88117)

sophia July 29th, 2012 01:36 PM

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

guenfire July 30th, 2012 10:55 AM

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!

sophia July 30th, 2012 12:03 PM

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


sophia July 30th, 2012 01:02 PM

Ok I got it working,
I was making a mistake.
A little difference in code is required
Code:

Where="it.aspnet_Users.UserId=@userId">

guenfire July 30th, 2012 01:20 PM

Quote:

Originally Posted by sophia (Post 285829)
Ok I got it working,
I was making a mistake.
A little difference in code is required

Glad you got it working.


All times are GMT -4. The time now is 02:35 AM.

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