Behseini
Behseini

Reputation: 6330

ASP.Net and Entity Framework Filter Search to Gridview

I am working on ASP.NET application which is using Entity Framework and getting data from a Database. I have following code to filter rendering data on a Grid View. I tried this code which it wrong for sure!

 protected void btnSearch_Click(object sender, EventArgs e)
        {
            GISEntities gis = new GISEntities();
            GIS_CONTRACTOR_TEST tbl = gis.GIS_CONTRACTOR_TEST.ToList().Where(x => x.CONTRACTORNAME == txtSearch.Text).First();
            GridView1.DataSource = tbl.CONTRACTORNAME;
            GridView1.DataBind();
        }

As you can see I have problem on GridView1.DataSource = tbl.CONTRACTORNAME; which I couldn't find any other property for tbl except of field constructors. Can you please let me know how I can filter the database into a grid View instead of displaying them separately!

Thanks

Upvotes: 0

Views: 2750

Answers (2)

Reza Shirazian
Reza Shirazian

Reputation: 2353

If I was to assume GISEntities is your database context then try this.

var result = (from a in gis.GIS_CONTRACTOR_TEST where a.CONTRACTORNAME == txtSearch.Text select a).ToList();

GridView1.DataSource = result;
GridView1.DataBind();

If you're not finding the correct properties in the result variable I would take a look at your GIS_CONTRACTOR_TEST model and database context, making sure it is defined correctly.

Upvotes: 2

Ravinder Gujiri
Ravinder Gujiri

Reputation: 1514

try this

GISEntities gis = new GISEntities();
GridView1.DataSource = gis.GIS_CONTRACTOR_TEST.Where(m => m.CONTRACTORNAME == txtSearch.Text).ToList();
GridView1.DataBind();

Upvotes: 1

Related Questions