Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi..
i want to bind datas from two different tables into a single datagridview.Here i am using batch select query to get datas,and binding can be done using datareader or dataadapter.But i am getting datas from first table only.How can i bind datas from two tables here.Am attaching the code below.Help me please..
Thanks in advance.
swathi.

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
bindgrid();

}
private void bindgrid()
{

SqlConnection con = new SqlConnection("Data Source=(local);Initial Catalog=book;User ID=sa;Password=nest123@!");
con.Open();
SqlCommand cmd = new SqlCommand(@"select id,name,author,price from sale;select invc_id,totalprice from bill", con);
SqlDataAdapter sda=new SqlDataAdapter(cmd);
cmd.ExecuteNonQuery();
SqlCommandBuilder sc = new SqlCommandBuilder(sda);
DataTable dt = new DataTable("sale", "bill");

sda.Fill(dt);
dataGridView1.DataSource = dt;









}





}
}
Posted
Updated 22-Jan-13 21:46pm
v3
Comments
Thomas Duwe 23-Jan-13 3:50am    
I think you have to change the sql command.
Now you send a command with 2 select commands inside. Usually only the results of the second
command are retrieved from the database.

I think your command should be:
select sale.id,sale.name,sale.author,sale.price,bill.invc_id, bill.totalprice from sale, bill

Although this select statement really doesn't make much sense.
Usman Khan Mohammad 23-Jan-13 3:57am    
Use the following query
select sale.id,sale.name,sale.author,sale.price,bill.invc_id, bill.totalprice from sale, bill

Couple of things here:
1. The second parameter is not DataTable.
2. What do you want to with the data tables, I see you are selecting 6 columns from 2 tables. Do you want to show six columns in the data grid? If so you will have to write a single query joining the tables, get the result set into single data table and bind to the grid.
 
Share this answer
 
Comments
A C swathi 23-Jan-13 4:13am    
Thanks.I have to use batch select query to retrive datas from two tables.ya i want to show the six columns in a single datagrid.join query cant be used here.Here i am testing the working of batch select query,that should be include in project.Thats why.whether i have to two datasets and two dataadpters for retrieve datas from two tables.and whether i have to use datarelation here..please reply..
If you want six columns in same query you can use Join between Sale and Bill table. It seems there is no relation between these two tabels. So You can Use the following query.
select sale.id,sale.name,sale.author,sale.price,bill.invc_id, bill.totalprice from sale, bill
 
Share this answer
 
v3
Comments
A C swathi 23-Jan-13 4:14am    
Thanks.But I have to use the batch select query not the join query.whether i have to two datasets and two dataadpters for retrieve datas from two tables.and whether i have to use datarelation here..please reply..

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