How to populate a GridView using multiple DataSource?
Using Multiple DataSource populates the same GridView. ie I need to populate data in a GridView using more than one DataSource. Please provide a code snippet if possible ...
More details: - Tables with the same schema are present in two different Databases. I need to get data from both and fill it inside one GirdView.
source to share
You can combine your result objects into one DataTable by using two SqlDataAdapters to populate the DataTable from the two databases respectively. Here's an example.
DataTable dt = new DataTable();
using(SqlDataAdapter a1 = new SqlDataAdapter("SELECT * FROM [user1]", "Data Source=DBServer1;Initial Catalog=Database1;User ID=user;Password=***"))
a1.Fill(dt);
using(SqlDataAdapter a2 = new SqlDataAdapter("SELECT * FROM [user2]", "Data Source=DBServer2;Initial Catalog=Database2;User ID=user;Password=***"))
a2.Fill(dt);
a1.Fill (dt) initializes the DataTable and fills it. a2.Fill (dt) just adds rows to DataTable dt from another result set. This example assumes that two data sources have the same schema. If not, you must prepare the data to accommodate both sets of results.
Hope it helps.