Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 1.0 and 1.1 > ASP.NET 1.1
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 19th, 2005, 05:04 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default 2 datatables in a single datagrid

Dear All,
i am trying to display 2 datatables in a single datagrid with the following code:

private void Page_Load(object sender, System.EventArgs e)
{
// Filling DataSets
      SqlConnection myconn=new SqlConnection(connstr);
      SqlDataAdapter da1=new SqlDataAdapter("select * from emp_Pers", myconn);
      SqlDataAdapter da2=new SqlDataAdapter("select * from emp_Off", myconn);

    DataSet ds1=new DataSet();
    DataSet ds2=new DataSet();

    da1.Fill(ds1, "emp_Pers");
    da2.Fill(ds1, "emp_Pers");

// Setting Primary Keys
   DataColumn[] pk1 = new DataColumn[] {ds1.Tables[0].Columns ["EmployeeID"]};
   ds1.Tables[0].PrimaryKey = pk1 ;

   DataColumn[] pk2 = new DataColumn[] {ds2.Tables[0].Columns["EmployeeID"]};
   ds2.Tables[0].PrimaryKey = pk2 ;

// Merging DataSets
    ds1.Merge(ds2, false, MissingSchemaAction.Add);

// Binding to DataGrid
    dgrid.DataSource =ds1;
    dgrid.DataBind();

}

i am getting runtime error as :

"System.ArgumentException: These columns don't currently have unique values" at the following line:
   ds1.Tables[0].PrimaryKey = pk1 ;

plz help out.

Regards,
Muskaan..




 
Old May 19th, 2005, 05:46 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

A. You can't bind a datagrid to more than one datatable (or specifically the DefaultView of a table). When you bind a dataset, the grid binds to the DefaultView of the first table in .Tables.

B. Look at the error you are getting. Your query is returning multiple rows with the same EmployeeID so ADO.NET can't make that column a primary key. You don't have to have a primary key on the datatable to bind it to a grid. However, it seems that you want to bind multiple tables so I'd guess that you are trying to do 1 of two things:

  1. Get related data from the second table (you'll need to do some complex binding)
  2. Perform a union of data. Instead of trying to merge data in .NET you could use a union query to lump it all together at the database level.

-Peter
 
Old May 19th, 2005, 06:00 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks Peter, i'll try it out.

Regards,
Muskaan.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Need Help With DataTables visanza Beginning VB 6 1 January 9th, 2007 01:30 PM
How to merge two datatables????????? avats ADO.NET 3 March 21st, 2006 08:40 AM
Displaying 2 datatables in 1 datagrid muskaanbajaj Classic ASP Databases 5 May 19th, 2005 11:36 AM
ASP.Net Single Single-on with Oracle Application S guhanath Oracle 0 October 6th, 2004 05:05 AM
2 x DataTables and Datagrid Vince ADO.NET 0 September 22nd, 2003 08:57 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.