My form relies on a SQL database view, and I wanted to catch if the user could not connect.
The code I have shown to test the SQL connection all appears to work (I can block my connection and receive the message, unblocking and the application loads)
The problem is closing the form. You can see three examples of what I have tried commented out in the "else" section of the bool code below.
1. The program hangs on this code as it cannot access a disposed object using this.Dispose(); or this.Close();
2. The program closes, but a blank form opens if I use Application.Exit();
I would like for the application to just close. Rather than do anything else, as if there is no SQL connection, there is no application.
Am I doing this in the wrong order, failing to account for some step? I don't understand why the a blank form opens or why it tries to open the form, after I have told it to either close it or dispose of it.
I have obviously tried a few different ordering of events before posting this and if I do not have it in this order, it hangs on the sql.database.Fill(server.View) section within the Designer.cs - so I think my order is closer to what it should be - but I could still have it slightly off.
Code for checking SQL connection:
The SqlTest() method is:
Any pointers gratefully received.
The code I have shown to test the SQL connection all appears to work (I can block my connection and receive the message, unblocking and the application loads)
The problem is closing the form. You can see three examples of what I have tried commented out in the "else" section of the bool code below.
1. The program hangs on this code as it cannot access a disposed object using this.Dispose(); or this.Close();
C#:
static void Main()
{
Application.Run(new Form1());
}
2. The program closes, but a blank form opens if I use Application.Exit();
I would like for the application to just close. Rather than do anything else, as if there is no SQL connection, there is no application.
Am I doing this in the wrong order, failing to account for some step? I don't understand why the a blank form opens or why it tries to open the form, after I have told it to either close it or dispose of it.
I have obviously tried a few different ordering of events before posting this and if I do not have it in this order, it hangs on the sql.database.Fill(server.View) section within the Designer.cs - so I think my order is closer to what it should be - but I could still have it slightly off.
Code for checking SQL connection:
C#:
bool result = SqlTest(@"Data Source = Server; database= database; Integrated Security=True");
if (result)
{
InitializeComponent();
// calls the rest of the 'start up' methods
}
else
{
//this.Close();
//this.Dispose();
//System.Windows.Forms.Applciation.Exit();
}
The SqlTest() method is:
C#:
using (SqlConnection connection = new SqlConnection(connectionString))
{
try
{
connection.Open();
return true;
}
catch (SqlException)
{
MessageBox.Show("Failed connection");
return false;
}
}
Any pointers gratefully received.