Clone DataTable from One Dataset to DataTable of Other DataSet - c#

I am trying to clone Datable from one dataset to datatable from other Dataset, but it does not work.
Dataset ds = new Dataset();
ds.Tables.Add("key");
ds.Table["key"] = cust.Tables[0].Clone(); // This line is not working
Above code gives below error
Property or indexer 'System.Data.DataTableCollection.this[int]' cannot be assigned to — it's read only
cust is also a Dataset. Can someone help me resolve this

The error you are getting is similar to this:
Property or indexer 'System.Data.DataTableCollection.this[int]' cannot
be assigned to -- it is read only
You are doing it right once, and then you are doing it wrong in the line after.
The error is telling you, that cust.Tables[0] is read only. You cannot assign any values to it, so you have to use the Add() method:
DataSet cust = new DataSet(); // create source DataSet
cust.Tables.Add("sourceTable"); //add table to source
DataSet ds = new DataSet(); // create target DataSet
ds.Tables.Add((cust.Tables[0]).Clone()); //clone source table
(ds.Tables[0]).TableName = "keys"; // assign name
You need to wrap ds.Tables[indexer] in paranthesis () so the compiler treats it as a datatable.
You should assign a DataTable variable to keep the readability:
var sourceTable = cust.Tables[0];
/* ... */
ds.Tables.Add(sourceTable.Clone());

Related

How to use a SSIS Dataset inside Dataflow Script component for Source?

