Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
hi,
 
with this code am getting table data on datagridview. but i want multiple table data to retrieve on single datagridview.
 
in each table userid is common cloumn.
 
SqlConnection conn = new SqlConnection();
            conn.ConnectionString = Helper.ConnectionString;
 
            conn.Open();
            SqlDataAdapter da = new SqlDataAdapter("Select UserId,UserName,UserDomainName,UserMachineName,UserIP,UserOsVersion,UserSystemDirectory,UserCurrentDirectory,ProcessorName,ProcessMnufacturer,ProcessorID,ProcessorDescription,ProcessorVersion,ProcessorStatus,ProcessorDeviceId,OSCaption,OSSerialNumber,OSManufacturer,OSVersion,OSStatus,OSName,BiosName,BiosVersion,BiosSerialNumber,BiosManufacturer,BiosCurrentlanguage,BiosStatus FROM SystemInfo Where UserId=@UserId", conn);
 
            //SqlDataAdapter da = new SqlDataAdapter("Select DisplayName,ServiceName,Status,ServiceType FROM ServiceInfo Where MainId=@MainId", conn);
            {
                da.SelectCommand.Parameters.AddWithValue("@UserId", textBox1.Text);
                DataTable dt = new DataTable();
                da.Fill(dt);
                dataGridView1.DataSource = dt;
 

can anyone help me with sample code example.
 
Thanks & Regards
sam.198979
Posted 24-Jun-13 3:02am
Comments
ThePhantomUpvoter at 24-Jun-13 8:04am
   
Sample code for what? Change your query to get the data from multiple tables.
Rajesh Anuhya at 24-Jun-13 8:40am
   
but i want multiple table data to retrieve on single datagridview --mean can you explain it more?
jaideepsinh at 24-Jun-13 8:44am
   
If you want data from multiple table use joins.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

Quote:
but i want multiple table data to retrieve on single datagridview

You have created one databale for the query which you mentioned in your code to retrieve from database.
Now, create one more SQL query and datatable to fetch records from another table which you want and add it to newly created datatable.
 
Now create one more datatable - third one. Add data from first & second to it.
 
First add columns of datatable 1 & then datatable 2.
and then, add data from each tables to this one.
 
Once you've done with it, use the new datatable [third one] as a datasource for grid.
 
For a similar schema datatable, use DataTable.Merge Method[^]
  Permalink  
Comments
sam.198979 at 27-Jun-13 3:11am
   
Thanks ;)
Prasad_Kulkarni at 27-Jun-13 7:10am
   
Glad it helps!
You're welcome.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Change your select to include the data from the other tables as well:
SELECT a.ID, a.Col1, b.Col1 
FROM Table1 a
JOIN Table2 b ON a.ID=b.ID
WHERE ID=@ID
If you don't specify a column, it won't be retrieved!
  Permalink  

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



Advertise | Privacy | Mobile
Web03 | 2.8.1411022.1 | Last Updated 24 Jun 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100