Help! My window application won't close.

G

Guest

I have a Window based application that shows up still running in the task
manager when I close it. It reaches the "this.close" statement and then it
stops at the "}" at the section of the following code after the
"Application.Run(new VMSMain());":
[STAThread]
static void Main()
{
Application.Run(new VMSMain());
}

I can't figure this one out at all. I would really appreciate some help
with this one.
Thanks, Alpha

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace VMS
{
/// <summary>
/// Summary description for VMSMain.
/// </summary>
public class VMSMain : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private SqlConnection conDB;
private SqlCommand scUpdate, scGetPath;
private string strATSPath, NewATS;
private System.Windows.Forms.Label label1;

[STAThread]
static void Main()
{
Application.Run(new VMSMain());
}



public VMSMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//Connect to VMS DB
// string conStr = @"Server=DEVELOPMENT\NetSDK; user id=tis; password=tis;
database = VMS";
// string conStr = @"Server=WENDI; user id=tis; password=tis; database =
VMS";
string conStr = @"Server=Support\NetSDK; user id=tis; password=tis;
database = VMS";

conDB = new SqlConnection(conStr);

try
{
conDB.Open();
scUpdate = conDB.CreateCommand();
conDB.Close();
}
catch(Exception ex)
{
MessageBox.Show("Error connecting to the database server. VMS can't run
with out database connection and VMS will close. " + ex.ToString()+
"Please contact your support person.", "VMS - Database Open Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();

}


}



/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(VMSMain));
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(80, 104);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(304, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Updating database. Please wait..............";
//
// VMSMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(496, 264);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "VMSMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "VMS - Updating Database ";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.vmsMain_Closing);
this.Load += new System.EventHandler(this.VMSMain_Load);
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{


}

private void vmsMain_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
// conDB.Close();
}