I'm looking for a way to use my Dataset variable in Script component of SSIS data flow task, here is what I tried till now
My First Task is Execute SQL task (CData PostgreSQL Task)
This would run a Postgre query and store the result in a variable User::resultSet which is of the data type DataSet
My Second Task is Data Flow Task
In my second task I wish to use the dataset as my source. I tried to achieve this by using Script Component (Source)
My Script
public override void CreateNewOutputRows()
{
DataTable dt = (DataSet)Variables.resultSet;
// The DataTable is now ready to use! No more recordset quirks.
foreach (DataRow dr in dt.Rows)
{
// Add a new output row for this transformed data.
OrdersBuffer.AddRow();
// Now populate the columns
OrdersBuffer.id = int.Parse(dr["id"].ToString());
OrdersBuffer.created = int.Parse(dr["created"].ToString());
OrdersBuffer.modified = int.Parse(dr["modified"].ToString());
}
}
Issue
In my Script, I cant figure out a way to convert the dataset into a datatable,
I believe the problem line is DataTable dt = (DataSet)Variables.resultSet; in my script
I also tried DataTable dt = resultSet.Tables[0]; & DataTable dt = resultSet.Table[0]; but all them are throwing syntax error.
Any lead or guidance will be really appreciated.
Try this one
DataTable dt = (Variables.resultSet as DataSet).Tables[0];
May throw an exception if type cast is not possible.
I have not used the dataset variable type but I have dont this with an OBJECT type variable and MS SQL DB. If you can change to use object variable this should work for you (or maybe if you cant you can see how I am passing the OBJECT variable and update your code to access the variable in similar way.
First I run a SQL task that has a result set as output in SSIS to my OBJECT variable data type. Then I pass it to my script task like this:
Inside the C# Script task I have this below code to access the OBJECT variable and then covert it to a data table that I use later on
Where this variable is my object variable type in the SSIS Package:
User::ObjDataToSaveToExportFile
// this gets the data object and sets ti to a data table
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::ObjDataToSaveToExportFile"].Value);
// for test data
//DataTable sourceTable = GetTestData();
DataTable sourceTable = dt;
You can use below approach of using DataAdapter to fill datatable. Reference Article
public override void CreateNewOutputRows()
{
// Set up the DataAdapter to extract the data, and the DataTable object to capture those results
OleDbDataAdapter da = new OleDbDataAdapter();
DataTable dt = new DataTable();
// Extract the data from the object variable into the table
da.Fill(dt, Variables.resultSet);
// Since we know the column metadata at design time, we simply need to iterate over each row in
// the DataTable, creating a new row in our Data Flow buffer for each
foreach (DataRow dr in dt.Rows)
{
// Add a new output row for this transformed data.
OrdersBuffer.AddRow();
// Now populate the columns
OrdersBuffer.id = int.Parse(dr["id"].ToString());
OrdersBuffer.created = int.Parse(dr["created"].ToString());
OrdersBuffer.modified = int.Parse(dr["modified"].ToString());
}

How to access Object SSIS variable inside script task?

I have SSIS variable of type object which holds the OLE DB result set from OLE DB source.
The object is list of blogs with each blog having attributes title and description.
How can I parse the SSIS object variable and iterate over each blog and then field?
EDIT :
I want to read full result set in script task.
// this gets the data object and sets ti to a data table
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, Dts.Variables["User::SSISObjectVariableFromPackage"].Value);
// for test data
DataTable sourceTable = dt;
//Now loop through databale and do your processing
//----------------------------------------------------------------------
//Updated -- possible solution to fix issue in comments but left original above as that works for most
//try this version instead, mabye converting the SSIS variable to a C# object first, and not accessing it directly into the A.Fill may resolve your issue in the comments:
Object OBJDataTableValidFieldListFull = Dts.Variables["User::SSISObjectVariableFromPackage"].Value;
// this gets the data object and sets ti to a data table
OleDbDataAdapter A = new OleDbDataAdapter();
System.Data.DataTable dt = new System.Data.DataTable();
A.Fill(dt, OBJDataTableValidFieldListFull);
// for test data
DataTable sourceTable = dt;
//Now loop through databale and do your processing

"DataTable already belongs to another DataSet"

I can't figure out why I am getting a "DataTable already belongs to another DataSet" exception.
Here is my code:
DataSet AlertSet = new DataSet();
DataTable generalAlertData = new DataTable("GeneralAlertData");
generalAlertData = //function that returns datatable
//throws exception
AlertSet.Tables.Add(generalAlertData)
When is another dataset being created and when is the generalAlertData datatable being added to it?
I tried using a slightly different syntax to create and add the table and got the same error:
DataSet AlertSet = new DataSet();
DataTable generalAlertData = //function that returns datatable
//throws exception
AlertSet .Tables.Add(generalAlertData);
It depends on what your function that retrieve the datatable does.
If that function use a DataAdapter and fills a dataset then you have the DataSet property of the table automatically assigned and you need to remove it before assigning a different DataSet
You could try this to remove the original DataSet and use your own
DataTable generalAlertData = GetTable();
DataSet u = generalAlertData.DataSet;
u.Tables.Remove(generalAlertData.TableName);
AlertSet.Tables.Add(generalAlertData);
So, if your hypothetical GetTable works in this way
public DataTable GetTable()
{
...
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
return ds.Tables[0];
}
then you have the DataSet assigned to the returned datatable.
Instead this code doesn't assign anything to the DataSet property
public DataTable GetTable()
{
...
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
return dt;
}
The DataTable returned from the method call, was added to a DataSet. Make sure it's not added to a DataSet; just build a stand alone DataTable.
As a side-note, returning a DataTable from a method call that assigns to this one makes the line above it irrelevant (the new).
This is an old question now but maybe this answer will help others.
I too received this error. I looked through my source code, found literally no other datasets and was puzzled as to why this was happening.
The solution to my issue was in my data access library. It didn't have a "getDataTable" routine but I did have a getDataset routine. When I needed just a datatable I called getDataset and took the tables(0) datatable instance.
The end result of this is that I got back my datatable like I wanted, but the datatable was already part of the dataset returned from my data access call. Thus, the error.
I had to add a getDatatable routine into my data access library to eliminate this problem.
I had the same problem and I solved it using Remove. In my opinion, your code could be this:
DataSet AlertSet = new DataSet();
DataTable generalAlertData = new DataTable("GeneralAlertData");
AlertSet.Tables.Add(generalAlertData);
AlertSet.Tables.Remove(generalAlertData);
Please, let me know if this code works.
My working code was like this:
DataTable myTable = new DataTable();
private void Save()
{
DataSet myDataSet = new DataSet();
myDataSet.Tables.Add(myTable);
myDataSet.Tables.Remove(myTable);//This works
myDataSet.WriteXml("myTable.xml");
}
private void buttonSave_Click(object sender, EventArgs e)
{
Save();
}
Every time I clicked the button buttonSave, the message “DataTable already belongs to another DataSet" appeared. After writing the line code myDataSet.Tables.Remove(myTable);//This works the application started running without problems and now I can click the button more times, without losing the value of myTable and without the error message.
I hope this can help.

Cannot implicitly convert type 'System.Data.DataTable' to DataSets.General.StudentDataTable'

I have a dataset containing some datatables, now i want to fetch a datatable from db and add it to the existing datatable in dataset i am using below line
return (DataSets.General.StudentDataTable) ds.Tables["DSDummy"];
But its giving me following error
Cannot implicitly convert type 'System.Data.DataTable' to
DataSets.General.StudentDataTable'. An explicit conversion exists
can someone please tell me how to cast this object? any help would be appreciated
Thanks
If ds is a strongly typed dataset, I would expect it to have an explicit property for the DSDummy table, something like ds.DSDummy, rather than going through the Tables collection, which bypasses the strong typing.
However, even as it stands, the explicit cast should have worked. Is it possible that you have DataSets.General.StudentDataTable defined more than once - once manually and once automatically from the DataSet Generator - and it's conflicting?
If you're trying to load one DataTable into another one, you can use the DataTable.Merge method:
DataSets.General.StudentDataTable.Merge(ds.Tables["DSDummy"]);
Check out the MSDN documentation
Below code was taken from the above link.
DataSet customerOrders = new DataSet("CustomerOrders");
DataTable ordersTable = customerOrders.Tables.Add("Orders");
DataColumn pkOrderID =
ordersTable.Columns.Add("OrderID", typeof(Int32));
ordersTable.Columns.Add("OrderQuantity", typeof(Int32));
ordersTable.Columns.Add("CompanyName", typeof(string));
ordersTable.PrimaryKey = new DataColumn[] { pkOrderID };
Try like below... may be it help you...
If you want to return dataset and assign to a Dataset then follow the below method..
DataSet ds = new DataSet();
ds = GetData();
public dataset Sample GetData()
{
......
.......
return ds
}
If you want to return Datatable and assign that to Dataset then follow the below method.
DataSet ds = new DataSet();
ds.Tables.Add(GetData());
public datatable Sample GetData()
{
......
.......
return ds.Tables["DSDummy"];
}

Extracting Data From DataGridView and Populating in XML

what i am trying to do is write a sepprate function that is in the business logic of my Windows form application. this function recieves the xml file path and the datagridview object as arguments.
This is supposed to work:
DataTable dt = new DataTable();
dt = (DataTable)datagridview.DataSource;
DataSet ds = new DataSet();
ds.Tables.Add(dt);
ds.WriteXml(xml_file, System.Data.XmlWriteMode.IgnoreSchema);
But i have a error everytime with the line:
dt = (DataTable)datagridview.DataSource;
[System.InvalidCastException] = {"Unable to cast object of type 'System.Windows.Forms.BindingSource' to type 'System.Data.DataTable'."}
so i was advised in another blog to try this:
BindingSource bs = (BindingSource)dgv.DataSource;
dt = (DataTable)bs.DataSource;
but i get
[System.InvalidCastException] = {"Unable to cast object of type 'ExportDataTestApp.NorthwindDataSet' to type 'System.Data.DataTable'."}
i have searched and tryed everything i would not be supprised if it is something simple as i am new to c# but i need help please
You're almost there.
BindingSource bs = (BindingSource)dgv.DataSource;
DataSet ds = (DataSet)bs.DataSource;
DataTable dt = ds.Tables["Customers"]; // or Tables[0]
Apparently you are binding with DataSourse=myDataSet, DataMember="Tablename"

Categories

Resources