0

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";
Ste
  • 1,136
  • 2
  • 10
  • 30
MichalCh
  • 201
  • 3
  • 15
  • are you getting an error ? what is it ? – Shyju Apr 24 '12 at 22:35
  • 1
    what error do you get? are you able to open the connection? try to hard code the server name in the z string and see what happens if you call databaza.Open(); – Davide Piras Apr 24 '12 at 22:37
  • You're saying that you want to load a string from a textbox, but there's no textbox in your code... You're only reffering to some settings (nastavenia) you set internally, but how do you populate it? What does "adresa_servera()" method do? Post RELEVANT code and maybe we'll be able to help you. – walther Apr 24 '12 at 22:40
  • It throws Exception has been thrown by the target of an invocation. on Application.Run(new Form1()); – MichalCh Apr 24 '12 at 22:40
  • 2
    Erm what doesn't work? I see no textbox either. You might want to have look at the SqlConnectionStringBuilder class. – Tony Hopkinson Apr 24 '12 at 22:42
  • That means something is wrong with your form, the constructor, a FormLoad event FormShow etc. Stick a debug on them and step through. It's usually a silly mistake. – Tony Hopkinson Apr 24 '12 at 22:45
  • i know its a silly mistake, but it drives me crazy :) i updated the post – MichalCh Apr 24 '12 at 22:50
  • @MichalChovaňák is that serbian? – Alex Gordon Apr 24 '12 at 22:52
  • @Артём Царионов its Slovak :) – MichalCh Apr 24 '12 at 22:54

2 Answers2

0

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:

string x = nastavenia.adresa_servera().Replace("\\", "\");

Please let me know if this helped you.

walther
  • 13,466
  • 5
  • 41
  • 67
  • Its not the whole code, its too big so i cant put it here. I know the problem is in the ConnectionString. When i use the debug tool, the violation is thrown when i try to open connection. And like i said, when i manualy put there the code it forks. I tryed to put this: "Data Source=" + x + ";Initial Catalog=ZBERUDAJOVTEPLA;Persist Security Info=False;User ID=sa; password=diplomovka"; in a Message box, and it shows the correct string. – MichalCh Apr 24 '12 at 23:02
  • @MichalChovaňák, So while using a debugger you can see that "x" has the value you want? What value you see when hovering over ConnectionString property after you combine it? – walther Apr 24 '12 at 23:07
  • Hmm it seems that the // is doubled "Data Source=MICHAL-PC\\\\SQLEXPRESS;Initial Catalog=ZBERUDAJOVTEPLA;Persist Security Info=False;User ID=sa; password=diplomovka" – MichalCh Apr 24 '12 at 23:13
  • @MichalChovaňák, please see my updated answer for quick fix. Should work. Not sure why is it escaping your textbox value though... Just tried it in my c# and no escaping whatsoever. – walther Apr 24 '12 at 23:36
  • I dont know why but it simply doubles /. Thx for your help a really apriciate it. Thanks – MichalCh Apr 24 '12 at 23:36
  • @MichalChovaňák, no problem. If it helped you, you can select the answer if you want ;) Ja by som sa mal vratit k praci :) – walther Apr 24 '12 at 23:38
  • Its my masters degree thesis so i am done now :) Your Slovak is pretty good :) Dont know how to select the answer. I should go to sleep, its 2:00 AM here. Ďakujem za pomoc kamarát :) – MichalCh Apr 24 '12 at 23:45
  • @MichalChovaňák, there should be a button near my answer to the left to accept. Can't miss it. A nemas zaco :) Keby si pozrel profil, videl by si, ze som tiez zo SVK :)) Ale pracovat treba... – walther Apr 24 '12 at 23:49
  • Jasné už som to označil :) A ďakujem ešte raz za pomoc. Už som program dokončil. – MichalCh Apr 24 '12 at 23:56
0

Hi you can do something like this:

try
            {
               string x = TextBox1.Text;
               string Connection ="Data Source=" + x + ";Initial Catalog=ZBERUDAJOVTEPLA;Persist Security Info=False;User ID=sa; password=diplomovka";
                using (var conn = new SqlConnection(Connection))
                {
                    using (var cmd = new SqlCommand("Select * from Yourtable", conn))
                    {

                        try
                        {
                            conn.Open(); 

                        }
                        catch (SqlException)
                        {
                            throw;
                        }
                        finally
                        {
                            if (conn.State == ConnectionState.Open) conn.Close();
                        }

                    }
                }
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }

Best Regards

BizApps
  • 6,048
  • 9
  • 40
  • 62
  • Hi thx for the post. I foud wit the help of others that the problem is in this:public string adresa_servera() { return textBox1.Text; } textBox1.Text; loads four //// instead of two //. But dont know why – MichalCh Apr 24 '12 at 23:33
  • Yeah c# read \\ to \\\\ so you just need is only one back slash. :) – BizApps Apr 25 '12 at 00:20