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";
Michal, if this is the whole code and you don’t have set a default value for your textbox, it’s no surprise it isn’t working. You need to retrieve the value while reacting on some event, for example when you push some button… Or maybe monitor OnTextChanged event…
This example shows you create some form and immediately try to retrieve a value. That won’t work, because the textbox has no value yet.
Edit:
Those double-double slashes are causing you these problems. Don’t know where it is applying this escaping logic, it may be some setting you set, but there’s an easy solution:
Please let me know if this helped you.