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
- public static DataTable PopupateDataTableFromDataReader(DataTable dt, clsDataReader dr)
- {
- DataTable dtSchema = dr.dr.GetSchemaTable();
- // You can also use an ArrayList instead of List<>
- ArrayList listCols = new ArrayList();
- if (dtSchema != null)
- {
- foreach (DataRow drow in dtSchema.Rows)
- {
- string columnName = System.Convert.ToString(drow["ColumnName"]);
- DataColumn column = new DataColumn(columnName, (Type)(drow["DataType"]));
- column.Unique = (bool)drow["IsUnique"];
- column.AllowDBNull = (bool)drow["AllowDBNull"];
- column.AutoIncrement = (bool)drow["IsAutoIncrement"];
- listCols.Add(column);
- if(dt.Columns.IndexOf(column.ColumnName) == -1)
- {
- dt.Columns.Add(column);
- }
- }
- }
- // Read rows from DataReader and populate the DataTable
- while (dr.dr.Read())
- {
- DataRow dataRow = dt.NewRow();
- for (int i = 0; i < listCols.Count; i++)
- {
- dataRow[((DataColumn)listCols[i])] = dr.dr[i];
- }
- dt.Rows.Add(dataRow);
- }
- return dt;
- }
- public static clsDataReader executeReturn2(string sqlCommand)
- {
- try
- {
- SqlConnection co =initConnection(2);
- clsDataReader dataReader=new clsDataReader();
- SqlCommand myCommand = new SqlCommand(sqlCommand, co);
- SqlDataReader dr= myCommand.ExecuteReader();
- dataReader.co =co ;
- dataReader.dr =dr;
- return dataReader;
- }
- catch(Exception e)
- {
- throw new Exception("An error occured while trying to run the following: " + sqlCommand,e);
- }
- }