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

<p>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</p>

<pre><code>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 &lt; 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");
}
</code></pre>

<p>Can anyone help me to resolve this problem ?</p>

<p>Thanks in advance</p>
