Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi,
I have a grid view and it shows the data from a table on page load.now there is a search button which when clicked shows textboxes and one can enter text by which they want to search the data . when you click go ,the same grid view must show the search results.So i wrote ado.net code on the buton click making the data source for gridview as dt(data table) but when i run it i get an error saying that both datasource n datasourceid are given n to remove 1. the datasourceid is the database table from which the data comes on pageload. how can i supply 2 data sources for the same grid view in order to achieve this?
Posted

Is it because you use first datasourceid to bind your gridview and then on button click you bind the gridview with DataSource?

1. One easy option is to set datasourceid to null on button click. Something like below:
GridView1.DataSourceID = null;
2. Other option can be to bind your gridview to datasource from codebehind in both the cases. Store the data in the ViewState (or session) in page load and bind your gridview, later when you click on button, fetch the data from viewstate/ session filtering it based on your search criteria.
 
Share this answer
 
Comments
Laksh112 24-Mar-11 0:59am    
yes that is true....on aspx page datasourceid is set and in code behind i bind gridview.datasource to datatable.I will tryout ur suggestions...thanks :)
Member 15545102 23-Feb-22 4:48am    
thanks brother
You cannot have 2 data sources at the same time. Why not use a LINQ query on your datatables and from that query extract a new collection or data table which you then use as a data source to the gridview.
 
Share this answer
 
Comments
Laksh112 23-Mar-11 13:26pm    
ok will try doing that.... thnx 4 ur reply
Wendelius 23-Mar-11 13:32pm    
No problem :)
Laksh112 23-Mar-11 14:10pm    
ok i tried linq but i am new to it(actually am quiet new to .net) and am not able to get it right....can you send me a sample?
Wendelius 23-Mar-11 14:34pm    
I'm sure there are lots of articles etc in CP and elsewhere. Quickly found one simple for you which uses datatables: http://www.codeproject.com/KB/windows/DatasetsAndLINQ.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900