8

I need to bind GridView, I am using this code:

  ProductDBEntities db = new ProductPDBEntities();

    var pro = from u in db.Products where u.PID == 1 select u;

    if (pro != null)
    {
        GridView1.DataSource = pro;
        GridView1.DataBind();
    }

and getting this error.

System.InvalidOperationException: Sequence contains more than one element

Can somebody please tell me what am I doin wrong?

6
  • I think you will need to convert it to ToList() for it to work as DataSource for a gridview Commented May 3, 2011 at 11:59
  • i tried ToList function its give me this error .. Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource ELAPDBEntities db = new ELAPDBEntities(); var assetFinan = from u in db.AssetFinancings where u.AppID == AppID select u; if (assetFinan != null) { gvAssetFinance.DataSource = assetFinan.ToList(); gvAssetFinance.DataBind(); } Commented May 3, 2011 at 12:15
  • U need to get ToList when you are assigning it to pro Commented May 3, 2011 at 12:22
  • can you give me an example .. please Commented May 3, 2011 at 12:24
  • You don't need ToList(). You can bind an IEnumerable to a GridView. And pro in your code is an IQueryable which is an IEnumerable. The exception makes no sense in the context of the code you have shown above. On which line do you get exactly this exception when you debug? Commented May 3, 2011 at 12:31

6 Answers 6

3

Check Duplication and then try to bind it.

I have edited my last answer in order to display the complete code :

ProductDBEntities db = new ProductPDBEntities();
GridView1.DataSource = (from u in db.Products where u.PID == 1 select u).First();
GridView1.DataBind();
Sign up to request clarification or add additional context in comments.

2 Comments

thanks for that .. but i m having problem displaying. getting error .. can you post your code how do you bind the gridview ?
First() returns a Product object. I don't think that this object has a ToList() method, has it?
1

This code may be helpful:

    gv.DataSource = (from u in db.Products .First(u=> u.PID==1)).ToList();

If you have a table and table detail can use this one:

    gv.DataSource = (from a in context.tblorder.First(p => p.id == 19).tblorderdetail where a.ID == 19 select a).ToList();

Comments

0

First, you might check your data to see if there's more than one product with PID = 1.

Second, you can use the .First() method to make sure you get only one result for binding:

var pro = (from u in db.Products where u.PID == 1 select u).First();

Comments

0

Store the variable to object type and assign the object type as datasource to grid

Comments

0

I think you have to do ToList() when to add to DataSource:

ProductDBEntities db = new ProductPDBEntities();

var pro = from u in db.Products where u.PID == 1 select u;

if (pro != null)
{
    GridView1.DataSource = pro.ToList();
    GridView1.DataBind();
}

Note: As it is a GridView, has to take n number of rows. No row limit.

Comments

0

I solve like this:

ProductDBEntities ctx = new ProductDBEntities ();
var q1 = ctx.Products.Where(x => x.PID == 1).ToList();
GridView1.DataSource = q1;
GridView1.DataBind();

I try it. It's work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.