1

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);
        }
    }
C_sharp
  • 408
  • 5
  • 26
  • Call your code that checks if the file exists **before** you actually save it and prompt the user for confirmation. – James Sep 12 '12 at 13:09

1 Answers1

4

You have not provided any code so I am left wondering.

But are you using SaveFileDialog? If not, I highly suggest it. It provides that functionality.

SaveFileDialog sfd = new SaveFileDialog();
sfd.OverwritePrompt = true;
if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    // Do Something
    // Access the filename they choose via: ofd.FileName
}

And if the user selects a file that exists, they will be asked if they are sure they want to overwrite it.

SaveFileDialog also has several properties you can define. Such as the Filter property for defining acceptable file extensions.

Michael Mankus
  • 4,628
  • 9
  • 37
  • 63
  • 1
    What is the value of SaveFile.OverwritePrompt? It should be set to true. That is the property which will cause the behavior you are looking for. Overwise I must not fully understand what you are looking for. – Michael Mankus Sep 12 '12 at 14:34
  • I just set the value of SaveFile.OverwritePrompt= true; and now its working!! – C_sharp Sep 12 '12 at 14:41