At the form load i am trying to see if my application can connect to the database or not. If it doesn't connect i want to show a message box with a nice message, and close the application. The problem that i am having is that when the it trys to close the application it hits the Form_closing event that asks them "if they want to exit the application" it looks kind of strange for a user that does not have access to the application/database to view the message box. I just want to skip the form closing event and just close the form, any help would be greatly appreciated.
private void Form1_Load(object sender, EventArgs e)
{
checkcon();
}
private void checkcon()
{
try
{
MSSQL.SqlConnection con = new MSSQL.SqlConnection(constr);
con.Open();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show("Your domain account does not have sufficient privilages to continue with the application please contact the IS support Team.");
Close();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
DialogResult result = MessageBox.Show("Are you sure you want to exit the application?", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.No)
{
e.Cancel = true;
}
else
{
}
}
add a static bool var as a check wether to show Dialogue or not:
public static bool HasPermission=true;
private void checkcon()
{
try
{
MSSQL.SqlConnection con = new MSSQL.SqlConnection(constr);
con.Open();
con.Close();
}
catch (Exception ex)
{
HasPermission=false;
MessageBox.Show("Your domain account does not have sufficient privilages to continue with the application please contact the IS support Team.");
Close();
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{ if (HasPermission)
{
DialogResult result = MessageBox.Show("Are you sure you want to exit the application?", "Alert", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
if (result == DialogResult.No)
{
e.Cancel = true;
}
else
{
}
}
}