ADO Tables

  • Thread starter Thread starter netnav
  • Start date Start date
N

netnav

I have created a DataTable with several columns and then added some
rows and data in the rows. It seems that when you access the data in
a particular row/column it is of type Object and not the data type
that you specified when you created the column. So it seems that to
use the data that I put in the row/columns I have to convert the
Object to the appropriate type, otherwise its just a string. So for
example if I have a column that is a DateTime datatype to use the
dates that I put into my table as date I have to convert each of
them. This doesn't seem right. Is there something I am missing.

Thanks
 
NetNav:

If I'm duplicating your scenario correctly, I'm definitely not getting the
same results. Each Column has a type which maps back to a System.Type. I
created a DataColumn for instance and when I get type, referencing either
the DataTable[RowIndex][ColumnIndex] or specify the specific datarow by name
and reference the column, each time I'm getting Type System.String which is
exactly what I expect.

DataColumn dcFullName = new DataColumn();

dcFullName.DataType = System.Type.GetType("System.String");

MessageBox.Show(dcFullName.GetType().ToString()); //Returns
System.Data.DataColumn

dcFullName.ColumnName = "FullName";

dtEmployees.Columns.Add(dcFullName);


DataRow dro = dtEmployees.NewRow();

dtEmployees.Rows.Add(dro);

dro[0] = "Bill";

MessageBox.Show(dtEmployees[0][0].GetType().ToString());//Returns
System.String

MessageBox.Show(dro[0].GetType().ToString());//Returns System.String

However, if I change the DataType in the first line to System.DateTime and I
change "Bill" at the bottom to a valid date (I have to do this so I don't
raise an exception), GetType returns DateTime. If I use Integer, it does
the same. If I don't specify a type then it will return System.String but
that's what you would pretty much expect.Anyway, if it's returning Object,
there's definitely a way to get it not to. Can you post the code, if I
could take a look at it I could probably figure out what's happening.

Cheers,


Bill

netnav said:
I have created a DataTable with several columns and then added some
rows and data in the rows. It seems that when you access the data in
a particular row/column it is of type Object and not the data type
that you specified when you created the column. So it seems that to
use the data that I put in the row/columns I have to convert the
Object to the appropriate type, otherwise its just a string. So for
example if I have a column that is a DateTime datatype to use the
dates that I put into my table as date I have to convert each of
them. This doesn't seem right. Is there something I am missing.

Thanks
 
I agree with you but consider the following code.

DataTable dt = new DataTable("Testing");
dt.Columns.Add("somenumber", typeof(double));
DataRow row;
row = dt.NewRow();
row["somenumber"] = 22.342;
dt.Rows.Add(row);

double anothernumber = dt.Rows[0]["somenumber"] + 25;

The last line of this code will thro an error indicating you can't use
+ with operands of object and int. This indicates to me that I must
convert the data in the table to the appropriate type so that I can
use it. The code that I have been using looks like this.

double anothernumber = Convert.ToDouble(dt.rows[0]["somenumber"]) +
25;

It seems silly to have defined the column type and then have to
convert it to that data type to be able to use it. I hope I'm
missing something.

Thanks for the help.
 
I agree with you but consider the following code.

DataTable dt = new DataTable("Testing");
dt.Columns.Add("somenumber", typeof(double));
DataRow row;
row = dt.NewRow();
row["somenumber"] = 22.342;
dt.Rows.Add(row);

double anothernumber = dt.Rows[0]["somenumber"] + 25;

The last line of this code will thro an error indicating you can't use
+ with operands of object and int. This indicates to me that I must
convert the data in the table to the appropriate type so that I can
use it. The code that I have been using looks like this.

double anothernumber = Convert.ToDouble(dt.rows[0]["somenumber"]) +
25;

It seems silly to have defined the column type and then have to
convert it to that data type to be able to use it. I hope I'm
missing something.

Thanks for the help.
 
I agree with you but consider the following code.

DataTable dt = new DataTable("Testing");
dt.Columns.Add("somenumber", typeof(double));
DataRow row;
row = dt.NewRow();
row["somenumber"] = 22.342;
dt.Rows.Add(row);

double anothernumber = dt.Rows[0]["somenumber"] + 25;

The last line of this code will thro an error indicating you can't use
+ with operands of object and int. This indicates to me that I must
convert the data in the table to the appropriate type so that I can
use it. The code that I have been using looks like this.

double anothernumber = Convert.ToDouble(dt.rows[0]["somenumber"]) +
25;

It seems silly to have defined the column type and then have to
convert it to that data type to be able to use it. I hope I'm
missing something.

Thanks for the help.
 
Back
Top