Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
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 22-Jan-13 20:36pm
Edited 22-Jan-13 21:46pm
v3
Comments
Thomas Duwe at 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 at 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
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.
  Permalink  
Comments
A C swathi at 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..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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
  Permalink  
v3
Comments
A C swathi at 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)

  Print Answers RSS
0 OriginalGriff 383
1 ChauhanAjay 186
2 Richard Deeming 180
3 Sergey Alexandrovich Kryukov 171
4 Yogesh Kumar Tyagi 165
0 Sergey Alexandrovich Kryukov 6,252
1 OriginalGriff 5,498
2 CPallini 2,473
3 Richard MacCutchan 1,627
4 Abhinav S 1,530


Advertise | Privacy | Mobile
Web04 | 2.8.140821.2 | Last Updated 23 Jan 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