Click here to Skip to main content
15,884,176 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to display two tables in a single grid view which are having different object names.
C#
SqlConnection con = new SqlConnection(ConnectionString);

String query= "select *from Employee1 ; select *from Employee2";

SqlCommand cmd = new SqlCommand(query,con);

SqlDataReader dr;

con.Open();

dr = cmd.ExecuteReader();

GridView1.DataSource = dr;

GridView1.DataBind();

con.Close();
Posted
v2

Combine Two Query in One otherwise use SqlAdapter with Dataset.
SqlConnection con = new SqlConnection(ConnectionString);

    String query = "select * from Employee1 , Employee2";

    SqlCommand cmd = new SqlCommand(query,con);

    SqlDataReader dr;

    con.Open();

    dr = cmd.ExecuteReader();

    GridView1.DataSource = dr;

    GridView1.DataBind();

    con.Close();
 
Share this answer
 
v2
Comments
Anil Vaghasiya 17-Jul-15 8:45am    
Hello,

If this solution is useful or helped to you then Please mark answer as Solution.

Thank You.. :)!!
Member 11843710 22-Jul-15 2:46am    
my issue is to read the two tables with sqlreader.
and need to display them in one gridview.
but at this situation i am able to display only one table with this query

"select *from Employee1 ; select *from Employee2";

i think i need to use NextResult() to overcome this.And thanku dude for the response
Anil Vaghasiya 22-Jul-15 2:53am    
Hello,
Ya Its True if You use Both Query then Use NextResult() to Overcome Otherwise to Use Dataset.
--AV
You need to either join the tables in the query if there's a relationship between them or if the 'same' data is distributed between two tables you can use UNION.

For more information see
- Join Fundamentals[^]
- UNION[^]

Also you can fetch multiple results sets if you like. Have a look at Executing multiple SQL statements as one against SQL Server[^]
 
Share this answer
 
v2
Use SqlDataAdapter with DataSet. DataSet can contain multiple tables.
 
Share this answer
 
Comments
Member 11843710 22-Jul-15 2:47am    
my issue is to read the two tables with sqlreader.
and need to display them in one gridview .
but at this situation i am able to display only one table with this query

"select *from Employee1 ; select *from Employee2";

i think i need to use NextResult() to overcome this.And thanku dude for the response
You can use that. Better to use DataAdapter for DataSets.

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