T
Thomas H
Hey guys, got a question! I've got a DataSet/DataTable, created by using a
DataAdapter.Fill call. I'm writing a C# class that handles all the ADO.NET
work I need, and a different class will call the public methods of this
class.
So; I've created the two public fields I need- Public String _custId and
Public DateTime _orderDate. But, I can't get the values out of the
DataSet/DataTable/DataColumn without doing (in my mind) useless casting! For
instance, this doesn't work:
_custID = dsOrders.Tables["tblOrd"].Rows[iCurrentRow][0];
I get the message "Cannot implicitly convert type 'object' to 'string' ". To
make it work, I have to do:
_custID = (String)dsOrders.Tables["tblOrd"].Rows[iCurrentRow][0];
.....or....
_custID = dsOrders.Tables["tblOrd"].Rows[iCurrentRow][0].ToString();
I'm confused as to why? The following line displays "System.String":
Console.WriteLine(dsOrders.Tables["tblOrd"].Rows[iCurrentRow].Table.Columns[
0].DataType);
So, shouldn't that mean that I don't have to do anything else? Why do I need
to use (String) or .ToString() ?
And I also can't get the DateTime into a variable. Trying
_orderDate = dsHousing.Tables["tblOrd"].Rows[iCurrentRow][1];
gives me "Cannot implicitly convert type 'object' to 'System.DateTime' ".
And, again, if I display the value for Columns[1].DataType, it shows me
"System.DateTime". Worse, there's no ".ToDateTime()" method of the Column
object, so the only way I can get the DateTime field out is to cast it to a
string, and then convert it back to a DateTime. Compared to my problem with
the String column, I'm doing twice the un-necessary work just to get the
DateTime column!
Is this really what's supposed to happen? If the DataType shows string, why
do I need to perform an explicit conversion from object to string? It seems
like I'm doing un-necessary work; if the value is stored as a certain
datatype, I should be able to pull it out of the DataTable and put it into a
variable of the same type.
I even toyed with the idea of .NET assuming I wanted the DataColumn being
put into a String- but I can't find a property of DataColumn that just gives
me the value. In classic ADO, there was a Field object, which had a Value
property. I can understand "DataColumn" being an object, but is there a
specific property for pulling the value out of DataColumn? For instance, if
those code samples I just gave would be in classic ADO, I would be trying to
say _CustID = rs.Fields(x).Value.
Oh I'm using VS 2003.NET EA, with WinXP, .NET 1.1, and have "Using
System.Data" and "Using System.Data.OracleClient" at the top of my C# class
definition. At first I thought this might be Oracle related, but
DataSet/DataTable/DataColumn is part of the System.Data namespace.
Thanks!
-Thomas
DataAdapter.Fill call. I'm writing a C# class that handles all the ADO.NET
work I need, and a different class will call the public methods of this
class.
So; I've created the two public fields I need- Public String _custId and
Public DateTime _orderDate. But, I can't get the values out of the
DataSet/DataTable/DataColumn without doing (in my mind) useless casting! For
instance, this doesn't work:
_custID = dsOrders.Tables["tblOrd"].Rows[iCurrentRow][0];
I get the message "Cannot implicitly convert type 'object' to 'string' ". To
make it work, I have to do:
_custID = (String)dsOrders.Tables["tblOrd"].Rows[iCurrentRow][0];
.....or....
_custID = dsOrders.Tables["tblOrd"].Rows[iCurrentRow][0].ToString();
I'm confused as to why? The following line displays "System.String":
Console.WriteLine(dsOrders.Tables["tblOrd"].Rows[iCurrentRow].Table.Columns[
0].DataType);
So, shouldn't that mean that I don't have to do anything else? Why do I need
to use (String) or .ToString() ?
And I also can't get the DateTime into a variable. Trying
_orderDate = dsHousing.Tables["tblOrd"].Rows[iCurrentRow][1];
gives me "Cannot implicitly convert type 'object' to 'System.DateTime' ".
And, again, if I display the value for Columns[1].DataType, it shows me
"System.DateTime". Worse, there's no ".ToDateTime()" method of the Column
object, so the only way I can get the DateTime field out is to cast it to a
string, and then convert it back to a DateTime. Compared to my problem with
the String column, I'm doing twice the un-necessary work just to get the
DateTime column!
Is this really what's supposed to happen? If the DataType shows string, why
do I need to perform an explicit conversion from object to string? It seems
like I'm doing un-necessary work; if the value is stored as a certain
datatype, I should be able to pull it out of the DataTable and put it into a
variable of the same type.
I even toyed with the idea of .NET assuming I wanted the DataColumn being
put into a String- but I can't find a property of DataColumn that just gives
me the value. In classic ADO, there was a Field object, which had a Value
property. I can understand "DataColumn" being an object, but is there a
specific property for pulling the value out of DataColumn? For instance, if
those code samples I just gave would be in classic ADO, I would be trying to
say _CustID = rs.Fields(x).Value.
Oh I'm using VS 2003.NET EA, with WinXP, .NET 1.1, and have "Using
System.Data" and "Using System.Data.OracleClient" at the top of my C# class
definition. At first I thought this might be Oracle related, but
DataSet/DataTable/DataColumn is part of the System.Data namespace.
Thanks!
-Thomas