2013-08-27 45 views
6
Task03Entities.Entites entities = new Task03Entities.Entites(); 

// Creat a object for my entites class 
Task03BAL.BAL bal = new Task03BAL.BAL(); 

// creat a object of BAL to call Get Data Method 
List<Task03Entities.Entites> entitiesList = new List<Task03Entities.Entites>(); 

// make a list of class Entities 
entitiesList = bal.GetData(entities); 

// store data in list 
ViewState.Add("Products", entitiesList.ToArray()); 

// use view state to store entitieslist 
Task03Entities.Entites[] entitiesArray =(Task03Entities.Entites[])ViewState["Products"]; 
List<Task03Entities.Entites> ViewStateList =new List<Task03Entities.Entites(entitiesArray); 

// so now ViewStateList contain entitieslist data 
// Now the Problem which i am facing is 
if (Request.QueryString["count"] != null) // this is okay 
{ 
    listCount =Convert.ToInt32(Request.QueryString["count"]); 
} 

for (int i = (listCount * 2)-2; i < listCount * 2; i++) 
{ 
    Task03Entities.Entites newViewStateList = new Task03Entities.Entites(); 
    newViewStateList = ViewStateList[i]; 

// view state contain a list of data here on above line m trying to acess single data 
%> 
<table> 
<%     

foreach (Task03Entities.Entites item in newViewStateList) 
// on above line i am getting following error message 

Compiler Error Message: CS1579: Câu lệnh foreach không thể hoạt động trên biến của loại 'Task03Entities.Entites' vì 'Task03Entities.Entites' không không chứa định nghĩa công khai cho 'GetEnumerator'foreach không thể hoạt động trên các biến kiểu định nghĩa nào cho 'GetEnumerator'

+0

'ViewStateList' là gì? –

+0

của nó một danh sách mà conatin ViewState dữ liệu ... thực sự đầu tiên tôi lấy tất cả dữ liệu từ cơ sở dữ liệu sau đó lưu trữ nó trong một ViewState các dữ liệu ViewState vào ViewStateList –

Trả lời

11

Rất đơn giản: Task03Entities.Entites không phải là một bộ sưu tập.

Bạn khởi tạo một đối tượng ở đây:

Task03Entities.Entites newViewStateList = new Task03Entities.Entites(); 

Và sau đó cố gắng để lặp qua nó như thể nó là một bộ sưu tập:

foreach (Task03Entities.Entites item in newViewStateList) 
{ 

} 

tôi nghi ngờ bạn muốn một cái gì đó như:

List<Task03Entities.Entites> newViewStateList = new List<Task03Entities.Entites>(); 

foreach (Task03Entities.Entites item in newViewStateList) 
{ 
    //code... 
} 
+0

Thực sự tôi muốn nhận được các mục cụ thể từ ViewStateList và sau đó lưu nó newViewstateList. Ví dụ có tổng số 6 mục trong ViewStateList và tôi muốn có được một mục duy nhất một bye một thông qua cho vòng lặp và sau đó hiển thị mỗi mục thông qua foreachloop beacuse mỗi mục chứa nhiều nộp như ID FirstName LastName ect ... –

+0

Không liên quan - 'newViewStateList' không một bộ sưu tập ** bạn không thể lặp lại nó ** - đây là nguyên nhân gây ra lỗi của bạn. – DGibbs

1

Xem thông báo loại của bạn Task03Entities.Entites không phải là tập hợp và/hoặc không thể đếm được. Vì vậy, để có thể sử dụng foreach trên đó, bạn phải làm cho nó như vậy.

Ví dụ có thể có một cái nhìn về: How to make a Visual C# class usable in a foreach statement

0

chỉ cần thêm @model IEnumerable int the firs t thay vì dòng @model thông thường của bạn

Các vấn đề liên quan