Click here to Skip to main content
Sign Up to vote bad
good
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:36
Edited 22 Jan '13 - 21:46

Comments
Thomas Duwe - 23 Jan '13 - 3:50
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:57
Use the following query select sale.id,sale.name,sale.author,sale.price,bill.invc_id, bill.totalprice from sale, bill

2 solutions

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 - 23 Jan '13 - 4:13
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
  Permalink  
Comments
A C swathi - 23 Jan '13 - 4:14
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
Your Filters
Interested
Ignored
     
0 Sergey Alexandrovich Kryukov 388
1 OriginalGriff 291
2 Mayur_Panchal 153
3 Mohammed Hameed 145
4 Dave Kreskowiak 125
0 Sergey Alexandrovich Kryukov 8,146
1 OriginalGriff 6,236
2 CPallini 3,482
3 Rohan Leuva 2,703
4 Maciej Los 2,234


Advertise | Privacy | Mobile
Web01 | 2.6.130516.1 | Last Updated 23 Jan 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid