473,719 Members | 2,914 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Column does not belong to table

6 New Member
Hey

Guyz I am having problem when i read the data from a datareader into a data table.

The whole scenario is this that I want to read data from two diffrent databases, the table strcutre is same. You can say that other one is the copy of 1st one. I want to read out the data from one table in one database and merge the results with the one read form the other database.

What i am doing is reading data from two databases into data readers. now i just have to merge them. what i did took one datatable and want to merge the data in two datareader object into that data table. frst one went ok. but when i was inserting the rows from the 2nd one it gave this error.
see the code bellow and suggest plz:

Expand|Select|Wrap|Line Numbers
  1. public static DataTable PopupateDataTableFromDataReader(DataTable dt, clsDataReader dr)
  2.         {
  3.  
  4.             DataTable dtSchema = dr.dr.GetSchemaTable();
  5.  
  6.             // You can also use an ArrayList instead of List<>
  7.             ArrayList listCols = new ArrayList();
  8.  
  9.             if (dtSchema != null)
  10.             {
  11.                 foreach (DataRow drow in dtSchema.Rows)
  12.                 {
  13.                     string columnName = System.Convert.ToString(drow["ColumnName"]);
  14.                     DataColumn column = new DataColumn(columnName, (Type)(drow["DataType"]));
  15.                     column.Unique = (bool)drow["IsUnique"];
  16.                     column.AllowDBNull = (bool)drow["AllowDBNull"];
  17.                     column.AutoIncrement = (bool)drow["IsAutoIncrement"];
  18.                     listCols.Add(column);
  19.                     if(dt.Columns.IndexOf(column.ColumnName) == -1)
  20.                     {
  21.                         dt.Columns.Add(column);
  22.                     }
  23.                 }
  24.             }
  25.  
  26.             // Read rows from DataReader and populate the DataTable
  27.             while (dr.dr.Read())
  28.             {
  29.                 DataRow dataRow = dt.NewRow();
  30.                 for (int i = 0; i < listCols.Count; i++)
  31.                 {
  32.                     dataRow[((DataColumn)listCols[i])] = dr.dr[i];
  33.                 }
  34.                 dt.Rows.Add(dataRow);
  35.             }
  36.             return dt;
  37.         }    
  38.  
  39.         public static clsDataReader  executeReturn2(string sqlCommand)
  40.         {
  41.             try
  42.             {
  43.  
  44.                 SqlConnection   co =initConnection(2);
  45.                 clsDataReader dataReader=new clsDataReader();
  46.                 SqlCommand   myCommand = new SqlCommand(sqlCommand, co);
  47.                 SqlDataReader dr= myCommand.ExecuteReader();
  48.                 dataReader.co =co ;
  49.                 dataReader.dr =dr;
  50.  
  51.                 return dataReader;
  52.             }
  53.             catch(Exception e)
  54.             {
  55.                 throw new Exception("An error occured while trying to run the following: " + sqlCommand,e); 
  56.             }
  57.         }
  58.  
Jun 26 '09 #1
1 12272
PRR
750 Recognized Expert Contributor
If you wish to merge two similar tables then DataSet.Merge() is far easier to use. You have to make sure that the primary key column exists though.

Merging Datasets

To give a brief idea consider a table Emp1: with columns empid, empname and second table Emp2: with columns empid, empjoindate.
You could have data of Emp1 in dataset1 and Emp2 in dataset2.
Expand|Select|Wrap|Line Numbers
  1. select empid,empname from Emp1;
  2. select empid,empjiondate from Emp2;
  3.  
You could easily merge the dataset by calling
Expand|Select|Wrap|Line Numbers
  1. dataset1.Merge(dataset2.Tables[0]);
  2.  
Jun 26 '09 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

0
1540
by: mkomasi | last post by:
I am using a datagrid in my form sometimes when a child form of this datagrid form closed an exception like "Column C does not belong to the table T" was thrown. this exception is rarely occured so rarely and I can not find the way that it was generated, so I can not debug the the exception. does anybody know how to generate this exception and how to handle it.
7
1695
by: Patrick Olurotimi Ige | last post by:
I have a simple Stored Procedure with multiple select statements..doing select * from links for example. I created a DataTable and then fill the tables But the first dtTemplate DataTable doesn't give the error but the links does! I get the error on this LINE(when looping):- PageLinks.Text = PageLinks.Text & dtLinks.Rows(iLoop)("link_url")
5
8489
by: Kejpa | last post by:
Hi, How do you write a Rowfilter for a boolean column? col1=true col1='true' col1= 1 col1= -1 Will all render an exception: Column 'col1' does not belong to table Test. Any thoughts?
10
2343
by: Nick | last post by:
Hello, Please pardon my ignorance as I'm sure this is easy to do. I have a datagrid where I want to let the user delete columns. I added a context menu to the datagrid that has a delete option. If the user right clicks on a column heading can I highlight that column and then delete it? I'm not exactly sure how to highlight it or figure out what column has been clicked. In my testing so far when I use the context menu I always get the...
6
35570
by: Mike S | last post by:
Hi all, A (possibly dumb) question, but I've had no luck finding a definitive answer to it. Suppose I have two tables, Employees and Employers, which both have a column named "Id": Employees -Id -FirstName -LastName
0
1602
by: Mike | last post by:
We are using .NET 2.0 and intermittently egt the following errors on almost all our web pages. The error is not repoducable and cycling the worker process seems to temporarily fix the problems. Has anyone seen this or have any suggestions? Event code: 3005 Event message: An unhandled exception has occurred. Event time: 9/5/2006 4:23:47 PM Event time (UTC): 9/5/2006 11:23:47 PM Event ID: cb408727296440278fb486579b359b15
4
15285
by: Mike | last post by:
We are using .NET 2.0 and intermittently egt the following errors on almost all our web pages. The error is not repoducable and cycling the worker process seems to temporarily fix the problems. Has anyone seen this or have any suggestions? Event code: 3005 Event message: An unhandled exception has occurred. Event time: 9/5/2006 4:23:47 PM Event time (UTC): 9/5/2006 11:23:47 PM Event ID: cb408727296440278fb486579b359b15
1
2504
by: shil | last post by:
Hi all, updating SQL database with the data.I'm stuck with an issue. One of the XML file from the list don't have a node mentioned. For example almost all XML files structure is like this <treatment_settings> <setup> <product1> <name>PSO-NO2</name>
2
5656
by: Ronald S. Cook | last post by:
In the code below, I get a runtime error where indicated. The error is: "Column 'TICKETNO' does not belong to table TICKET_DEDUCTIONS." If I put a breakpoint on that line (so not yet executed) and type ?dedrow(c), It says basically the same thing: Run-time exception thrown : System.ArgumentException - Column 'TICKETNO' does not belong to table TICKET_DEDUCTIONS.
0
8833
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8727
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9210
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9126
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7998
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6661
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
1
3186
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2567
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2125
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.