Inserting BackColor into MS Access Database

PedroA

Active member
Joined
Oct 14, 2013
Messages
26
Programming Experience
Beginner
Hi,

Can I insert the back colour of a button into a database?

Such as the following, which does not work (Error: "Operator '+' is not defined for types 'String' and 'System.Drawing.Color')

Query = "INSERT INTO table1 (field) VALUES ('" + Me.day.BackColor + "')"
cmd = New OleDbCommand(Query, con)
cmd.ExecuteNonQuery()

Thanks







 
Firstly, don't use + to perform string concatenation. Always use &. Sometimes they do exactly the same thing but other times they do not. & is the string concatenation operator so use it when you want to perform string concatenation.

Secondly, don't use string concatenation to insert values into SQL code. To learn why and how to use parameters, follow the Blog link in my signature below and check out my post on Parameters In ADO.NET.

Finally, you can't save a .NET Color value directly to a database. What you can do is call Color.ToArgb to convert the Color to an Integer and save that, then use Color.FromArgb to convert it back to a Color value after retrieving it.
 
Back
Top