Home > Mobile >  Getting values from my SQL database and applying them to the properties of my objects in C# window a
Getting values from my SQL database and applying them to the properties of my objects in C# window a

Time:03-27

I want users to be able to customize my software. Please I want to be able to set the properties of all my objects in my C# program, by selecting from SQL database and applying as default for all my objects e.g, textbox, label, groupbox, etc. Thanks.

I already tried:

int HBColor=Convert. ToInt32(@myargbcolor);

Groupbox mygbox=new Groupbox();
mygbox.DefaultBackcolor=Color.FromArgb(HBColor);

Where HBcolor is the argb color selected from and converted to into. But it's telling me the mygbox.DefaultBackcolor must be at the left hand of the assignment =. Please help me to be able to set the properties of my objects.

CodePudding user response:

This is working for me. We could give you more guidance if we knew what your SQL command looks like, but without that I took a guess.

int myArgbColor;

// this is a guess as to what your sql command looks like. adjust as necessary.
using (var sqlConnection = new SqlConnection("replace this with your connection string"))
{
    var sqlCommand = new SqlCommand("select @myargbcolor = ArgbColor from dbo.FormSettings;", sqlConnection);
    var myArgbcolorParam = sqlCommand.Parameters.Add("@myargbcolor", System.Data.SqlDbType.Int);
    myArgbcolorParam.Direction = System.Data.ParameterDirection.Output;
    sqlConnection.Open();
    sqlCommand.ExecuteNonQuery();
    myArgbColor = int.Parse(myArgbcolorParam.Value.ToString());
    sqlConnection.Close();
};

// If you want to refer to an existing `GroupBox`  rather than instantate a new one. Use something like `this.mygbox = ...`.
GroupBox mygbox = new GroupBox();
mygbox.BackColor = Color.FromArgb(myArgbColor);

Here it is in action:

enter image description here

CodePudding user response:

I've tried creating a new instance of groupbox or textbox but all it did was to create a group box object or textbox object, it didn't make my selected color the default color of all group box or textbox in my form.

I'm actually creating a software in which my users can customize the appearance, by inserting and updating the default page, header and footer, textbox, button color into my database. While on my forms load it selects the imputed color from the database and makes it the default background and border color of all the different form items, e.g header, footer, labels, buttons and other objects.

  • Related