-2

I am trying to set Background Color of Button(Names: b0,b1,b2,b3,b4,b5,b6,b7,b8,b9) in WPF on runtime.

Color Name is getting from Database that is Red right now. But it's giving me System.NullReferenceExceptionn: Object reference not set to an instance of an object

private void ButtonBgColor()
{
string qryBgColor = "select Name from lookup where code in (select VALUE from qSettings where name='BUTTON_BG_COLOR') and type='BgColor'";
try
{
sqlConnection.Open();
sqlCommand = new SqlCommand(qryBgColor, sqlConnection);
sqlDataReader = sqlCommand.ExecuteReader();
if (sqlDataReader.Read())
{
string BUTTON_BG_COLOR = sqlDataReader["Name"].ToString();
Button[] b = new Button[9];
for (int i = 0; i < b.Length; i++)
{
var textBlock08 = (TextBlock)b[i].Template.FindName("myTextBlock", b[i]);
textBlock08.Background = (System.Windows.Media.SolidColorBrush)new System.Windows.Media.BrushConverter().ConvertFromString(BUTTON_BG_COLOR);
}
}
}
catch (Exception exp)
{
MessageBox.Show(exp.ToString(), "Button Background Color Exception");
}

Can anyone help me to resolve this problem ?

Thanks in advance

Jaa Zaib
  • 151
  • 2
  • 6
  • 14

1 Answers1

1

You haven't assigned anything to b. It's just an empty array. Therefore, calling b[i] will always result in a null-ref.

Jace
  • 777
  • 1
  • 5
  • 18