Hi,
I want to join tables and display in gridview.
thanks
Loading
Know the answer? Post it — somebody with the same question will find it here.
Sign in to answer this question
It is the same account you read, post and publish with — and you will come straight back to this page.
Datta KharadPosted Dec 7, 2011, 2:26 AM
SELECT
FROM
ON
//Traditional Joins
SELECT p.last_name, p.address, c.designation, c.salary
FROM person p, Company c
WHERE p.eid = c.eid;
//ANSI Joins (Recommended)
SELECT p.last_name, p.address, c.designation, c.salary
FROM person p INNER JOIN Company c
ON p.eid = c.eid;
//Example:-
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class Data : System.Web.UI.Page
{
SqlConnection con;
SqlDataAdapter sda;
DataSet ds;
protected void Page_Load(object sender, EventArgs e)
{
con = new SqlConnection();
con.ConnectionString = "Data Source=.;Database=Train;User Id=scott;Password=tiger";
con.Open();
sda = new SqlDataAdapter("SELECT p.last_name, p.address, c.designation, c.salary
FROM person p INNER JOIN Company c
ON p.eid = c.eid", con);
ds = new DataSet();
sda.Fill(ds);
GridView1.DataSource = ds;
GridView1.DataBind();
}
Satyapriya NayakPosted Dec 7, 2011, 2:22 AM
You can do it in two ways in query as below
str = "select customer.custid,customer.custname,customer.custaddress,product.prodname,product.prodprice from customer, product where customer.prodid = product.prodid";
str = "select cust.custid,cust.custname,cust.custaddress,prod.prodname,prod.prodprice FROM customer cust INNER JOIN product prod ON cust.prodid = prod.prodid";
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com;
SqlDataAdapter sqlda;
DataSet ds;
string str;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
bindgrid();
}
}
void bindgrid()
{
SqlConnection con = new SqlConnection(connStr);
con.Open();
str = "select customer.custid,customer.custname,customer.custaddress,product.prodname,product.prodprice from customer, product where customer.prodid = product.prodid";
//str = "select cust.custid,cust.custname,cust.custaddress,prod.prodname,prod.prodprice FROM customer cust INNER JOIN product prod ON cust.prodid = prod.prodid";
com = new SqlCommand(str, con);
sqlda = new SqlDataAdapter(com);
ds = new DataSet();
sqlda.Fill(ds, "customer");
GridView1.DataSource = ds;
GridView1.DataMember = "customer";
GridView1.DataBind();
con.Close();
}
}
Thanks