none
Strange problem with dataset RRS feed

  • Question

  • Hi !

     I have a strange problem with a dataset.... it is really a nightmare... I fill my dataset from a DataTableAdapter using a query which sometimes returns 0 values. This is a part of my code: 

    DataSet ds = new DataSet();

    OdbcDataAdapter oda = new OdbcDataAdapter("select value1,value2 from myTable where id>" + greatherThan, conn);

    oda.Fill(ds, "DataFromMessageInfo");


     Then in my code( which is a windows service) i use this syntax to check if dataset contains data and then use them:

    if (ds1.Tables[0] != null){

    for (int j = 0; j < ds1.Tables[0].Rows.Count; j++){

    object[] items = ds1.Tables[0].Rows[j].ItemArray;
    ....}}

     The big problem is that sometimes... i get the following error message: "Cannot find table 0 !" Should i use this condition:
    if(ds1!=null) instead of the above one, i am really confused... and this error kills me....and stops all my windows service...

    Please help me if you have an ideea !

    Regards,
    radu


    radu
    Friday, February 27, 2009 3:06 PM

Answers

  • It looks like that DataTable is not always being filled in. There are two options.

    1) Change the fill statement to passing in a DataTable (create this along with the DataSet at the beginning), this way you will always have a DataTable in existance. If there are zero rows the table will have zero rows

    Or as another option you can add an additional check into your if statement

    if (ds1.Tables.Count > 0 ){

    for (int j = 0; j < ds1.Tables[0].Rows.Count; j++){

    object[] items = ds1.Tables[0].Rows[j].ItemArray;
    ....}}

    Thanks

    Chris Robinson

    Program Manager - DataSet


    This posting is provided "AS IS" with no warranties, and confers no rights.
    Friday, February 27, 2009 6:18 PM