Atif Imtiaz
Atif Imtiaz

Reputation: 225

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

Upvotes: 1

Views: 31769

Answers (4)

Arindam Rudra
Arindam Rudra

Reputation: 624

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 };

Upvotes: 1

Pandian
Pandian

Reputation: 9126

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"];
}

Upvotes: 0

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

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?

Upvotes: 2

Eren Ersönmez
Eren Ersönmez

Reputation: 39085

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"]);

Upvotes: 1

Related Questions