Click here to Skip to main content
15,886,025 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
try
        {
            RealEstateDataClassesDataContext db = new RealEstateDataClassesDataContext();
            if (ddlstate.SelectedItem.Text != "Select State" && dp_city.SelectedItem.Text == "Select City")
            {
                var query = db.getagentdetailsbystate(dp_usertype.SelectedItem.Text, int.Parse(ddlstate.SelectedItem.Value));
                if (query.Count()>0)
                {
                    GridView1.DataSource = query;
                    GridView1.DataBind();
                    GridView1.Visible = true;
                    lb_nodata.Visible = false;
                }
                 
            }
            else if (ddlstate.SelectedItem.Text != "Select State" && dp_city.SelectedItem.Text != "Select City")
            {
                var query1 = db.getagentdetails1(dp_usertype.SelectedItem.Text, int.Parse(ddlstate.SelectedItem.Value), int.Parse(dp_city.SelectedItem.Value));
                if (query1 .Count() > 0)
                {
                    GridView2.DataSource = query1;
                    GridView2.DataBind();
                    GridView1.Visible = false;
                    lb_nodata.Visible = false;
                }
                else
                {
                    lb_nodata.Visible = true;
                    lb_nodata.Text = "Data Is not Available";
                }
                
            }
            else
            {
                lb_nodata.Visible = true;
                lb_nodata.Text = "Data Is not Available";
            }

             
        }


Here I got the error that the query results cannot be enumerated more than once. Where is the problem?
Can anybody solve that?

Thanks in advance!
Posted
Updated 5-Apr-11 3:28am
v2
Comments
luisnike19 5-Apr-11 10:02am    
which line throw that error?
what does getagentdetailsbystate do?
Kim Togo 5-Apr-11 15:47pm    
Perhaps "RealEstateDataClassesDataContext" is a forward-only database lookup ?

1 solution

It's pretty tough to say why without seeing all of your code. But, I guess the reason is this:

In the line

var query = db.getagentdetailsbystate(dp_usertype.SelectedItem.Text, int.Parse(ddlstate.SelectedItem.Value));


db.getagentdetailsbystate probably returns an IEnumerable<T>, When you call

query.Count()


you have enumerated your results. Then you are assigning this as a data source to GridView1, which enumerates the results again thereby causing this error. To resolve this just convert the results from db.getagentdetailsbystate to a list as shown below:

var query = db.getagentdetailsbystate(dp_usertype.SelectedItem.Text,int.Parse(ddlstate.SelectedItem.Value)).ToList();


Similarly for query1 convert it to a list as shown before. That should resolve your problems...

Hope this helps!
 
Share this answer
 
v2
Comments
Henry Minute 5-Apr-11 20:08pm    
Sounds reasonable to me! +5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900