Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Invalid filename

Posted on 2014-01-29
8
Medium Priority
?
348 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 1600 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
RHCE - Red Hat OpenStack Prep Course

This course will provide in-depth training so that students who currently hold the EX200 & EX210 certifications can sit for the EX310 exam. Students will learn how to deploy & manage a full Red Hat environment with Ceph block storage, & integrate Ceph into other OpenStack service

 
LVL 1

Author Comment

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

Assisted Solution

by:David Johnson, CD, MVP
David Johnson, CD, MVP earned 400 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

688 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