none
Retrieving data from a datatable RRS feed

  • Question

  • Hi.

    I am new to windows programming, so I apologize if this is a stupid question. I have tried looking for an answer on the forums and haven't found it. My problem is that I have a datatable, and I am having problems retrieving data. I have a column in my table that I want to store a true or false value, so naturally I set it to Boolean. I have tried retrieving the data in it like so:

    Boolean ^b1 = DataTable1->Rows->default[0]->default[1];

    I think that this should copy the boolean value in row 0, column 1 of DataTable1 to b1. But the compiler keeps telling me that it can't convert "System::Object ^" to "System::Boolean ^". So this would imply that the value in the table actually isn't a boolean at all. It is an object. Why is that? What method am I suppose to use to retrieve my boolean data? Thanks for any help.

    Saturday, July 14, 2012 11:18 PM

Answers

  • All values in non-typed DataTables are stored as objects, that's why you can store any value to it. If you know it is boolean then you can use Convert.ToBoolean method or cast to convert the value to boolean.

    If you are not sure whether or not it is boolean, DataColumn.DataType property tells what kind of an type is stored, or you can check the objects type if it's boolean type and then safery cast or convert the returned object instance to boolean, or alternatively get the string representation of the object with ToString method and use Boolean.TryCast to try to convert the string value to boolean.


    Sunday, July 15, 2012 7:32 AM