I am developing a program where I need to make a settings form for my database connection. I stopped at how to set the connection string from textboxes. Here is my code:
In the settings form:
public string adresa_servera()
{
return textBox1.Text;
}
The text in this textbox is: MICHAL-PC\SQLEXPRESS
In my main Form I use:
db_nastavenia nastavenia = new db_nastavenia(); //db_nastavenia is the name of the settings Form
string x = nastavenia.adresa_servera();
SqlConnection databaza = new SqlConnection();
databaza.ConnectionString = "Data Source=" + x + ";Initial Catalog=ZBERUDAJOVTEPLA;Persist Security Info=False;User ID=sa; password=diplomovka";
To x I load text from the textbox of the settings Form.
When I try it by manually typing the connection string like this, it works well:
databaza.ConnectionString = "Data Source=MICHAL-PC\\SQLEXPRESS;Initial Catalog=ZBERUDAJOVTEPLA;Persist Security Info=False;User ID=sa; password=diplomovka";