The c# windows app with Access database (accdb) doesn't work on other computers after installation

0

I want to insert a text in my Access database (accdb) in C#-windows application project. The "accdb" . It works correctly on my computer ONLY if i run the exe file which situated in my project source folder but the problem is that when i build a setup file and installed it and run the software, it opens but when i click on the insert button, it couldn't work. The problem is with the database (location/access) but i dont't know how can i solve it. Does anyone know how to solve this problem?

The error is: Unhandled exception has occurred in your application. ... "Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately. See the end of this message for details on invoking just-in-time (JIT) debugging instead of this dialog box.

************** Exception Text ************** System.Data.OleDb.OleDbException (0x80004005): Operation muss eine aktualisierbare Abfrage verwenden. at System.Data.OleDb.OleDbCommand.ExecuteCommandTextErrorHandling(OleDbHResult hr) at System.Data.OleDb.OleDbCommand.ExecuteCommandTextForSingleResult(tagDBPARAMS dbParams, Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommandText(Object& executeResult) at System.Data.OleDb.OleDbCommand.ExecuteCommand(CommandBehavior behavior, Object& executeResult)

enter image description here enter image description here

Here is my code:

 public Form1()
    {
        InitializeComponent();
    }

    public static string GetDBConnection()
    {
        try
        {
            string dbExecPath = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "Test11.accdb");
            return $"Provider=Microsoft.ACE.OLEDB.12.0;Data Source={ dbExecPath }";
        }
        catch (Exception)
        {
            return string.Empty;
        }
    }

   OleDbConnection con = new OleDbConnection(GetDBConnection());
   OleDbCommand cmd;

    private void button1_Click(object sender, EventArgs e)
    {

        OleDbCommand cmd = new OleDbCommand("insert into[Table](name, code) VALUES(@name, @code)", con);
            cmd.CommandType = CommandType.Text;

            cmd.Parameters.AddWithValue("@name", textBox1.Text);
             cmd.Parameters.AddWithValue("@code", textBox2.Text);

            con.Open();

            System.Windows.Forms.MessageBox.Show("An Item has been successfully added", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

        int i = 0;
        i = cmd.ExecuteNonQuery();
        if (i > 0)
        {
            MessageBox.Show("Inserted");
            DisplayData();
        }

        else
        {
            MessageBox.Show("Not Inserted");
        }
        con.Close();
        ///////
    }
c#
database
database-connection
oledbconnection
asked on Stack Overflow Mar 10, 2020 by arash6657 • edited Mar 10, 2020 by arash6657

1 Answer

1

In your code you use Assembly.getExecutingAssembly().Location to specify the location of you .accdb-file. As soon as you change the location of your executable (.exe) the value that Assembly.getExecutingAssembly().Location returns changes (See MS docs). So you need to make sure your executable and your .accdb-file are always in the same folder. Otherwise a connection to your Access database cannot be established.

You could add a message box that shows the path your program expects the database to be located. Afterwards you can check whether your database really is located in the right directory.

answered on Stack Overflow Mar 10, 2020 by Erik T.

User contributions licensed under CC BY-SA 3.0