Solved

Invalid filename

Posted on 2014-01-29
8
345 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Use Filtering Commands to Process Files in Linux

Learn how to manipulate data with the help of various filtering commands such as `cat`, `fmt`, `pr`, and others in Linux.

 
LVL 1

Author Comment

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

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 Install Bash on Windows 10

Windows’ budding partnership with Canonical has certainly led to some great improvements. One of them being the ability to use Bash on your Windows machine without third party applications! This might be one of the greatest things a cloud engineer in a Windows environment can do!

Question has a verified solution.

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

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…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

623 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