View Full Version : Another C# problem

10-28-2005, 08:09 PM
Why this's not working: (this's not the full code).

for (i = 0; i < dstTopics.Tables[0].Rows.Count; i++)
DataRow CatRow = dstTopics.Tables[0].NewRow();
CatRow["CategoryID"] = (int)dstTopics.Tables[0].Rows[i].ItemArray[0];

it gives me this error: Specified cast is not valid.
When i try this: Convert.ToInt32(dstTopics.Tables[0].Rows[i].ItemArray[0])

it gives me this error: Input string was not in a correct format.

when i try not using any cast type, it gives looooooooooooong error statment,
Thanks too much for help

10-28-2005, 10:11 PM
what are you tring to do? What information is going into CatRow["CategoryID"]?


10-28-2005, 10:53 PM
The CategoryID from the DataSet table and sure its integer, when i used the casting before with string it worked fine
string curCat;
curCat = (string)dstTopics.Tables[0].Rows[i].ItemArray[0];

i also tried to save the CategoryID in variable first but it didnt work. :confused:

10-28-2005, 11:10 PM
try this:

// create a datatable
DataTable myTable = new DataTable("tablename");

// create a column
DataColumn dcCategoryID = new DataColumn("Item ID", Type.GetType("System.Int32"));

// add column to datatable

// create an array
object [] myArray = new object[1];

// populate array
DataRow dr;
for(int i = 0; i < 4; i++)
myArray[0]= "item id " + i;
dr = dt.NewRow();
dr.ItemArray = myArray;

// display the table
foreach(DataRow myRow in myTable.Rows)
foreach(DataColumn myColumn in myTable.Columns)