Home > Back-end >  i cant connect my database with my c# windows form app i get "unhandled exception" error
i cant connect my database with my c# windows form app i get "unhandled exception" error

Time:04-23

so I am trying to connect my Microsoft access database with my visual studio windows form app in c# so that it would display one of the tables from the database and I have ran into a problem that states "Exception Unhandled" and the error is: System.Configuration.ConfigurationErrorsException. I know what exceptions are in c# but I don't know where to put the exception so if anyone knows where please feel free to copy my code and change it so that there will be exceptions. here is the form code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.OleDb;

namespace AccessBD_PT2022
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }
        private DataTable getTable1()
        {
            DataTable dtSwitchboard = new DataTable();

            string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;

            using (OleDbConnection con = new OleDbConnection(connString))
            {
                using (OleDbCommand cmd = new OleDbCommand("SELECT * FROM Switchboard Items", con))
                {
                    con.Open();

                    OleDbDataReader reader = cmd.ExecuteReader();

                    dtSwitchboard.Load(reader);
                }
            }

            return dtSwitchboard;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            dataGridView1.DataSource = getTable1();
        }
    }
}

and heres also the app.config code that I needed to additionally change a bit too:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
    </configSections>
    <connectionStrings>
        <add name="AccessBD_PT2022.Properties.Settings.PP2022ConnectionString"
            connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\PP2022.accdb;Jet OLEDB:Database Password=123"
            providerName="System.Data.OleDb" />
    </connectionStrings>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
  <connectionString>
    <add name="dbx" connectionstring="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\user\Desktop\PP2022.accdb" providerName="System.Data.OleDb"/>
  </connectionString>
</configuration>

CodePudding user response:

You are getting error on this line:

string connString = ConfigurationManager.ConnectionStrings["dbx"].ConnectionString;

The reason is that, you have inccorect tags in your app config file for connection string.

<connectionString> or <connectionStrings> should be replaced with <ConnectionStrings>

CodePudding user response:

You have a number of mistakes in your config file. First, you need to declare the connectionStrings section in the configSections. Then, you need to put your connection strings inside of connectionStrings. And finally, the elements are case sensitive so you have spell them exactly right.

Compare this with what you are currently using:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <configSections>
        <section name="connectionStrings" type="System.Configuration.ConnectionStringsSection, System.Configuration, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" requirePermission="false"/>
    </configSections>
    <connectionStrings>
        <add name="AccessBD_PT2022.Properties.Settings.PP2022ConnectionString"
            connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=|DataDirectory|\PP2022.accdb;Jet OLEDB:Database Password=123"
            providerName="System.Data.OleDb" />

        <add name="dbx" connectionString="Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\user\Desktop\PP2022.accdb" providerName="System.Data.OleDb"/>
    </connectionStrings>

    <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
    </startup>
</configuration>
  • Related