Vb.net - Using Eval With Sum() And Count() With EntityFramework And ListView?

Dec 30, 2010

I am working on my first project with the Entity Framework and am having some difficulty displaying advanced information with the EntityDataSource and a ListView.

For example, given the two entities:

Item
Name
Price
Order
Number
Items <---- Navigation Property to Items contained in the Order

I want to display a list of all orders with a column with the total number of items in the order and a column with the sum of the prices of all items in the order.

I am using an EntityDataSource configured as follows:

<asp:EntityDataSource ID="eds" runat="server" ConnectionString="name=NDSEntities"
DefaultContainerName="NDSEntities" EnableFlattening="False" Include="Items"
EntitySetName="Orders"></asp:EntityDataSource>

In the ItemTemplate of the ListView, I can write the order number as follows:

<%# Eval("Number") %>

I had trouble figuring out how to display a count of items in the order. I tried using Items.Count() function in select statement in the EntityDataSource but that didn't work. Eventually I figured out I could do the following in my ItemTemplate:

<%# Eval("Items").Count() %>

Now I am stuck trying to get the sum of the items. I am stuck here. I have tried using

<%# Eval("Items").Sum(Function(i) i.Price)%>

but I get the following error:

Public member 'Sum' on type 'EntityCollection(Of Item)' not found.

This confuses me because I know that Sum is a method of EntityCollection(Of ).

My two questions are as follows:

Am I getting the item count correctly?

How should I go about getting the sum of the prices of the items?

View 1 Replies


Similar Messages:

C# - If Statement Inside A ListView With Eval() Or DataBinder.Eval()?

Nov 11, 2010

I have a listview control on an .aspx page. Inside this list view i want to check "Type" property which comes from database. here is the example code :

[code]...

As a last resort i tried to user DataBinder.Eval() but i get the exception "Expected class, delegate, enum, interface, or struct". What can i be doing wrong? Writing a function in code-behind isn't an option for me.

View 3 Replies

Forms Data Controls :: Using Eval To Get Data Bound Values From Outer Listview In Nested Listview Sample Case?

Jul 6, 2010

i need to get data-bound items from outer listview to display in inner Listview, in this scenario:

[code]....

Where the higlighted text mean the title for outer datasource.

View 3 Replies

Forms Data Controls :: How To Concate 2 Eval In Listview

Jun 9, 2010

i think its a simple problem but i am strugling with it i have list view, and in item template i am bounding a label with 2 fields that is first name and last name

i am tring this code

Text='<%# Eval("firstname") + Eval("lastname") %>'
/>

but it gives error taht Operator '+' cannot be applied to operands of type 'object' and 'object' for single value it works that is

Text='<%# Eval("firstname") %>'
/>
and even for constant concatenate it works that is
Text='<%# "India" + Eval("firstname") %>'
/> .... is working fine

I tried different arrangement but its not working that is

Text='<%# Eval("firstname","Lastname") %>'
/>
Text='<%# Eval("firstname" + "Lastname") %>'
/>

View 5 Replies

Forms Data Controls :: ListView Renders All Eval Statements Except One?

Feb 16, 2010

I have a ListView bound to a database view (vw_Reunions). The Eval statements in the ItemTemplate render the data in vw_Reunions just fine but I have one in the LayoutTemplate that doesn't display. Can anyone tell me why?

[Code]....

View 8 Replies

C# - Count All Checked CheckBox In The ListView That Has Paging?

Feb 11, 2011

I'd like take both C# and VB.NET suggestion. I have a simple ListView with DataPager like the following:

<asp:ListView ID="lvStudent" runat="server">
<LayoutTemplate>
<table id="TimeSheet" cellspacing="1" class="tablesorter">

[code]...

View 1 Replies

Web Forms :: Set GroupItem Count In ListView Control

Feb 25, 2016

I  am using listview to show the data in my application...

Populate ASP.Net ListView from database and Repeat columns horizontally using GroupTemplate but the problem is that when i am doing GroupItemCount="4" its still showing the 3 items in a row.

View 1 Replies

Forms Data Controls :: Nested Listview Has Incorrect Item Count?

Mar 8, 2011

I am trying to export a Nested Listview to Excel, but I am having issues with the code recognizing the records in the ListView. When I do a FindControl, that code works fine.The Issue arises when I try to loop through the Items of the Nested Listview. The Count always comes back as 0 (zero). However, if I do a watch on the nested listview variable and navigate to the Items collection, then the count is updated with the correct content. I've tried forcing a Databind() on the 2nd Listview, but that really shouldn't be necessary as I know the records are there. As a result, I am having issues Exporting the Data to Excel since only data from the Parent ListView is returned.

[Code]....

View 11 Replies

Data Controls :: Count Rows Item Values In ListView On Button Click?

Dec 23, 2015

I got below data on my listview:

id   salary    extra_salary   total_salary

1    1000      100               null

2    2000      50                 null

on my program, i have count button on below listview, when user click that button, system will auto count salary + extra_salary and updated into total_salary...

View 1 Replies

MVC :: Updating 1 Column In EntityFramework?

