Solved

Invalid filename

Posted on 2014-01-29
8
338 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
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 78

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

In my previous article (http://www.experts-exchange.com/Programming/Languages/.NET/.NET_Framework_3.x/A_4362-Serialization-in-NET-1.html) we saw the basics of serialization and how types/objects can be serialized to Binary format. In this blog we wi…
Today I had a very interesting conundrum that had to get solved quickly. Needless to say, it wasn't resolved quickly because when we needed it we were very rushed, but as soon as the conference call was over and I took a step back I saw the correct …
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video discusses moving either the default database or any database to a new volume.

760 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

17 Experts available now in Live!

Get 1:1 Help Now