Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 964
Kay Avatar asked May 03 '11 11:05

Kay


2 Answers

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();
like image 190
Nasser Hadjloo Avatar answered Nov 19 '22 17:11

Nasser Hadjloo


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();
like image 35
atefeNaeni Avatar answered Nov 19 '22 17:11

atefeNaeni