Solved

Invalid filename

Posted on 2014-01-29
8
340 Views
Last Modified: 2014-01-31
Hi,

I have this code to compact a msaccess database. But I get an error: Invalid filename. I think
I have made programming error by gettings the connectionstring of the settings.settings file and put it in a string.

What I have done wrong?

        private void bbComDb_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            string AccessConnectionString = Properties.Settings.Default.DBRootConnectionString;
            string File_Path = Application.StartupPath + "\\DBRoot.mdb";
            CompactJetDatabase(AccessConnectionString, File_Path);
            if  (CompactJetDatabase(AccessConnectionString, File_Path))
                XtraMessageBox.Show("Is goed");
            else
                XtraMessageBox.Show("Is niet goed");
        }

        public static bool CompactJetDatabase(string connectionString, string fileName)
        {
            try
            {
                string oldFileName = fileName;
                string newFileName = Path.Combine(Path.GetDirectoryName(oldFileName),
                Guid.NewGuid().ToString("N") + ".mdb");
                JRO.JetEngine objJet = new JRO.JetEngine();
                objJet.CompactDatabase(String.Format(connectionString, oldFileName),
                 String.Format(connectionString, newFileName));
                File.Delete(oldFileName);
                File.Move(newFileName, oldFileName);
                return true;
            }
            catch (Exception error)
            {
                XtraMessageBox.Show(error.ToString()); 
                return false;
            }
        }

Open in new window


Greetings, Peter Kiers
0
Comment
Question by:peterkiers
  • 4
  • 3
8 Comments
 
LVL 29

Accepted Solution

by:
fibo earned 400 total points
ID: 39817460
Some things I would start with first:
- I presume that the double \ is required, but this would me my first suspect
- you are first calling the function, then make another call in your test... I would start by commenting out the first call, just in case calling the function twice in a row creates some unexpected side-effect
- what is the expected result from String.Format(connectionString, newFileName)?
- I would suspect string newFileName = Path.Combine(Path.GetDirectoryName(oldFileName),  Guid.NewGuid().ToString("N") + ".mdb"); to return a wrong result (eg, .mdb present twice with unexpected results) and would probably make a debug print of this value
- and finally, I would also check that I have the rights to create/ write a file in the target directory
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39817522
Oke, calling the function twice was dumb of me.
But still got the error.

P.
0
 
LVL 29

Expert Comment

by:fibo
ID: 39817532
- what gives the display of the filename?
- can you test within your program if you can create this file? (ie, if you programs has the right rights)?
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 1

Author Comment

by:peterkiers
ID: 39818118
I have to go. I'll be back at approx 22:30
P.
0
 
LVL 79

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 100 total points
ID: 39819427
put a breakpoint in line 19 and check the variables
0
 
LVL 1

Author Comment

by:peterkiers
ID: 39820162
The variable:

ConnectionString = "Provider=MicrosoftJet.OLEDB.4.0;Data Source=|DataDirectory|\\DBRoot.mdb
oldFileName = c:\\Users\\Peter\\Documents\\MyProject\\Diabetes Manager\\bin\\Debug\\DBRoot.mdb
newFileName = c:\\Users\\Peter\\Documents\\MyProject\\Diabetes Manager\\bin\\Debug\\12d95a43bab54e2aad6c25a6a8e44df9.mdb

Open in new window

0
 
LVL 1

Author Comment

by:peterkiers
ID: 39820209
GOT IT.

        private void bbComDb_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = (Properties.Settings.Default.DBRootConnectionString);
            conn.Close();
            if (CompactJetDatabase())
                XtraMessageBox.Show("Is goed");
            else
                XtraMessageBox.Show("Is niet goed");
            conn.Open();
        }

        public const string AccessConnectionString =
        "Data Source={0};Provider=Microsoft.Jet.OLEDB.4.0;";

        public static bool CompactJetDatabase()
        {
            try
            {
                string oldFileName = Application.StartupPath + "\\DBRoot.mdb";
                string newFileName = Application.StartupPath + "\\temp.mdb";
                JRO.JetEngine engine = new JRO.JetEngine();
                engine.CompactDatabase(String.Format(AccessConnectionString, oldFileName),
                String.Format(AccessConnectionString, newFileName));
                File.Delete(oldFileName);
                File.Move(newFileName, oldFileName);
                return true;
            }
            catch (Exception ex)
            {
                XtraMessageBox.Show(ex.Message);
                return false;
            }
        }

Open in new window


Greetings, Peter Kiers
0
 
LVL 29

Expert Comment

by:fibo
ID: 39823642
B-) glad we could help. Thx for the grade and points!
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Flash (http://en.wikipedia.org/wiki/Adobe_Flash) has evolved over the years to where it has become a masterful tool for displaying content screen.  It has excellent layout placement, UI precision as well as rendering capabilities. This, along with t…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question