Oct 28, 2010

I have Create and Edit forms for Categories. Columns are Id, CategoryName, Order, CreatedOn. On Create form I save the values for all but on Edit form I need to change only CategoryName. Below is the code. Problem is that on Edit form only CategoryName field exists which on submit button is passed to Edit Action. Whenever I update Order and CreatedOn becomes null because the values are not returned from form. Someone tell me any good approach how to Updated selected values in EF. Below is code.

Making multiple Get calls. Form is saving fine but I don't like the approach. Need clean approach. Anyone got better solution to thiis?

[Code]....

View 5 Replies

Get The Id Of A New Record In SavingChanges Event On The Entityframework?

Jul 21, 2010

Im tring to implement some generic logging in the entityframework 4.0 using the SavingChanges event in the entity context.

I want to record details about any create/ update and deleted records including the id of the record being changed (which is always an int identity field). During the update and delete processes I can get the id for the record using

Dim AddedItems = Statemanager.GetObjectStateEntries(EntityState.Added)
For Each entry As ObjectStateEntry In DirectCast(sender, ObjectContext).ObjectStateManager.GetObjectStateEntries(EntityState.Added)
NewLogItem.RecordId = CInt(entry.EntityKey.EntityKeyValues(0).Value.ToString())
Next

But I obviously cannot get the id of the about to be inserted record during an insert becuase it hasn't been written to the database yet, is there a simple way around this?

View 3 Replies

Binding Between EntityFramework Objects And WebForm Controls?

Jun 14, 2010

With ASP.Net 4, is there any way that we can bind web-form controls to entity framework objects?

<asp:TextBox ID="UserName" runat="server" BoundTo="UserName" BindingSource="..."></asp:TextBox>

so just like in MVC I can

form.UpdateModel(userObj),
form.DisplayModel(userObj)

View 1 Replies

Web Forms :: How To Bind EntityFramework Objects With Controls

Jun 14, 2010

with asp.net 4, is there any way that we can automatically bind web controls (textbox, listbox, check, etc), to EF4 Objects?

View 2 Replies

Scenario Cause An UpdateException In EntityFramework While Calling SaveChanges?

Jan 27, 2011

SaveChanges can generate an UpdateException when an object added to the ObjectContext cannot be successfully created in the data source. This can happen if a row with the foreign key specified by the relationship already exists. When this occurs, you cannot use Refresh to update the added object in the object context. Instead, reload the object with a value of OverwriteChanges for MergeOption.

In a table, a foreign key column can have a single value multiple times. e.g. DepartmentID foreign key in Users Table: More than one User can have same DepartmentID foreign key.

So how can this cause an UpdateException ?

View 1 Replies

C# - Get A Custom Sorted EntityCollection By Default In EntityFramework ?

Jan 15, 2010

I would like to call a method in the ObjectContext which returns EntityCollection and have it sorted the way I want by default. For example, Imagine I have the following Category table/class:

CategoryID (PK)
Name
ParentID (FK_Category) foreign key
pointing to CategoryID itself.

(This table is the base for a tree structure of Categories and SubCategories)

Now, in my data model, the public partial class Category : EntityObject has a property which returns all Categories that have ParentID==CategoryID, in other words, EntityCollection<Category> SubCategories.

Alright, now i want to show in my page all the Categories and SubCategories by using Repeater:

<asp:Repeater ID="rptSubCategories" runat="server">
<ItemTemplate>
<%# Eval("CategoryID") %> - <%# Eval("Name") %>
<asp:Repeater ID="rptSubCategoryCategories" runat="server" DataSource='<%#Eval("SubCategories")) %>'>
<ItemTemplate>
<%# Eval("CategoryID") %> - <%# Eval("Name") %>
</ItemTemplate>
</asp:Repeater>
</ItemTemplate>
</asp:Repeater>

Then in code behind, I set the DataSource:

IList<Category> categoryList = new CategoryProvider().GetAll();
rptSubCategories.DataSource = categoryList;
rptSubCategories.DataBind();

Simply and everything works! Except that, rptSubCategoryCategories doesn't gives sorted Categories by Name. The only way I found to do it, is to change its DataSource from:

DataSource='<%# Eval("SubCategories")) %>'

to:

DataSource='<%# ((EntityCollection<Category>)Eval("SubCategories")).OrderBy(p => p.Name) %>'

but I wish I could do something else to have a default sorting so i don't need to call the OrderBy. Something like Attributes like we do in DynamicData, following the tutorial at [URL] Setting the default sort column of an Entity with DisplayColumnAttribute. Unless somebody tells me it's impossible what I want to do.

Using .NET 4.0 BETA 2

View 1 Replies

DataSource Controls :: Getting Some Data Out Of My EntityFramework Using Linq?

Jul 6, 2010

I got a problem when getting some data out of my EntityFramework using Linq

Heres my code

[Code]....

Heres the ClientViewModel :

[Code]....

As you can se in the ClientViewModel the Users property is a List

But when trying to compile I get a error saying

Cannot implicitly convert type 'AdminMVC.Models.Clients_Username' to 'System.Collections.Generic.List<AdminMVC.Models.Clients_Username>'

