0

I've got code like this:

DataSet QtyDS = null;
. . .
QtyDS = GetAllUPCDSDRecords(txtUPC.Text);

...that is blowing up with "cannot find table 0"

To try to prevent that, I've tried the following, all to no avail; I still get that err msg when I try to access the first table in the Dataset:

1)

if (null != QtyDS)

2)

string table0 = QtyDS.Tables[0].ToString();
if (!table0.Equals(string.Empty))

3)

if (null != QtyDS.Tables[0])

How can I safely determine whether the query is returning a dataset so as to avoid the err msg?

UPDATE

public DataSet getAllUPCDSDRecords(string upc)
{
    string query = string.Format(
        "SELECT tyger_id as tyger, upc_source as UPC, description as Descrip, unit_qty as Qty, "+
        "department as Dept, vendor_id as Ven, upc_pack_size as UPCPK, pack_size as PKSize, "+
        "unit_cost as Cst, unit_list as Lst "+
        "FROM {0} WHERE upc_source = {1}", tablename, upc);
    return dbconn.getDataSet(query);
}

public DataSet getDataSet( string dynSQL )
{
    checkConnection();

    SqlCeDataAdapter oDA = new SqlCeDataAdapter( dynSQL, objCon );
    DataSet          oDS = new DataSet( "Command" );

    try
    {
        oDA.Fill( oDS );
    } 
    catch
    {
        //SSCS.ExceptionHandler(ex, "DBConnection.getDataSet");
    }

    return( oDS );
} // getDataSet
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • 1
    It sounds to me like the problem could actually be in the `GetAllUPCDSDRecords` method. Can you post its code? Other than that you can test the number of tables using `QtyDS.Tables.Count`. – Martin Mar 26 '13 at 22:50
  • Okay, adding the other code above. – B. Clay Shannon-B. Crow Raven Mar 26 '13 at 23:05
  • There is no Count property for Dataset.Tables, at least not in my prehistoric version ("Hello from .NET 1.1, a land far, far away, etc.") – B. Clay Shannon-B. Crow Raven Mar 26 '13 at 23:15
  • There is, however, `Dataset.Tables.List.Count`. No ... that's protected. Hmmmm . . . – Jim Mischel Mar 26 '13 at 23:22
  • Hmm .Net 1.1, got to cast my mind back some way! :) Althought this is not a great approach, can you not simply catch the `DataException` that occurs when you try to access the table in the `DataSet`? You clearly wont get the exception when the table does exist, so you can use that as the test for whether it exists or not. – Martin Mar 26 '13 at 23:45
  • It's probably worth mentioning here that DataSets are, generally, a bad idea all around. They are slow memory hogs. A DataReader or ResultSet is far better. You'd also get an order of magnitude better speed on the query in your example by foregoing the query parser altogether and using table direct. – ctacke Mar 27 '13 at 02:27
  • As a side note on all of your questions of late, I'm still confused by the lack of debugging. I've been doing CF work since before CF 1.0 (there is no CF 1.1) and I've *always* had debugging capabilities. I'd encourage you to ask a question about getting that working so we can help you become more productive. – ctacke Mar 27 '13 at 02:29
  • @ctacke: I'm not going to mess with this spaghetti more than I need to; it's a "whack-a-mole" code body - touch one thing, and something seemingly unrelated elsewhere pops up to screech at you. There are tons of global variables, magic numbers, forms referencing forms, etc. Anyway, I already did ask the debugging question and explained some of my challenges that way here: http://stackoverflow.com/questions/14714163/how-can-i-run-my-windows-ce-project-from-within-visual-studio-2003 – B. Clay Shannon-B. Crow Raven Mar 27 '13 at 15:00
  • @ctacke revisited: the legacy code base is also chock full of background threads that run willy-nilly and are, besides being inherently confusing and complex, often-times seem nonsensical and incomprehensible; not helping matters is the complete lack of comments or documentation. – B. Clay Shannon-B. Crow Raven Mar 27 '13 at 15:57

2 Answers2

1

Without seeing the rest of your code, I would assume your query is not returning anything. Try placing a breakpoint on return( oDS ); and see what oDS has to say. If it doesn't have anything in it, then your query isn't working correctly.

You may need to check your query syntax or connection string.

Also, you can find the table count by checking QtyDS.Tables.Count. If it's greater than 0, you have tables.

McCee
  • 1,549
  • 10
  • 19
  • Can't use breakpoints - this is a Windows CE project that must be run in an emulator or directly on the device, but due to my limitations based on my setup and the ancient version of software I'm using, I can't do that. I have to be "creative" (old school) to debug this. See for details http://stackoverflow.com/questions/15644987/how-could-the-existence-of-pseudo-debug-strings-cause-a-difference-in-functional – B. Clay Shannon-B. Crow Raven Mar 26 '13 at 23:16
  • Aha. Do you have a way to run queries on the database in question? You could at least eliminate the query syntax as a possible issue. Also, try iterating through the tables themselves: `foreach (DataTable myTable in QtyDS.Tables) { Console.WriteLine(myTable.TableName); }`. If you get output, then there are tables. – McCee Mar 26 '13 at 23:25
  • No, unfortunately; they are SQLCE tables for which I do not have a utility such as Data Explorer or some such. It's such an archaic version of everything that I'm using that the niceties and cool tools we all get used to doing modern development are usually not available. I'm jumping through more hoops than a Bengal Tiger at a circus. – B. Clay Shannon-B. Crow Raven Mar 26 '13 at 23:33
1

It looks like you are swallowing and ignoring your error, which would make you think your code is running fine when it is not.

Remove the comment.

public DataSet getDataSet( string dynSQL )
{
  checkConnection();

  SqlCeDataAdapter oDA = new SqlCeDataAdapter( dynSQL, objCon );
  DataSet          oDS = new DataSet( "Command" );

  try
  {
      oDA.Fill( oDS );
  } 
  catch (Exception ex)
  {
      Console.WriteLine(ex.Message);
  }

  return( oDS );
}

Just put a break point on the Console.WriteLine to read what the error is. Once that is fixed, you should just remove the whole try...catch routine so that if something fails it is not hidden from you.

To take it a step further, change the signature and put a check on that checkConnection method:

public DataSet getDataSet( string dynSQL )
{
  var oDS = new DataSet("Command");
  try
  {
    if (!checkConnection()) {
      throw new Exception("No connection to database.");
    }
    using (var oDA = new SqlCeDataAdapter(dynSQL, objCon)) {
      oDA.Fill(oDS);
    }
  } 
  catch (Exception ex)
  {
      Console.WriteLine(ex.Message);
  }

  return( oDS );
}
  • It would be nice to be able to set a breakpoint, but I am writing to a log file now, which is helping a lot. And oddly enough, checkConnection() does not return a book - it's a void method (?!?) – B. Clay Shannon-B. Crow Raven Mar 27 '13 at 15:38