1

I'm searching a method for setting dynamically the values of a list of variables whose names and values are readed from a database.

I've found a lot of code for getting the variable name and value, but nothing that works for setting the value.

I'll try to explain myself better. Think to have a database that contains two columns, say "name" and "value". Say for example that you have two records:

1) Name="string1", Value="message1" 2) Name="string2", Value="message2"

I have the code to read the two records, what i want is a way to take dinamically the names of the variables from the records and assign to them the corresponding values.

The code is this:

SqlCommand cmd = new SqlCommand("Select Name, " + lng + " from dbo.traductions", Global.languageconn);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
    var v1 = dr["Name"].ToString();
    var v2 = dr[lng].ToString();

    //Something here to assign the value stored in v2 to the variable whose name is stored in v1

}

Thank you all

8
  • Why not just create a class with fields for "name" and "value", and read your SQL data into there? Commented Jul 6, 2017 at 13:27
  • Because my variables in a second moment will become components' names. For example in the database i'll have records like this: Name="textbox1", Value1="Insert your name", Value2="entrez votre nom", Value3="geben Sie Ihren Namen", Value4="Inserisci il tuo nome" and, depending on which value i'll read, the corresponding component has to take that value (message in this example). So i need a piece of code that allow me to refer to textbox1 passing from a variable that contain "textbox1". So I need something that do it dinamically, depending on the query result from the db. Commented Jul 6, 2017 at 13:41
  • Or you could have a language flag on those records, include a language field in the class, and then select the appropriate object accordingly. Commented Jul 6, 2017 at 13:42
  • Yes, at the beginning I thought to this solution, but my boss wants me to do this using a database in the way I've explained, so i have no choice :( Commented Jul 6, 2017 at 13:49
  • For example, to read a value you can use something like this: var fieldValue = source.GetType().GetField(field).GetValue(source); I need something to do the opposite, something to set the value. Commented Jul 6, 2017 at 13:52

2 Answers 2

1

I don't know why you would want to do this but why not just use a dictionary?

SqlCommand cmd = new SqlCommand("Select Name, " + lng + " from dbo.traductions", Global.languageconn);
SqlDataReader dr = cmd.ExecuteReader();
var variables = new Dictionary<string, string>();
while (dr.Read())
{
    var v1 = dr["Name"].ToString();
    var v2 = dr[lng].ToString();
    variables.Add(v1, v2);
}

Then to use them:

var x= variables[v1];

Update:

I see you added a note saying you need to refer to objects. You could change the Dictionary to hold objects instead. You didn't mention the type so for argument sake I will assume they are all text boxes:

SqlCommand cmd = new SqlCommand("Select Name, " + lng + " from dbo.traductions", Global.languageconn);
    SqlDataReader dr = cmd.ExecuteReader();
    var variables = new Dictionary<string, object>();
    while (dr.Read())
    {
        var v1 = dr["Name"].ToString();
        var v2 = dr[lng].ToString();
        var textbox = new TextBox() { Text = v2 };
        variables.Add(v1, textbox);
    }

Then to use them by name:

var textbox= variables[v1];
Sign up to request clarification or add additional context in comments.

1 Comment

They're mostly textboxes, labels and a certain number of buttons and "normal" string variables.
0

Based on your comments, you want to access "controls by name"; this is accomplished with Controls.Find():

Control ctl = this.Controls.Find(v1, true).FirstOrDefault();
if (ctl != null && ctl is TextBox)
{
    TextBox tb = (TextBox)ctl;
    tb.Text = v2;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.