0

This code throws an error

Object reference not set to an instance of an object

Code:

string strSelect = "SELECT LookUpValuesID, [Description] FROM tblLookUpValues WITH(NOLOCK) WHERE LookUpTypeID = 2 ORDER BY [Description]";

using (SqlConnection cnSQL = new SqlConnection(strConnection))
using (SqlDataAdapter daValues = new SqlDataAdapter(strSelect, cnSQL))
{
    SqlCommandBuilder cbValues = new SqlCommandBuilder(daValues);
    DataSet dsValues = new DataSet();
    daValues.Fill(dsValues);

    MessageBox.Show(dsValues.Tables.Count.ToString());
    chkbxLatches.DataSource = dsValues.Tables[0];
}

The MessageBox shows that there is 1 table in dsValues. Why would I be getting that error if there is a table present? Is there something else I should be looking for?

Thanks for any suggestions.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Donald
  • 19
  • 5
  • Side question, do you *really* "need" that `NOLOCK` hint? – Thom A Jun 24 '22 at 14:08
  • I worked at a place that mandated NOLOCK due to their massive databases and continual updates. Whether that was actually required, I've no idea, but we were chastised if we didn't do it. – Neil Jun 24 '22 at 14:10
  • Assigning a table to a datasource will involve lots of reflection to populate the datasource. I wonder if its not actually the assignment in your code, but something in the display mapping? – Neil Jun 24 '22 at 14:11
  • 2
    *"I worked at a place that mandated NOLOCK"* what you mean, then, is you worked at a company that had a database that performed poorly, and instead of fixing the problem they slapped a sticker on it that said "The data you get may be incorrect". The fact that they weren't even teaching people to change the isolation instead shows they had no idea what they were doing. If you aren't at that company any more, I suggest you stop that bad habit. See [Bad habits : Putting NOLOCK everywhere](https://www.sentryone.com/blog/aaronbertrand/bad-habits-nolock-everywhere) – Thom A Jun 24 '22 at 14:14

0 Answers0