0

I have the following code to change colors and store into the database... For some reason it only allows one color to be changed from the colorDialog...

if (colorDialog1.ShowDialog() == DialogResult.OK)
{                
    ColorDialog col = new ColorDialog();
    //col.ShowDialog();
    string color = col.Color.ToArgb().ToString("x");
    color = color.Substring(2, 6);
    color = "#" + color;
    con.Open();
    string sql2 = ("Update Employee SET PanelColor= '" + color + "' WHERE EID='" + 17002 + "' ");
    SqlCommand cmd2 = new SqlCommand(sql2, con);
    SqlDataReader dr2 = cmd2.ExecuteReader();
    con.Close();
    MessageBox.Show(color);
    panel1.BackColor = ColorTranslator.FromHtml(color);
}

Step 1 green color was selected

Step 2 Messagebox showing the color codes(for testing purpose)(wrong color code)

Step 3 it uploads the wrong color into the panel...

I am not sure what is wrong with this... Please advice me thank you

MethodMan
  • 18,625
  • 6
  • 34
  • 52
NoobCoder
  • 27
  • 5
  • Possible duplicate of [How do I get the color from a hexadecimal color code using .NET?](https://stackoverflow.com/questions/2109756/how-do-i-get-the-color-from-a-hexadecimal-color-code-using-net) – MethodMan Dec 15 '17 at 03:49
  • Is the green color selected from `colorDialog1`? Then just get `colorDialog1.Color`. Why to create another ColorDialog `col`? – skyoxZ Dec 15 '17 at 03:53
  • @MethodMan it is not the problem with the conversion but i think the problem is it is selecting the default color – NoobCoder Dec 15 '17 at 04:00
  • what is the datatype of the field holding color in the database.? – MethodMan Dec 15 '17 at 04:06
  • varchar(50) but the database does not affect this in this case... – NoobCoder Dec 15 '17 at 04:16

1 Answers1

0
if (colorDialog1.ShowDialog() == DialogResult.OK)
{                

    string color = colorDialog1.Color.ToArgb().ToString("x");
    color = color.Substring(2, 6);
    color = "#" + color;
    con.Open();
    string sql2 = ("Update Employee SET PanelColor= '" + color + "' WHERE EID='" + 17002 + "' ");
    SqlCommand cmd2 = new SqlCommand(sql2, con);
    SqlDataReader dr2 = cmd2.ExecuteReader();
    con.Close();
    MessageBox.Show(color);
    panel1.BackColor = ColorTranslator.FromHtml(color);
}
NoobCoder
  • 27
  • 5