Search code examples
c#asp.netlinqc#-4.0entity-framework-4

How to bind gridview using linq/Entity Framework?


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?


Solution

  • 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();