ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  Graph color list from dB table
Posted by Bill on Jun-05-2020 13:05
How do I call the color code (0xFF0000) store in SQL dB table and paste it in my Graph table like this.

string mycolor = "0xFF0000";  //I stored this value as NVARCHAR(10) in my SQL table.
BarLayer layer = c.addBarLayer2(Chart.Side, 0);
layer.addDataSet(OUT, "+mycolor+", "ACTUAL VOL (KU)");

With this, there was an error. not int format.
What should be the syntax here?
Thanks in advance - Bill

  Re: Graph color list from dB table
Posted by Peter Kwan on Jun-05-2020 13:59
Hi Bill,

The color is a 32-bit integer. 0xFF0000 is a 32-bit integer in the C# language, so it can be used. "0xFF0000" is not an integer in the C# language. It is a text string. As C# is a strongly type language, it would not allow the code to compile.

To solve the problem, you can simply convert the text string to an integer using the C# function Convert.Int32.

int myColor = Convert.ToInt32("0xFF0000", 16);

See:

https://docs.microsoft.com/en-us/dotnet/api/system.convert.toint32?view=netframework-4.0#System_Convert_ToInt32_System_String_System_Int32_

If you can change the database schema, a better method is to store the color as an 32-bit integer in the first place and not as a text string. In this way, no conversion is necessary and the color is always valid. If you store as NVARCHAR, there is always a risk that the text is invalid. For example, if the color is entered by a user, he may enter "0xXYZ19845" and this may get stored in the database. This would cause the C# Convert.ToInt32 to throw an exception at runtime and you may need extra code to validate and handle all these situations.

Regards
Peter Kwan