0
using (Connection = new SqlConnection(CS))
{
    SqlDataAdapter adapter = new SqlDataAdapter("SELECT category_name FROM book_category", Connection);
    Connection.Open();

    DataTable dt_category = new DataTable();
    adapter.Fill(dt_category);

    if (dt_category.Rows.Count > 0)
    {
        categories = new string[dt_category.Rows.Count];

        for (int count = 0; count < dt_category.Rows.Count; count++)
        {
            DataRow category_row = dt_category.Rows[count];

            // Populate Tab Page for our tab control
            tc.TabPages.Add(category_row[0].ToString().ToUpper());
            tc.TabPages[category_row[0].ToString()].Name = category_row[0].ToString();

            // Store Categories data to global Array variable categories for further use
            categories[count] = category_row[0].ToString().ToUpper();
        }
    }
}

I try to populate my tab control pages using data from SQL Server, which I already got, but I want to set the name property too to the same value I extracted from my database. It throws an error

System.NullReferenceException: 'Object reference not set to an instance of an object.'

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Najar Pro
  • 1
  • 1
  • 1
    you use `.ToUpper()` when creating and adding tab page, but do not use it when trying to get tab page and set the name. Use the same key everywhere and this should work. – Serg Apr 17 '23 at 02:10

0 Answers0