0

I am making a small project to build my skills, and what I am trying to do is query a DataSet which is filled from a SQL database using a SELECT query.

However, I cannot manage to query the dataset further, I would like to be able to search the dataset and it will pull up results, this is what I have so far, though I get this error - An unhandled exception of type 'System.NullReferenceException' occurred

My table is called Animals, and I am searching in the parlour_number column.

I have searched, but I don't understand most of what I have looked at, I have a very basic understanding still.

DataSet ds;
DataRow dRow;

private void searchBtn_Click(object sender, EventArgs e)
    {
        string searchFor = tbSearch.Text;
        int results = 0;
        DataRow[] returnedRows;

        returnedRows = ds.Tables["Animals"].Select("parlour_number='" + searchFor + "'");

        results = returnedRows.Length;

        if(results > 0)
        {
            dRow = returnedRows[0];

            MessageBox.Show(dRow[1].ToString() + " " + dRow[2].ToString());
        }
        else
        {
            MessageBox.Show("No records found");
        }
    }
Nezz
  • 41
  • 2
  • 11
  • Probably you don't have values for the column at index 1 or 2. The duplicate covers this scenario. – Steve Sep 21 '15 at 19:58
  • In general with exceptions, Visual Studio can be of a great help. Place a breakpoint on the start of the method. Debug until you hit the breakpoint. Go to menu / DEBUG / Exceptions. Then check "Common Language Runtime Exceptions". Next, continue the debug. VS will help on the place where the exception is thrown. – Ladi Sep 21 '15 at 20:04
  • Thanks for the help guys, I have already seen that thread about the Null Values, as far as I can see from my code, rhe only null is the array value returnedRows. I thought the DataTable Select method would populate it? – Nezz Sep 22 '15 at 15:23
  • Feel like such an idiot... I had the name of the table in my SQL db and not the name of my DataSet.... Joys of being a newb. – Nezz Sep 22 '15 at 20:54

0 Answers0