Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All

I have a table tblFlags with 3 columns UniqID, FlagName,FlagValue

This table is used for storing app settings ie DataPathBackup

What i would like to do is loop through this table to set public static strings upon startup

ie public static string "FlagName" = "FlagValue"
this should return : public static string DataPathBackup = z:\AppBackup\


How would i be able to achieve this?

What I have tried:

I have set some default values
ie
public static String DataPathBackup= @"Z:\AppBackup\";

then i call FillFlags()

From below;
j returns the correct FlagName and k returns the correct FlagValue


so i would like to have something like
public static string "j" = "k"

this will then overide my default setting if it was found in this table
C#
public void FillFlags()
{
    string connectionString = null;
    SqlConnection cnn,cnn2;
    connectionString = CString;
    cnn = new SqlConnection(connectionString);
    cnn2 = new SqlConnection(connectionString);
    SqlDataReader dr,dr2;

        SqlCommand comm = new SqlCommand("SELECT [FlagName],[FlagValue] from tblFlags");

    comm.Connection = cnn;
        cnn.Open();
        dr = comm.ExecuteReader();

        while (dr.Read())
        {
            string j = dr["FlagName"].ToString();
           
            SqlCommand comm1 = new SqlCommand("SELECT [FlagValue] from tblFlags WHERE FlagName='"+ j +"'");
            comm1.Connection = cnn2;
            cnn2.Open();
            dr2 = comm1.ExecuteReader();
            MessageBox.Show(j);
            while (dr2.Read())
            {
                
                string k=dr2["FlagValue"].ToString();
                //j = k;
               
                MessageBox.Show(k);
            }
            dr2.Close();
            dr2.Dispose();
            cnn2.Close();
        }
        dr.Close();
        dr.Dispose();

        cnn.Close();
}
Posted
Updated 29-Aug-16 23:09pm
v2
Comments
Tomas Takac 30-Aug-16 4:55am    
What's the problem with your code? Few comments though:
1) Use using Statement[^] with disposable objects.
2) You can use ExecuteScalar Method[^] when you are retrieving a single value from the database.
3) Consider loading all the flags and process the whole list at once.
Member 11259478 30-Aug-16 5:11am    
Hi Tomas

Thank you for taking the time to look at my question

It does not set my static string at all

In below section:
if I change j=k to DataPathBackup = k it works

Problem is that i would like not to specify the public static string manually - it must get it from the tblFlags FlagName

how would i be able to do the whole list in 1 go?

Thanks



while (dr2.Read())
{

string k=dr2["FlagValue"].ToString();
//j = k;

MessageBox.Show(k);
}

1 solution

Although it's probably possible to do this, that doesn't make it a good idea. I suspect it's possible to do exactly what you want using reflection:
C#
public static string MyVariable;
...
MyVariable = "Old value";
SetVariableByName("MyVariable", "New value for MyVariable");
Console.WriteLine(MyVariable);

And get
"New value for MyVariable"
printed. (But you'd have to write the method, and it would be complicated)

But it's not nice, slow, and prone to errors.
Instead, consider using a Dictionary:
C#
public static Dictionary<string, object> MySettings = new Dictionary<string, object>();

And use the name and value you retrieve from your DB to set entries in that:
C#
MySettings["MyVariable"] = "New value for MyVariable";
It's a lot easier to understand what is going on, and a lot safer!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900