Problem with variable connection to C# Visual Studio Database

Asked

Viewed 65 times

1

I have my connection string that takes the connection variable from App.Config

static String string_conn = ConfigurationManager.ConnectionStrings["bd1"].ConnectionString;

However I put a function that lists the banks in a Combobox in the login form and sends the name of the connection to this main form that contains the connection above, but I need this value that comes from the login form to be in place of this

Connectionstrings**["bd1"]**

for example:

   String nomeConexao = Form1.LoginInfo.StringConexao;

static String string_conn = ConfigurationManager.ConnectionStrings[nomeConexao ].ConnectionString;

In other words, instead of

ConnectionStrings["bd1"]

Stay

ConnectionStrings[nomeConexao ]

only that of the error, it does not accept the variable even being a string which I can do?

2 answers

1

My suggestion would be to do the following.

Declare your connection variable like this:

string _conn;

And in the event of index of your ComboBox, put the following code:

private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var selectedConn = (sender as ComboBox).Text;

    _conn = ConfigurationManager.ConnectionStrings[selectedConn].ConnectionString;
}

Thus, after selecting the desired connection on ComboBox, you would have the desired connection in the global variable _conn.

0


I solved it! I was putting the code snippet in the wrong place, it was careless of me.

I was putting off the button click function.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.