Merging two Datasets into a single Datagrid

Posted by Kalyani under ASP.NET on 12/1/2011 | Points: 10 | Views : 2907 | Status : [Member] | Replies : 5
Hi All,
am getting error while merging 2 dataset into 1datset.
for example: ds having 3 colums and ds1 having 2 columns . i want 5 columns in 1dataset. i wrote code


string strConn = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter dAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
cmd.Connection = con;
dAdapter = new SqlDataAdapter("exec SIP_RTI_REVENUE", con);
ds = new DataSet();
dAdapter.Fill(ds);
dAdapter = new SqlDataAdapter("Exec SIP_RTI_GetProjectRole" , con);
ds1 = new DataSet();
dAdapter.Fill(ds1);//ds.Merge(ds1);this is not working

ds.Merge(ds1,true ,MissingSchemaAction.Ignore);//this is not adding both the tables
// ds.Relations.Add(ds.Tables[0], ds.Tables["1"]);

GridView1.DataSource = ds;
GridView1.DataBind();




Responses

Posted by: Hemanthlaxmi on: 12/1/2011 [Member] Starter | Points: 25

Up
0
Down
Instead of using the two select statement use one select statement like:

Select s.Id, name, course, tam, eng, mat from

Studentinfo s

join studentdetails sd on sd.Id = s.Id.

sada = new SqlDataAdapter("
Select s.Id, name, course, tam, eng, mat from StudentInfo s join studentdetails sd on sd.Id = s.Id.", scon);
sada.Fill(ds1, "table");
Gridview1.Datasource = ds1;

Gridview1.DataBind();

Hope this helps.

If this helps you .
Please "Mark as Answer"

Kalyani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kalyani on: 12/1/2011 [Member] Starter | Points: 25

Up
0
Down
Hi laxmi,

thanks for ur replay. but am passing 2 diff sp's


Thanks
kalyani

Kalyani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Varung on: 12/4/2011 [Member] Starter | Points: 25

Up
0
Down
ds.Merge(ds,true);
try this

G.Varun

Kalyani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Kalyani on: 12/9/2011 [Member] Starter | Points: 25

Up
0
Down
ds.Merge(ds,true);
this will give only first ds . it's not merge the ds1,ds

if i have ds columns t1,t2,t3
ds1 columns k1,k2,k3

if i mearge ds,ds1

i want t1,t2,t3,k1,k2,k3 columns . if u have any method s please let me know

thanks
kalyani




Kalyani, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sksingh on: 12/9/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try below code

string strConn = ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString; 

SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
SqlDataAdapter dAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
DataSet ds1 = new DataSet();
cmd.Connection = con;
dAdapter = new SqlDataAdapter("exec SIP_RTI_REVENUE", con);
ds = new DataSet();
dAdapter.Fill(ds,"Mytable"); dAdapter = new SqlDataAdapter("Exec SIP_RTI_GetProjectRole" , con);
ds1 = new DataSet();
dAdapter.Fill(ds1,"Mytable");// ds.Merge(ds1);this is not working

ds.Merge(ds1,true ,MissingSchemaAction.Ignore);//this is not adding both the tables
// ds.Relations.Add(ds.Tables[0], ds.Tables["1"]);

GridView1.DataSource = ds.Tables[0].DefaultView ;
GridView1.DataBind();


See changes done in your existing code marked as underline.

Now this code works because the table name for both datasets are the same.


Regards,
Sunil

Kalyani, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response