using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
}
}

This post has been edited by Curtis Rutland: 02 October 2012 - 02:02 PM

Replies To: Access variable from Form2 to Form1.

Re: Access variable from Form2 to Form1.

Posted 02 October 2012 - 12:53 PM

There are a few ways. One nice way is to pass the current form (form1) to the showdialog() method and after the result is returned and is ok, set variables/call methods of form1 using data from form2. Here we call this from form1 when we show the dialog...

Form2 f2 = new Form2();
// Here "this" is going to be form1, the current form.
if (f2.ShowDialog(this) == DialogResult.OK)
{
// Read the contents of f2 TextBox and put it in a local variable of form1.
localDbString = f2.txtDatabase.Text;
}

You could also expose public methods on form1 and have form2 reference form one, calling its public methods and passing it values from form2.

You could create a separate class that both forms know about and used to store settings. Pass around an instance of that class and have various forms fill it or read it. This is a nice solution when you have a ton of settings that many forms may use.

Re: Access variable from Form2 to Form1.

Posted 05 October 2012 - 01:07 PM

It sounds like the problem is purely on establishing the connection. If you have Visual Studio Professional (or higher) on the machine you are trying to connect from, can you connect using the Tools.Connect to Database... menu option?

Re: Access variable from Form2 to Form1.

Posted 05 October 2012 - 01:36 PM

No I don't have Visual Studo Professional (or higher). Actually, all I want to do is make it possible for the user to change the connection string, and the only way I can think of is making a variable of the new SQL details like Instance, DB, UserID and Password but seems like the SqlConnection function can't read variables in the string.

So what i've done so far is:
The user enter the new SQL details in 4 textBoxes in another form. One for Instance, DB, UserID and one for Password.
When the user click the save-button the details stores in a xml file.
Then in form1, i've done 4 variables for each detail value.
And then created the ConnectionString with the ConnectionStringBuilder.
So why isn't this working?

Re: Access variable from Form2 to Form1.

Posted 05 October 2012 - 01:52 PM

Does the connection string that is built up and displayed by line 45 of your code look valid? If it looks valid, then the only questions left will be:
- can the machine be addressed by name
- are the ports open on that machine
- is the SQL server and associated services running on that machine

One way to independently test those outside you your program was to use VS. If you have SQL Server Management Studio, you could also try connecting without using your program.

As an aside, how is the Instance being entered by the user? Does it look like "MACHINENAME\SqlExpress" ?

Re: Access variable from Form2 to Form1.

Posted 05 October 2012 - 02:01 PM

Yes the instance looks like "MACHINENAME\SQLExpress" and there is no problems with the SQL server itself. I can connect to the SQL server without any problem by using this string instead of the variable: