Click here to Skip to main content
15,913,487 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear Sir/Madam.
Quote:
How to achieve the join query in c# code behind file and display the result in gridview..


Quote:
Assume that there are two tables: Products and Sales

Products:

ProductID | Name | Manfacturer | Rate | Date
-------------------------------------------------------
1 Scooty TVS 35000 10/10/2012
2 Splendor Hero Honda 50000 5/6/2012

Sales:

SaleID | CustomerID | ProductID | Qty | SoldDate
------------------------------------------------------
1 1 2 1 12/12/2012


Here i want to join these two tables. It will return the matching records in both tables.

SELECT S.SaleID, P.ProductID, P.ProductName, P.Manfacturer, S.CustomerID, S.SoldDate, S.Qty,P.Rate, (s.Qty * P.Rate) AS [Total] FROM Products P INNER JOIN Sales S ON s.ProductID = P.ProductID

The above query returns the result set as

SaleID | ProductID | ProductName | Manfacturer | CustomerID | SoldDate | Qty | Rate | Total
---------------------------------------------------------------------------------------------------------
1 2 Splendor Hero Honda 1 12/12/2010 1 50000 50000.00
Posted
Comments
Darshan.Pa 15-May-14 6:58am    
how do you want ??

with sql or Linq (edmx)
Telstra 15-May-14 7:05am    
above query is right. Then what and which query you want to write?

1 solution

sample code

C#
string sql = "SELECT S.SaleID, P.ProductID, P.ProductName, P.Manfacturer, S.CustomerID, S.SoldDate, S.Qty,P.Rate, (s.Qty * P.Rate) AS [Total] FROM Products P INNER JOIN Sales S ON s.ProductID = P.ProductID";
using (SqlConnection con = new SqlConnection("connectionString"))
using (SqlDataAdapter sadp = new SqlDataAdapter(sql, con))
{
    DataSet ds = new DataSet();
    sadp.Fill(ds);
    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
}
 
Share this answer
 

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