0

I am trying to test different queries on a dataset from an existing Sql Server DB. I need to be able to grab a row by its id column for the project I am doing. What seems like a straightforward query gets no results however.

DataSet prodspdata = new prodspDataSet(); 

DataRow[] load;
load = prodspdata.Tables["TripNumber"].Select("[ctripnumber] = '21605178'");

Console.WriteLine(load.Length);
Console.ReadLine();

Output from load.Length is 0. I understand from documentation that load should be an array of rows returned from the Select() query. I know that this result exists from looking in the data so I expect load.length to be 1.

I tested a query from within Sql Server Management Studio to make sure I wasn't missing something:

SELECT * FROM TripNumber WHERE ctripnumber = '21605178'

This returns the 1 row as expected.

Is my Select query bad? I am brand new to C#, am I testing it wrong? I simply need to know if load contains any results so I know that my query works as intended.

EDIT: So it appears my DataTable has no rows.

DataTable loadTable;
loadTable = prodspdata.Tables["TripNumber"];
Console.WriteLine(loadTable.Rows.Count);
Console.ReadLine();

This returns 0 rows whereas loadTable.Columns.Countgives me 133

Did I miss a step, perhaps something to do with a TableAdapter?

lutze
  • 66
  • 5
  • 1
    It will help if you show the code for prodspDataSet – Juan Jul 22 '16 at 15:46
  • @Juan Are you specifying the prodspDataSet.Designer.cs code? There are 8400 lines. It was autogenerated when I added it as a data source in Visual Studio, via Project > Add New Data Source. – lutze Jul 22 '16 at 17:31
  • 1
    Are there any rows when you explore the dataset from the debugger? http://stackoverflow.com/questions/491555/how-can-i-easily-view-the-contents-of-a-datatable-or-dataview-in-the-immediate-w – Juan Jul 22 '16 at 18:31
  • @Juan I found my mistake, but to answer your question: there are no rows when I explore it in the debugger when I haven't done anything with the TableAdapter first. – lutze Jul 22 '16 at 19:08
  • Glad to hear that! – Juan Jul 25 '16 at 09:25

2 Answers2

2

Don't use the square brackets in your filtering.

load = prodspdata.Tables["TripNumber"].Select("ctripnumber = '21605178'");

Also remember that you can use Visual Studio debugger to see the contents of DataSets and Datatables.

Juan
  • 3,675
  • 20
  • 34
1

I had not actually filled my dataset with anything, which was causing the problem. I severely neglected the TableAdapter. The code I added was:

prodspDataSetTableAdapters.TripNumberTableAdapter ta = new prodspDataSetTableAdapters.TripNumberTableAdapter();

DataTable loadTable = ta.GetData();

So without a TableAdapter instantiated and a table filled with the data (based on the TableAdapter's settings) you basically get an empty schema of the database with tables, columns and no values. Rookie mistake.

lutze
  • 66
  • 5