...

View Full Version : Another C# problem



Cipher
10-28-2005, 09: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

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

-john

Cipher
10-28-2005, 11: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:

kampfer
10-29-2005, 12:10 AM
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
myTable.Columns.Add(dcCategoryID);

// 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;
myTable.Rows.Add(dr);
}

// display the table
foreach(DataRow myRow in myTable.Rows)
{
foreach(DataColumn myColumn in myTable.Columns)
{
Console.WriteLine(myRow[myColumn]);
}
}

-john



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum