SaveFileDialog changes connectionString!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Bassem
    Contributor
    • Dec 2008
    • 344

    SaveFileDialog changes connectionString!

    Hello,

    I'm facing something odd with ConnectionStrin g; when I use a SaveFileDialog the connection string changes to the location I've chosen in the SaveFileDialog.

    Here is how I implement that:
    Code:
            static OleDbConnection connection = new OleDbConnection
                (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\db1.mdb;Persist Security Info=True");
    Anyone has an idea?

    Thanks in advanced!
  • Bassem
    Contributor
    • Dec 2008
    • 344

    #2
    I'm sorry I forgot to post what gives the exception and the exception itself!

    Here is:
    Code:
                try
                {
                    OleDbDataAdapter adapt = new OleDbDataAdapter(commandText, connection);
                    DataSet ds = new DataSet();
                    adapt.Fill(ds);
                    return ds;
                }
                catch
                {
                    connection.Close();
                }
    It breaks on adapt.Fill(ds); and the exception message:
    "Could not find file 'C:\Documents and Settings\Mr. Bassem\Desktop\ db1.mdb'."


    Thank you for your time!

    Comment

    • Dheeraj Joshi
      Recognized Expert Top Contributor
      • Jul 2009
      • 1129

      #3
      Whether "db1.mdb" file is placed in "C:\Documen ts and Settings\Mr. Bassem\Desktop" folder?

      I suspect after you are done with SaveFileDialogu e your app still points to the same location and try to get that .mdb file from there.

      Regards
      Dheeraj Joshi

      Comment

      • Bassem
        Contributor
        • Dec 2008
        • 344

        #4
        Thank you Dheeraj,

        My database is stored as exactly I created the connection string in the application start up path, exactly where my exe is.

        You are right, for some reason the application points to where the SaveFileDialog points, but I checked the connection string and it is the same as I initialized (never changed).

        I think I don't know if that bug is related or not to the connection string!


        Thank you for your help!

        Comment

        • Bassem
          Contributor
          • Dec 2008
          • 344

          #5
          Hi I found the problem and fixed it.

          in my command text I typed: "db1.table1 " in a select query, when I changed it to "table1" and removed "db1" everything worked fine.

          I wonder what happened there!!!

          Anyone has an idea?!

          Thanks for your help!

          Comment

          • Plater
            Recognized Expert Expert
            • Apr 2007
            • 7872

            #6
            Well SaveFileDialog will change your working directory I think. (I also believe there is a property you can set so that it does NOT do that)
            So it might have something to do with using relative paths in your connection string?

            Comment

            • Bassem
              Contributor
              • Dec 2008
              • 344

              #7
              Thanks Plater,

              Yes, I think you have defined the problem well.

              That property should be related to the application, right?

              Comment

              • Plater
                Recognized Expert Expert
                • Apr 2007
                • 7872

                #8
                I believe it is the RestoreDirector y property on your SaveFileDialog

                Comment

                Working...