Subscribe to Subscribers

Forums » .NET » SQL Server »

How to bind two tables into dataset?


Posted Date: 29 Apr 2012      Posted By:: naresh     Member Level: Silver    Member Rank: 602     Points: 1   Responses: 5



how to bind two tables into dataset?

i need code for this and proper explanation




Responses

#668297    Author: S. Mohan      Member Level: Bronze      Member Rank: 2406     Date: 29/Apr/2012   Rating: 2 out of 52 out of 5     Points: 2

one approach would be to just make a union between the two
tables in the SQL statement (This requires that both resultsets are in
the same format).

select id, name, address TABLE1 UNION select id, name, address from
TABLE2

http://www.w3schools.com/sql/sql_union.asp


 
#668324    Author: Ravindran        Member Level: Diamond      Member Rank: 3     Date: 29/Apr/2012   Rating: 2 out of 52 out of 5     Points: 4

Refer below code sample to hold dataset two tables and bind in the gridview


using System.Data;
using System.Data.SqlClient;
public partial class test_two_dataset : System.Web.UI.Page
{
SqlConnection sqlcon = new SqlConnection(@"Server=SQLEXPRESS;database=test;uid=xxxx;pwd=yyyy;");
SqlCommand sqlcmd;
SqlDataAdapter da;
DataSet ds = new DataSet();

protected void Page_Load(object sender, EventArgs e)
{
sqlcon.Open();
sqlcmd = new SqlCommand("select * from emp", sqlcon); //first table query command
da = new SqlDataAdapter(sqlcmd);
da.Fill(ds, "emp"); //Bind first table in Dataset

sqlcmd = new SqlCommand("select * from empdata", sqlcon); //second table query command
da = new SqlDataAdapter(sqlcmd);
da.Fill(ds, "empdata"); //Bind second table in same Dataset



//first table bind in gridview1
GridView1.DataSource = ds.Tables["emp"];
GridView1.DataBind();

////first table bind in gridview2
GridView2.DataSource = ds.Tables["empdata"];
GridView2.DataBind();
}
}


Regards
N.Ravindran
Your Hard work never fails


 
#668343    Author: Anil Kumar Pandey      Member Level: Platinum      Member Rank: 1     Date: 29/Apr/2012   Rating: 2 out of 52 out of 5     Points: 3

check this sample.


SqlConnection con = new SqlConnection("Data Source=(local);Integrated Security=sspi");

SqlCommand qur = new SqlCommand("select * from employee", con);

SqlDataAdapter ada = new SqlDataAdapter();
ada.SelectCommand = qur;

DataSet ds = new DataSet();
ada.Fill(ds);


DataTable dt = new DataTable();
ada.Fill(dt);


Thanks & Regards
Anil Kumar Pandey
Microsoft MVP, DNS MVM





 
#668366    Author: Pawan Awasthi        Member Level: Diamond      Member Rank: 4     Date: 29/Apr/2012   Rating: 2 out of 52 out of 5     Points: 1

Hai Naresh,
There are various methods to use 2 tables in to the dataset:-
1. Create 2 data tables and add them in to the dataset. Create the relation between the tables using primary key and foreign key and then use them using code.
2. Create the relation between them in the SQL Server database and generate a single query. Use that query and load to the dataset.
Hope it will be helpful to you.

Regards,
Pawan Awasthi(DNS MVM)
+91 8143683708 (pawansoftit@gmail.com)
Outstanding Contribution Award..NTT Data Inc


 
#668501    Author: Sriram      Member Level: Gold      Member Rank: 895     Date: 30/Apr/2012   Rating: 2 out of 52 out of 5     Points: 3

SQlconnection con=new sqlconnection(str)
string sql="select * from emp";
sqlcommand cmd=new sqlcommand(sql,con);
sqldataadapter adp=new sqldataadapter(cmd);
dataset ds=new dataset();
adp.fill(ds);
gridview.Datasource=ds;
gridview.Databind();


sqlcommand cmd=new sqlcommand("select * from empsalary",con);
sqldataadapter adp=new sqldataadapter(cmd);
dataset ds=new dataset();
adp.fill(ds);
gridview.Datasource=ds;
gridview.Databind();



Hope this will help

Regards
Sriram.R


 
Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.



Next : Update Records in Different Tables
Previous : How do you know errors in database?
Return to Discussion Forum
Post New Message
Category:

Related Messages

Active Members
TodayLast 7 Daysmore...

Awards & Gifts
Talk to Webmaster Tony John
Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
2005 - 2013 All Rights Reserved.
.NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
Articles, tutorials and all other content offered here is for educational purpose only.
We are not associated with Microsoft or its partners.