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!