Click here to Skip to main content
15,897,273 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can i write Program to display table values from the database..
i have 2 table in my database ...
i want to join between this tables and display these values in Gridview..
please any one help me regarding this...
thanks
Posted
Comments
ClimerChinna 2-Sep-14 2:51am    
send me those tables details and tel us which details you want to display

To display the data from multiple table use the join.

Suppose the Tables are:

table1 : Dept(Deptno,Deptname,LOcation)
table2 : Emp(empno,empname,salary,deptno)


Then to display the data from both tables in combined form use the inner join
select empno,empname,salary,emp.deptno,deptname,location
from Emp inner join Dept
ON Emp.deptno = Dept.deptno


Then output of this query is used in Dataset:

string qry = "
SQL
select empno,empname,salary,emp.deptno,deptname,location from Emp inner join Dept ON Emp.deptno = Dept.deptno";
  DataSet ds; 
  SqldataAdapter adpt = new SqlDataAdapter(qry,cn);
  ds = new DataSet();
  adpt.fill(ds);
  GridView1.DataSourec=ds.Tables[0];
  GridView1.DataBind();
 
Share this answer
 
In page Load or in button Click you can call this function :
C#
private void bindData()
{
SqlConnection con=new SqlConnection();
con.ConnectionString="Your Connection String";
con.open();
Dataset ds;
SqlDataAdapter da;
da=new SqlDataAdapter("select S1.Col1,S1.Col2,S1.Col3,S1.Col4,S2.Col2,S2.Col3,S2.Col4 from Table1 s1
 inner join 
Table2 s2 on s1.id=s2.id where s1.dates Between dte1 AND dte2",con);
da.fill(ds,"MultiTable");
Gridview1.DataSource=ds;
Gridview1.Bind();
}
 
Share this answer
 
you need to join two tables and fetch the results as below

using (SqlConnection connection = new SqlConnection(ConnectionString))
            using (SqlCommand cmd = connection.CreateCommand())
            {
                connection.Open();
                cmd.CommandText = "SELECT T1.Id, T1.Name,  T2.Address FROM table1 T1 INNER JOIN Table2 T2 ON T1.Id= T2.Id";
                SqlDataAdapter adap = new SqlDataAdapter(cmd);
                DataSet ds = new DataSet();
                adap.Fill(ds);
                dataGridView1.DataSource = ds.Tables[0].DefaultView;
            }
 
Share this answer
 
i have given an example for this below check that,

following is Programmer table:

PID-----PName-----DOB-------------DOJ-----------Gender--Prof1---Prof2
1-------ANAND-----1965-04-12------1992-04-21----M-------PASCAL--BASIC
3-------ALTAF-----1964-07-02------1990-11-13----M-------CLIPPER-COBOL
4-------JULIANA---1969-01-31------1990-04-21----F-------COBOL---DBASE
5-------KAMALA----1977-10-30------1992-01-02----F-------C-------DBASE
6-------MARY------1968-06-24------1991-02-01----F-------CPP-----ORACLE
7-------NELSON----1965-09-11------1989-10-11----M-------COBOL---DBASE
8-------RAMESH----1963-05-03------1991-02-28----M-------PASCAL--DBASE


below is software table:

PID---Title------------DevelopingLanguage----SCost---DCost---Sold
6-----README-----------CPP-------------------300-----1200----84
1-----PARACHUTES-------BASIC-----------------399.95--6000----43
1-----VIDEO TITLING----PASCAL----------------7500----16000---9
4-----INVENTORY--------COBOL-----------------3000----3500----0
5-----PAYROLL PKG.-----DBASE-----------------9000----20000---7
6-----FINANCIAL ACCT.--ORACLE----------------18000---85000---4
6-----CODE GENERATOR---C---------------------4500----20000---23
9-----VACCINES---------C---------------------1900----3100----21
8-----HOTEL MGMT.------DBASE-----------------13000---35000---4
8-----DEAD LEE---------PASCAL----------------599.95--4500----73
2-----PC UTILITIES-----C---------------------725-----5000----51
2-----TSR HELP PKG.----ASSEMBLY--------------2500----6000----7
3-----HOSPITAL MGMT.---PASCAL----------------1100----75000---2
4-----TSR EDITOR-------C---------------------900-----700-----6


now i need to display the Details of software Developed by Ramesh.
for that i need to join programmer and software tables and display the details.
here is the query for this:

SQL
Select Title, DevelopingLanguage, SCOST, DCOST, SOLD from Software INNER JOIN Programmer
ON Software.PID=Programmer.PId where Programmer.PName = 'RAMESH'



Title-----------DevelopingLanguage------SCOST----DCOST----SOLD
HOTEL MGMT.-----DBASE-------------------13000----35000----4
DEAD LEE--------PASCAL------------------599.95---4500-----3


string qry="Select Title, DevelopingLanguage, SCOST, DCOST, SOLD from Software INNER JOIN Programmer
ON Software.PID=Programmer.PId where Programmer.PName = 'RAMESH'";
  DataSet ds; 
  SqldataAdapter adpt = new SqlDataAdapter(qry,cn);
  ds = new DataSet();
  adpt.fill(ds);
  GridView1.DataSourec=ds.Tables[0];
  GridView1.DataBind();
 
Share this answer
 
v4

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