473,386 Members | 1,785 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Column does not belong to table

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 12062
PRR
750 Expert 512MB
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
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...
7
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...
5
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
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....
6
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...
0
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. ...
4
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. ...
1
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 ...
2
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)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
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...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.