I have an application in which the user interacts with a database and prepares a report of what he needs. The item prices are listed in the database. Once he chooses the items then he generates a report in Excel format. When he hits the Generate a Report button, he is asked where to save the report. These are all good. The problem is that when he saves the report and hits the Generate A Report button, he is able to save the same file with the same name at the same location when a file of the same name exists at that location. It basically replaces the old one. I wrote a code to check for the file if it exists and it works but the problem is that it does not appear at the instant when you are saving the file. It appears before. What I want is when I try to save the file at any location, it gives me at that instant that "The file exists and do you want to replace it with it", more like Windows dialog when you try to save a word document into a location where there is a document with the same name.
If anyone needs any clarifications please comment, I am online most of the times.
Here is the code
private void btnRunReport_Click(object sender, EventArgs e)
{
if (cmbReportsList.SelectedIndex != -1)
{
_qry = new QueryMgt();
OpenProjectDb();
string _sProjectName = (dbManager.ExecuteScalar(CommandType.Text, _qry.GetProjectFileName())).ToString();
CloseProjectDb();
if (cmbReportsList.SelectedIndex == 0)
{
SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx";
SaveFile.DefaultExt = "xlsx";
SaveFile.FileName = _sProjectName + " Report1";
strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }
btnRunReport.Visible = false;
bg1804Rpt.RunWorkerAsync();
Application.DoEvents();
}
}
else if (cmbReportsList.SelectedIndex == 1)
{
SaveFile.FileName = _sProjectName + " Report2";
SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx";
SaveFile.DefaultExt = "xlsx";
if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//SaveFile.FileName = "Report2";
this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }
btnRunReport.Visible = false;
bgMCRpt.RunWorkerAsync();
Application.DoEvents();
}
}
else if (cmbReportsList.SelectedIndex == 2)
{
_qry = new QueryMgt();
_formula = new FormulaMgt();
string sSQL;
string chrCountryName = "";
OpenProjectDb();
OpenTableDb();
DataSet dsProjectSpec;
DataSet dsCountry;
sSQL = _qry.GetProjectSpec().ToString();
dsProjectSpec = dbManager.ExecuteDataSet(CommandType.Text, sSQL);
sSQL = _qry.GetCountry().ToString();
dsCountry = dbManagerTable.ExecuteDataSet(CommandType.Text, sSQL);
foreach (DataRow row in dsCountry.Tables[0].Select("pk_lngCountryID ='" + dsProjectSpec.Tables[0].Rows[0]["lngCountryId"] + "'"))
{
chrCountryName = row["chrCountryName"].ToString();
break;
}
SaveFile.FileName = _sProjectName + " Report3";
SaveFile.Filter = "excel 2007 (*.xlsx)|*.xlsx";
SaveFile.DefaultExt = "xlsx";
// SaveFile.Filter = "Microsoft Office Excel Worksheet (*.xlsx)|*.xls|All files (*.*)|*.*";
if (SaveFile.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
//SaveFile.FileName = "Report3";
this.Text = System.IO.Path.GetFileName(SaveFile.FileName);
strFilepath = System.IO.Path.GetFullPath(SaveFile.FileName);
if (strFilepath.Length > 218) { strFilepath = StringMgt.Left(strFilepath, 218); }
btnRunReport.Visible = false;
bgMDRpt.RunWorkerAsync();
Application.DoEvents();
}
}
}
else if (cmbReportsList.SelectedIndex == -1)
{
MessageBox.Show("Please select any report", "Info", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
}