0

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

2
  • What exactly is the problem? Are you getting an error or are you getting results other than expected? Commented Feb 27, 2014 at 22:56
  • well this code is logically wrong! I want to get the result in gridview but I am accessing only to the columns property from tbl.! Commented Feb 27, 2014 at 23:07

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Merci Reza, but I am getting "Data source is an invalid type. It must be either an IListSource, IEnumerable, or IDataSource." error on running your code!
Thanks Reza, it is working now. just one quick question? Are yo using the Linq to query the data? so what is different between your method by using "from a in gis.CONTRACTOR.TEST where.... select a" and Where() which G Ravinder used?
I am using linq query, the final result shouldn't be different than what you were doing. It's what I prefer personally over calling Where() and what not.
1

try this

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

2 Comments

Thanks G RAvinder, just can you please also let me know how I can check if there is any thing to return or not? I mean sending a message to the users if there wasn't any matches in the database?
just use <asp:GridView ID="GridView1"> <EmptyDataTemplate>No results found.</EmptyDataTemplate> </asp:GridView> it will show "No results found." when there no matching results in database.

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.