0

I have a form that has a few labels that display data from a SQL database. It uses a stored procedure to return one line of data. As part of the returned dataset, a label_color is also returned. I want to be able to set a certain label to this color. The returned format is Hex value ('#006600'-- for Green etc)

You will see in the if statement.. I have a lbl1latest1.ForeColor = Color.Red .. Instead I want to set it to what SQL returns.. can some one help me out please?

            myDbconnection.Open();

            string sqlQRY1 = "exec [sp_get_standpack_values]  1";
            SqlCommand cmd1 = new SqlCommand(sqlQRY1, myDbconnection);
            SqlDataReader reader1 = cmd1.ExecuteReader();
            reader1.Read();

            if (reader1.HasRows)
            {
                lblStandpack1.Text = reader1["standpack"].ToString();
                lbltarget1.Text = reader1["cl_rating"].ToString();
                lbllatest1.Text = reader1["XRayCL"].ToString();

                string colorcode = reader1["label_color"].ToString();
                int argb = Int32.Parse(colorcode.Replace("#", ""), NumberStyles.HexNumber);
                Color clr = Color.FromArgb(argb);
                lbllatest1.ForeColor = clr;

            }
            else
            {
                lblStandpack1.Text = "";
                lbltarget1.Text = "";
                lbllatest1.Text = "";
                lbllast10avg1.Text = "";
                lbltodayavg1.Text = "";
            }
            myDbconnection.Close();
Harry
  • 2,636
  • 1
  • 17
  • 29
  • 1
    Use the `Color.FromArgb` method to create a `Color` value and assign that to the `ForeColor` property of the `Label`. – jmcilhinney Oct 07 '15 at 01:39
  • What techniques have you tried so-far to convert Hexadecimal color values to a `Color` instance? – Dai Oct 07 '15 at 01:39
  • I can easily modify the SQL to return Red, Green or what ever instead of the Hex value. The issue is not in converting it. The issue I have is how do I assign the lbllatest1.ForeColor to what is being returned from SQL in reader1.Read() .Hope my question makes sense. – Harry Oct 07 '15 at 02:16
  • PS: - I am a newbie at WINFORM and C# if you can't tell from the code already. – Harry Oct 07 '15 at 02:17
  • OK.. found the answer. thanks for your efforts :) I have modified the original question code to reflect what I needed in case anyone else is looking – Harry Oct 07 '15 at 02:22

0 Answers0