Solved

Invalid filename

Posted on 2014-01-29
8
339 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
 
LVL 1

Author Comment

by:peterkiers
ID: 39818118
I have to go. I'll be back at approx 22:30
P.
0
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

867 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now