View 2 Replies

Forms Data Controls :: GridView_RowUpdated Event The E.OldValue.count E.NewValue.Count

Jun 4, 2010

In the GridView_RowUpdated event the e.OldValues.count <> e.NewValues.Count. 1 out of 5 columns has been converted to a template field.

View 9 Replies

Forms Data Controls :: Gridview Row Count / How To Get A Count Of The Number Of Rows That Are Returned

Dec 17, 2010

When a user submits the query to SQL when clicking a button I need to get a count of the number of rows that are returned. Which event does the gridview.count code need to be place in?

Right now I have it in the btn_click event, but in order to get it to count the rows you have to hit the button twice since the query is not returned yet. Here is my code.

[Code]....

View 1 Replies

Data Controls :: Display Total Row Count And Count Of Specific Types In Label In GridView

Oct 5, 2012

I HAVE A GRID VIEW IN asp.net + VB Code. There is a drop down list and the selected data is displayed in web page. I placed a label in web page and code behind i used following code.

Protected Sub GridView1_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DataBound
Label1.Text = GridView1.Rows.Count.ToString()
Dim ONYO As Integer = 0
Dim DONE As Integer = 0
For i As Integer = 0 To GridView1.Rows.Count - 1
If GridView1.Rows(i).Cells(0).Text = "ON YO" Then

[Code] .....

View 1 Replies

Entity Framework - .NET EntityFramework 4 Data Context / What Is The Best Practice

Oct 2, 2010

I'm working on a project which is using EntityFramework 4 and I am using the entity objects as my business objects. I ran into an issue recently where I had a context declared in a using statement in a user control. The method the statement was in returned an entity object which got used in another control. So I had to detach the entity then attach it to the new context in the other control. I would like to avoid this if possible. What I'm thinking is I would like to declare a context in the master page and then pass that to any page/usercontrol that needs it so they are all using the same context and I don't have to write all these using statements.

My questions are these:

1) is it a bad practice to declare a context on Pre_Init/Page_Load and then dispose of it on Page_Unload?

2) if it is what is the best practice for handling them?

3) if I do go the route of declaring the context in the master page what is the best way to pass that to the pages/usercontrols?

View 1 Replies

ADO.NET :: Intercept/extend Createquery In Entityframework To Allow Global Filter?

Feb 22, 2011

I have not found any way to intercept or override calls to createquery in my datacontext. What I am trying to do is to allow for global filters, i.e. if a user has access to only a subset of entities I would like to filter away these on the lowest level possible. Is there a way to do this? I have tried several apporaches but no one worked out. Perhaps I lack patience or is just plain stupid, but I think it should be possible! So if anyone has any pointers to where

View 2 Replies

DataSource Controls :: EntityFramework Base Class Inheritance?

Feb 11, 2010

I am having some issues with my base class inheritance. I am currently employing Table per Type Inheritance for the following tables:

Database:

Members Table

- Member ID (PK)

- First Name

- Last Name

- Email

- ...

Students Table

- Member ID (FK)

- Credits

- Notes

- ...

Instructors Table

- Member ID (FK)

- Biography

- Office Hours

- ...

My Entity Data Model defines "Member" as the base class for both Students and Instructors, since members can be both students as well as instructors simultaneously. The problem, however, occurs when I attempt to get a list of Members objects. Because the database contains members who are indeed students AND instructors, the following exception is thrown:

All objects in the EntitySet 'CountyCollegeEntities.BaseMembers' must have unique primary keys. However, an instance of type 'CountyCollege.NET.Administrator' and an instance of type 'CountyCollege.NET.Student' both have the same primary key value, 'EntitySet=BaseMembers;ID=10016'.

I am beginning to think that I made a mistake by building these classes to inherit from Member although it has been ideal up until this point. But before I start ripping up all my existing code to remove this inheritance, I thought I would see if anyone has any tricks that would make this work. Any advice as to either how I can get around this error or a more appropriate way to structure my classes would be very much appreciated.

View 2 Replies

DataSource Controls :: Using Views With EntityFramework Is Not Able To Recognize Primary Keys

May 2, 2010

Cannot seem to find a good fit for my question so I will ask here.

Does Asp.Net 4.0 solve the problems with using Views with EntityFramework is not able to recognize primary keys.

View 2 Replies

Can A DataBinder.Eval Be Nested Within Another DataBinder.Eval

Jul 16, 2010

Can you do something like :

<%# DataBinder.Eval(Container.DataItem,"Column1").ToString().Replace("_",<%# DataBinder.Eval(Container.DataItem,"Column2") %> %>

Can a DataBinder.Eval be nested within another DataBinder.Eval?

View 1 Replies

ADO.NET :: Update Single Item With EntityFramework - How To Update Object

Oct 27, 2010

I want to update object via entity framework

For example I have to changed object:

Obj1
Obj2

I want to save change to the Obj1 Only

[code]....

I try to Save with ApplyPropertyChanges but the data didn't saved

How can I update object?

View 2 Replies







Copyrights 2005-15 www.BigResource.com, All rights reserved