0

I Have a WinForm with 2 Text boxes to input ServerName and Database, RadioButtons to switch between providers, and 1 Button to build ConnectionStrings depending of inputs. _ServerName and _DatabaseName are Global variables. I would like to build the connection string outside the Form and get the result returned to a label control in my Form, the code in my external class is as next:

    public static string _ServerName { get; set; }
    public static string _Base { get; set; }
    public static SqlConnection _Con { get; set; }
    static void ConOption1()
    {
        Global._Con = new SqlConnection();
        Global._Con.ConnectionString = @"data source=" + Global._ServerName + "; initial catalog=" + Global._Base + "; Integrated Security=True";
    }

The code in my form (Form1) is:

        private void button1_Click(object sender, EventArgs e)
    {
        Global._ServerName = textBox1.Text;
        Global._Base = textBox2.Text;
        ConOption1();
        label1.Text = Global._Con.ToString();
    }

The problem here is that I cannot call conOption1 from Form1 to get the built string in Label1.text, thanks for your help.

1 Answer 1

2

You did not mark ConOption1 as being public.

What do you exactly mean by "I cannot call conOption1 to get the built string"? The compiler not only won't compile your code but will also point at the exact problem. There is no point in asking the question on SO "Where is the error", as the compiler already told you where is the error.

Sign up to request clarification or add additional context in comments.

4 Comments

thank you for your reply, please consider that I am new to C#. I have changed from static void ConOption1() to public void ConOption1(), the compiler returns now: "The name 'ConOption1' does not exist in the current context"
@AlphaBird, you need to preface your call to ConOption with the class name and/or namespace. I suspect this will work: Global.ConOption1();
@Brad, can't get neither MyWorkSpace.MyClass.ConOption1() in the intellisens nor Global.ConOption1(); to work this seems to be a variable not a function.
@AlphaBird, the method needs to be both public AND static: public static void ConOption1()

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.