private void VMSMain_Load(object sender, System.EventArgs e)
{
string[] ATSFiles;
bool NoFiles = false;
this.Show();
Cursor.Current = Cursors.WaitCursor;
// this.Show();
// MessageBox.Show("Updating VMS database. Please
wait.....................", "Updating");

//sdaUpdate = new SqlDataAdapter();
//GET THE ats file path from the DB
try
{
conDB.Open();
scGetPath = new SqlCommand("select LogFilePath from Admin", conDB);
strATSPath = scGetPath.ExecuteScalar().ToString();
strATSPath = strATSPath.Replace(@"\", @"\\");
conDB.Close();

}
catch(Exception ex)
{
MessageBox.Show("Error getting the ATS file path : " + ex.ToString()+
"Please contact your support person.", "VMS - ATS file path error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}

//Get the latest ATS Access DB file name
try
{//@"C:\\VMS\\VMSDB"
ATSFiles = Directory.GetFiles(strATSPath, "Ats*Db.mdb");
if(ATSFiles.Length <= 0)
NoFiles = true;
else
{
NoFiles = false;
Array.Sort(ATSFiles);
NewATS= ATSFiles[ATSFiles.Length - 1];
NewATS = NewATS.Replace(@"\\", @"\");
}
}
catch(Exception ex)
{
MessageBox.Show("Error getting the ATS Access database files : " +
ex.ToString()+
"Please contact your support person.", "VMS - ATS files Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}

if (!NoFiles)
{
try
{
//Begin Transaction
// SqlTransaction DbUpdateTransaction = conDB.BeginTransaction();
//
// scUpdate.Transaction = DbUpdateTransaction;

//Save the Previous readings
// scUpdate.CommandType = CommandType.StoredProcedure;
// scUpdate.CommandText = "SavePrevOdo";
// int row = scUpdate.ExecuteNonQuery();
conDB.Open();
int row;
//Insert new vehicle from the ATS Access database
scUpdate.CommandType = CommandType.Text;
scUpdate.CommandText = "INSERT INTO VehDetail(VName, LastOdometerDate,
LastOdometerReading)"+
" SELECT [d_RemoteName],CAST(MAX([d_DateTime]) AS DATETIME),
CAST(MAX([d_OdometerTenths]) AS INT(4))"+
" FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', '" +
NewATS + "';'Admin';'', DATA)"+
" WHERE [d_RemoteName] NOT IN(SELECT VNAME FROM VehDetail)"+
" GROUP BY [d_RemoteName]";
row = scUpdate.ExecuteNonQuery();

//Update the last date the odometer was read for each vehicle
scUpdate.CommandText = "UPDATE VehDetail SET VehDetail.LastOdometerDate
= (SELECT CAST(MAX([d_DateTime]) AS DATETIME)"+
" FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','"+
NewATS + "';'Admin';'', DATA)"+
" WHERE (d_RemoteName = VehDetail.VName))";
row = scUpdate.ExecuteNonQuery();

//Update the odometer at the last date the reading was taken
scUpdate.CommandText = "UPDATE VehDetail SET
VehDetail.LastOdometerReading ="+
" (SELECT CAST(MAX([d_OdometerTenths]) AS INT(4)) FROM
OPENROWSET('Microsoft.Jet.OLEDB.4.0','"+
NewATS + "';'Admin';'', DATA)"+
" WHERE (d_RemoteName = VehDetail.VName and DATEDIFF(SS,
CAST([d_DateTime] AS DATETIME), " +
"VehDetail.LastOdometerDate)=0))";
row = scUpdate.ExecuteNonQuery();

// DbUpdateTransaction.Commit();//Commit the transaction

}
catch(Exception ex)
{
MessageBox.Show("Error updating the database from ATS Access Database :
" + ex.ToString(), "Database Update Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}
// finally
// {
//
// }

conDB.Close();

try
{
SqlCommand scRetMaint = conDB.CreateCommand();
scRetMaint.CommandType = CommandType.StoredProcedure;
scRetMaint.CommandText = "ReturnMaintDued";
conDB.Open();
scRetMaint.ExecuteNonQuery();
scRetMaint.CommandType = CommandType.Text;
scRetMaint.CommandText = "select count(*) from MaintenanceDued";
int rowCount = (int)scRetMaint.ExecuteScalar();
conDB.Close();
Cursor.Current = Cursors.Default;
if (rowCount > 0)
{
this.Hide();
Form frmMD = new MaintDued(conDB);
frmMD.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("There is no maintenance dued for any vehicle at this
time.", "VMS - No Maintenance Dued",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
Form frmVeh = new vmsHome(conDB);
frmVeh.ShowDialog();
this.Close();
}
}//end try
catch(Exception ex)
{
MessageBox.Show("Error obtaining the count for number of vehicels dued
for maintenance." + ex.ToString(), "Database Update Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
}//end catch
}// end if (!NoFiles)
else if (NoFiles)
{
MessageBox.Show("There is no ATS database files found in the directory
specified.", "VMS - No ATS database files",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
Form frmVeh = new vmsHome(conDB);
frmVeh.ShowDialog();
this.Close();
}//END else if (NoFiles)

//this.Close();
}


}
}
 
G

Guest

Ok, I found the error. I put the "this.show" at the wrong place. It was at
the end of VMSMain(). I removed it and it works fine now.

Alpha said:
I have a Window based application that shows up still running in the task
manager when I close it. It reaches the "this.close" statement and then it
stops at the "}" at the section of the following code after the
"Application.Run(new VMSMain());":
[STAThread]
static void Main()
{
Application.Run(new VMSMain());
}

I can't figure this one out at all. I would really appreciate some help
with this one.
Thanks, Alpha

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
using System.IO;

namespace VMS
{
/// <summary>
/// Summary description for VMSMain.
/// </summary>
public class VMSMain : System.Windows.Forms.Form
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
private SqlConnection conDB;
private SqlCommand scUpdate, scGetPath;
private string strATSPath, NewATS;
private System.Windows.Forms.Label label1;

[STAThread]
static void Main()
{
Application.Run(new VMSMain());
}



public VMSMain()
{
//
// Required for Windows Form Designer support
//
InitializeComponent();

//
// TODO: Add any constructor code after InitializeComponent call
//Connect to VMS DB
// string conStr = @"Server=DEVELOPMENT\NetSDK; user id=tis; password=tis;
database = VMS";
// string conStr = @"Server=WENDI; user id=tis; password=tis; database =
VMS";
string conStr = @"Server=Support\NetSDK; user id=tis; password=tis;
database = VMS";

conDB = new SqlConnection(conStr);

try
{
conDB.Open();
scUpdate = conDB.CreateCommand();
conDB.Close();
}
catch(Exception ex)
{
MessageBox.Show("Error connecting to the database server. VMS can't run
with out database connection and VMS will close. " + ex.ToString()+
"Please contact your support person.", "VMS - Database Open Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);
this.Close();

}


}



/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
System.Resources.ResourceManager resources = new
System.Resources.ResourceManager(typeof(VMSMain));
this.label1 = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.Location = new System.Drawing.Point(80, 104);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(304, 23);
this.label1.TabIndex = 0;
this.label1.Text = "Updating database. Please wait..............";
//
// VMSMain
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.BackColor = System.Drawing.SystemColors.Control;
this.ClientSize = new System.Drawing.Size(496, 264);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
this.MaximizeBox = false;
this.Name = "VMSMain";
this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
this.Text = "VMS - Updating Database ";
this.Closing += new
System.ComponentModel.CancelEventHandler(this.vmsMain_Closing);
this.Load += new System.EventHandler(this.VMSMain_Load);
this.ResumeLayout(false);

}
#endregion

private void button1_Click(object sender, System.EventArgs e)
{


}

private void vmsMain_Closing(object sender,
System.ComponentModel.CancelEventArgs e)
{
// conDB.Close();
}

private void VMSMain_Load(object sender, System.EventArgs e)
{
string[] ATSFiles;
bool NoFiles = false;
this.Show();
Cursor.Current = Cursors.WaitCursor;
// this.Show();
// MessageBox.Show("Updating VMS database. Please
wait.....................", "Updating");

//sdaUpdate = new SqlDataAdapter();
//GET THE ats file path from the DB
try
{
conDB.Open();
scGetPath = new SqlCommand("select LogFilePath from Admin", conDB);
strATSPath = scGetPath.ExecuteScalar().ToString();
strATSPath = strATSPath.Replace(@"\", @"\\");
conDB.Close();

}
catch(Exception ex)
{
MessageBox.Show("Error getting the ATS file path : " + ex.ToString()+
"Please contact your support person.", "VMS - ATS file path error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}

//Get the latest ATS Access DB file name
try
{//@"C:\\VMS\\VMSDB"
ATSFiles = Directory.GetFiles(strATSPath, "Ats*Db.mdb");
if(ATSFiles.Length <= 0)
NoFiles = true;
else
{
NoFiles = false;
Array.Sort(ATSFiles);
NewATS= ATSFiles[ATSFiles.Length - 1];
NewATS = NewATS.Replace(@"\\", @"\");
}
}
catch(Exception ex)
{
MessageBox.Show("Error getting the ATS Access database files : " +
ex.ToString()+
"Please contact your support person.", "VMS - ATS files Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}

if (!NoFiles)
{
try
{
//Begin Transaction
// SqlTransaction DbUpdateTransaction = conDB.BeginTransaction();
//
// scUpdate.Transaction = DbUpdateTransaction;

//Save the Previous readings
// scUpdate.CommandType = CommandType.StoredProcedure;
// scUpdate.CommandText = "SavePrevOdo";
// int row = scUpdate.ExecuteNonQuery();
conDB.Open();
int row;
//Insert new vehicle from the ATS Access database
scUpdate.CommandType = CommandType.Text;
scUpdate.CommandText = "INSERT INTO VehDetail(VName, LastOdometerDate,
LastOdometerReading)"+
" SELECT [d_RemoteName],CAST(MAX([d_DateTime]) AS DATETIME),
CAST(MAX([d_OdometerTenths]) AS INT(4))"+
" FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0', '" +
NewATS + "';'Admin';'', DATA)"+
" WHERE [d_RemoteName] NOT IN(SELECT VNAME FROM VehDetail)"+
" GROUP BY [d_RemoteName]";
row = scUpdate.ExecuteNonQuery();

//Update the last date the odometer was read for each vehicle
scUpdate.CommandText = "UPDATE VehDetail SET VehDetail.LastOdometerDate
= (SELECT CAST(MAX([d_DateTime]) AS DATETIME)"+
" FROM OPENROWSET('Microsoft.Jet.OLEDB.4.0','"+
NewATS + "';'Admin';'', DATA)"+
" WHERE (d_RemoteName = VehDetail.VName))";
row = scUpdate.ExecuteNonQuery();

//Update the odometer at the last date the reading was taken
scUpdate.CommandText = "UPDATE VehDetail SET
VehDetail.LastOdometerReading ="+
" (SELECT CAST(MAX([d_OdometerTenths]) AS INT(4)) FROM
OPENROWSET('Microsoft.Jet.OLEDB.4.0','"+
NewATS + "';'Admin';'', DATA)"+
" WHERE (d_RemoteName = VehDetail.VName and DATEDIFF(SS,
CAST([d_DateTime] AS DATETIME), " +
"VehDetail.LastOdometerDate)=0))";
row = scUpdate.ExecuteNonQuery();

// DbUpdateTransaction.Commit();//Commit the transaction

}
catch(Exception ex)
{
MessageBox.Show("Error updating the database from ATS Access Database :
" + ex.ToString(), "Database Update Error",
MessageBoxButtons.OK, MessageBoxIcon.Error);

}
// finally
// {
//
// }

conDB.Close();

try
{
SqlCommand scRetMaint = conDB.CreateCommand();
scRetMaint.CommandType = CommandType.StoredProcedure;
scRetMaint.CommandText = "ReturnMaintDued";
conDB.Open();
scRetMaint.ExecuteNonQuery();
scRetMaint.CommandType = CommandType.Text;
scRetMaint.CommandText = "select count(*) from MaintenanceDued";
int rowCount = (int)scRetMaint.ExecuteScalar();
conDB.Close();
Cursor.Current = Cursors.Default;
if (rowCount > 0)
{
this.Hide();
Form frmMD = new MaintDued(conDB);
frmMD.ShowDialog();
this.Close();
}
else
{
MessageBox.Show("There is no maintenance dued for any vehicle at this
time.", "VMS - No Maintenance Dued",
MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Hide();
Form frmVeh = new vmsHome(conDB);
frmVeh.ShowDialog();
this.Close();
}
}//end try
catch(Exception ex)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top