Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder Cipher's Avatar
    Join Date
    Dec 2004
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Another C# problem

    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
    i need a creative atmosphere

  • #2
    New Coder
    Join Date
    Oct 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what are you tring to do? What information is going into CatRow["CategoryID"]?

    -john

  • #3
    Regular Coder Cipher's Avatar
    Join Date
    Dec 2004
    Posts
    123
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    i need a creative atmosphere

  • #4
    New Coder
    Join Date
    Oct 2005
    Posts
    24
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •