none
Unable to cast object of type 'System.Data.DataTable' to type 'myStronglyTypedDataTable'.

    Question

  • Hi

    i have a method which return DataTable.

    now i want to cast it to my stronglyTyped dataTable. to do this, i'm using this code :

    string cmdText = "SELECT * FROM Patients";
    DataTable dtResult = Houshmand.Classes.Utilities.DataManager.GetData(cmdText, criteria, Properties.Settings.Default.MedicalCenterConnectionString);
    if (dtResult != null && dtResult.Rows.Count > 0)
    {
    	MedicalCenterDataSet.PatientsDataTable patinetsDataTable = (MedicalCenterDataSet.PatientsDataTable)dtResult; // error occures in this line
           this._PatientRow = patinetsDataTable[0];
    }
    

    but, i'm facing this error :

    Unable to cast object of type 'System.Data.DataTable' to type 'PatientsDataTable'.

    How can i convert my dataTable to strongly typed dataTable ?

    thanks in advance


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    Sunday, March 27, 2011 9:45 AM

Answers

  • Thanks for reply

    I solved my problem via this code (like your code) :

    public static void ConvertDataTableToStronglyTypedDataTable(DataTable dtSource, DataTable dtTarget)
          {
            foreach (DataRow row in dtSource.Rows)
            {
              dtTarget.ImportRow(row);
            }
          }
    

    thanks for your attentions everybody.


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    • Marked as answer by Hamed_1983 Sunday, March 27, 2011 11:45 AM
    Sunday, March 27, 2011 11:45 AM

All replies

  • Create a DataView. Like this:

    DataView newTable = new DataView(dtResult);

    To use in (or pass the "newTable" variable you can do:

    void Method()
    {
       DataView newTable = new DataView(dtResult);
       UseNewTable(newTable.Table);
    }
    
    
    private void UseNewTable(DataTable table)
    {
       //use table as normaly here
       //for example:
       foreach(DataRow dr in table.Rows)
       {
         //code to populate controls or what ever
       }
    }

    Hope it helps,

    Mitja


    Sunday, March 27, 2011 9:55 AM
  • See if there is an overload of your GetData method that allows you to pass the target DataSet/DataTable as a parmeter. This would use the standard DataAdapter.Fill method to put the data directly into the Typed Dataset.


    Recipient of the Microsoft Community Contributor Award 2011.
    Sunday, March 27, 2011 10:14 AM
  • See if there is an overload of your GetData method that allows you to pass the target DataSet/DataTable as a parmeter. This would use the standard DataAdapter.Fill method to put the data directly into the Typed Dataset.


    Recipient of the Microsoft Community Contributor Award 2011.

    Thanks for reply

    but i can't do it, because i have a lot of strongly typed dataTable, create overload for each dataTable is hard work.


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    Sunday, March 27, 2011 10:20 AM
  • The overload can still be of type DataTable, you can pass your typed datatable instance to this method:

    MyTypedDataTable dt = new MyTypedDataTable();

    DataManager.FillData(/* DataTable */ dt, criteria, connectionstring);

    You don't need an overload because every TypeDataTable is also of type DataTable though inheritance. The problem you're running into at the moment is that DataTable isn't also a TypeDataTable (the other way around) and thus cannot be cast of converted as you would expect.


    Recipient of the Microsoft Community Contributor Award 2011.
    Sunday, March 27, 2011 10:41 AM
  • Thanks for reply

    I solved my problem via this code (like your code) :

    public static void ConvertDataTableToStronglyTypedDataTable(DataTable dtSource, DataTable dtTarget)
          {
            foreach (DataRow row in dtSource.Rows)
            {
              dtTarget.ImportRow(row);
            }
          }
    

    thanks for your attentions everybody.


    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    • Marked as answer by Hamed_1983 Sunday, March 27, 2011 11:45 AM
    Sunday, March 27, 2011 11:45 AM
  • Please note that the solution you've chosen is a very expensive operation, compared to passing a reference to the correct DataTable instance to the GetData method. Import will check if the schema matches for each row being imported and will also add rows or update the schema if needed. This might lead to unwanted behavior later on
    Recipient of the Microsoft Community Contributor Award 2011.
    Sunday, March 27, 2011 2:00 PM
  • Thanks for your attentions
    http://www.codeproject.com/KB/codegen/DatabaseHelper.aspx
    Sunday, March 27, 2011 2:15 PM