Resolved just close application?

ConsKa

Well-known member
Joined
Dec 11, 2020
Messages
140
Programming Experience
Beginner
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();

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.
 
Solution
First of all. Do not call this.Dispose() on a form, especially if someone else has a reference to your form. Let whomever instantiated your form take care of disposing your form. In general, though, when you close your form via this.Close() the garbage collector will take care of disposing it if the person who instantiated your form doesn't explicitly dispose it.

Anyway, the simplest approach is do check for the connection even before you open your form. Something like:
C#:
void Main()
{
    if (SqlTest())
        Application.Run(new MainForm());
}
First of all. Do not call this.Dispose() on a form, especially if someone else has a reference to your form. Let whomever instantiated your form take care of disposing your form. In general, though, when you close your form via this.Close() the garbage collector will take care of disposing it if the person who instantiated your form doesn't explicitly dispose it.

Anyway, the simplest approach is do check for the connection even before you open your form. Something like:
C#:
void Main()
{
    if (SqlTest())
        Application.Run(new MainForm());
}
 
Solution
Works perfectly thank you, and thank you for the advice about closing rather than disposing. I generally used close, but given the issues was willing to try alternatives.
 
Back
Top Bottom