none
Strongly Typed DataSet casting problem RRS feed

  • Question

  • I'm going crazy!  What am I doing wrong below?  The cast is occuring, but I cannot print/access it.  The relevant text is in blue and red below.  Note the strongly typed DataSet works fine, and from Debug mode it seems that cast is working, but nothing in the print stage (and I tried various things besides the below).  As an example, I also include a traditional weakly cast DataSet, which works fine.  It's the first time I've tried to cast a strongly typed DataSet into a weakly typed (untyped) DataSet, so I'm sure it's some sort of trivial syntax problem, but what?

    ds = strongly typed DataSet, using the .xsd schema.

    RC


    private void button1_Click(object sender, EventArgs e)

            {

               

                string str = Properties.Settings.Default.Chap2DbConnectionString.ToString();

                string sqlQuery = "SELECT [Product].ID, [Product].ProductName, [Product].ProductUnitPrice FROM dbo.Product";

     

                SqlConnection cnn = new SqlConnection(str);

     

     

                SqlCommand cmd = new SqlCommand(sqlQuery, cnn);

                SqlDataAdapter da = new SqlDataAdapter(cmd);

     

                AStronglyTypedDataSet ds = new AStronglyTypedDataSet();

     

              //  AStronglyTypedDataSet.Product1DataTable tblProduct1 = ds.Product1;

     

                AStronglyTypedDataSet.ProductDataTable tblProduct_ = ds.Product;

     

                DataSet myDS = new DataSet();

     

                da.Fill(myDS);

              

              

     

                AStronglyTypedDataSetTableAdapters.ProductTableAdapter daProduct_Table;

                //create the TableAdapter

     

                daProduct_Table = new WdB1.AStronglyTypedDataSetTableAdapters.ProductTableAdapter();

     

                //fill the .xsd ds

              // daProduct1Table.FillOne(tblProduct1); //note .FillOne is the user defined term

                daProduct_Table.FillOne(tblProduct_);

     

               DataSet dsUntyped;

                dsUntyped = new DataSet();

               dsUntyped = (DataSet)ds;

     

               DataTable tblProduct_UNTYPED;

               tblProduct_UNTYPED = dsUntyped.Tables[0];

     

             

               //////////

     

                AStronglyTypedDataSet.ProductDataTable tblProduct = ds.Product;

     

     

                foreach (DataRow drow in tblProduct_.Rows)

                {

                    Console.WriteLine(" .xsd ver!-> {0}, {1}", drow[0], drow[1]);

                }

     

    //NOTHING!

                foreach (DataRow drow in tblProduct_UNTYPED.Rows)

                {

                    Console.WriteLine(" reg1 ver - {0}, {1}", drow[0], drow[1]);

                }

     

     

                foreach (DataRow drow in myDS.Tables[0].Rows)

                {

                    Console.WriteLine(" reg2 ver - {0}, {1}", drow[0], drow[1]);

                }

     

     

     

            }

     

    //OUTPUT IS AS FROM TABLE, CORRECT

     

    .xsd ver!-> 1, first    

     .xsd ver!-> 2, second   

     .xsd ver!-> 3, third    

     .xsd ver!-> 4, fourth   

     .xsd ver!-> 5, fifth    

     .xsd ver!-> 6, sixth    

     reg2 ver - 1, first    

     reg2 ver - 2, second   

     reg2 ver - 3, third    

     reg2 ver - 4, fourth   

     reg2 ver - 5, fifth    

     reg2 ver - 6, sixth    

    Friday, April 10, 2009 8:55 PM

Answers

  • OK, I figured it out just now, kind of, by casting to a table, but I'd still like to know if possible why a cast to a DataSet does not work.

    Here is my workaround:

    DataTable myTABLE = new DataTable();

    myTABLE = (DataTable) ds.Product;  //product is the name of the Table in the DataSet ds

    //now you can print out easily as follows:

    foreach (DataRow drow in myTABLE.Rows) Console.WriteLine(" reg1TABLE ver - {0}, {1}", drow[0], drow[1]);}

    //RC

    • Marked as answer by RonConger09 Sunday, April 12, 2009 9:07 PM
    Friday, April 10, 2009 9:05 PM

All replies

  • OK, I figured it out just now, kind of, by casting to a table, but I'd still like to know if possible why a cast to a DataSet does not work.

    Here is my workaround:

    DataTable myTABLE = new DataTable();

    myTABLE = (DataTable) ds.Product;  //product is the name of the Table in the DataSet ds

    //now you can print out easily as follows:

    foreach (DataRow drow in myTABLE.Rows) Console.WriteLine(" reg1TABLE ver - {0}, {1}", drow[0], drow[1]);}

    //RC

    • Marked as answer by RonConger09 Sunday, April 12, 2009 9:07 PM
    Friday, April 10, 2009 9:05 PM
  • Ron,

    The only thing I can think of why your original code didn't work is that maybe ds.Tables[0] was not actually the same as ds.Product. IOW, you had another table in Tables[0]. So that when you used this code:


    DataSet dsUntyped;

    dsUntyped = new DataSet();

    dsUntyped = (DataSet)ds;

     

    DataTable tblProduct_UNTYPED;

    tblProduct_UNTYPED = dsUntyped.Tables[0];


    ... you weren't actually getting the ds.Product table, which you thought you should be getting. You didn't show your .xsd, so I can't be certain of that ... but it sure looks  like that's what's going on.
    ~~Bonnie Berent [C# MVP]
    Sunday, April 19, 2009 4:35 AM