Connecting Tech Pros Worldwide Forums | Help | Site Map

Column does not belong to table

Newbie
 
Join Date: Jun 2009
Location: Karachi Pakistan
Posts: 2
#1: Jun 26 '09
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.  

PRR PRR is offline
Moderator
 
Join Date: Dec 2007
Location: India
Posts: 702
#2: Jun 26 '09

re: Column does not belong to table


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.  
Reply

Tags
datareader, datareader and datatable, datatable, reading data in datatable