0

Here is the code:

 string str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:/Users/charlyn_dale/Documents/Visual Studio 2010/Projects/LMS/WindowsFormsApplication2/Accounts.accdb;Persist Security Info=False");
            OleDbCommand conn = new OleDbCommand(str);
            con.Open();
            string query  = "insert into Account ([Username],[Password],FirstName,MiddleName,LastName,Age,Section,Gender,Address,AccountStatus) values('" + txt1.Text + "','" + txt2.Text + "','" + txt4.Text + "','" + txt5.Text + "','" + txt6.Text + "','" + txt7.Text + "','" + txt8.Text + "','" + cmb2.Text + "','" + txt9.Text + "','" + cmb1.Text + "')";
            OleDbCommand cmd = new OleDbCommand(query, con);
            conn.ExecuteNonQuery();
            MessageBox.Show("Registration Success!");
            con.Close();

and the error is:

Connection property has not been initialized

Prashanth Benny
  • 1,523
  • 21
  • 33

1 Answers1

0

There are 3 main issues in your Access DB connection:

  1. OleDbConnection connection string property has not initialized when opening OLE DB connection (note that con is different from conn in this context).

  2. The connection string wrongly assigned to variable conn which declared as OleDbCommand, use OleDbConnection instead.

  3. The connection string data source path seems invalid by using slash sign for directory separator (assuming target file exists in Windows folder), use backslash escape sequence (\\) or single backslash with literal string instead (e.g. @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\......").

Hence, the correct connection sequence should be like this:

string str = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\Users\\charlyn_dale\\Documents\\Visual Studio 2010\\Projects\\LMS\\WindowsFormsApplication2\\Accounts.accdb;Persist Security Info=False");

using (OleDbConnection conn = new OleDbConnection(str))
{
    conn.Open();

    // security tips: better use parameter names to prevent SQL injection on queries
    // and put value checking method for all textbox values (sanitize input)
    string query = "insert into Account ([Username],[Password],FirstName,MiddleName,LastName,Age,Section,Gender,Address,AccountStatus) values ('" + txt1.Text + "','" + txt2.Text + "','" + txt4.Text + "','" + txt5.Text + "','" + txt6.Text + "','" + txt7.Text + "','" + txt8.Text + "','" + cmb2.Text + "','" + txt9.Text + "','" + cmb1.Text + "')";
    using (OleDbCommand cmd = new OleDbCommand(query, conn))
    {
        conn.ExecuteNonQuery();
    }
    ... // other stuff
    conn.Close();
}

NB: using statements added due to OLE DB connection should be disposed immediately after usage to free up resources.

Similar issues:

get an error as ExecuteNonQuery:Connection property has not been initialized

ExecuteNonQuery: Connection property has not been initialized (access database)

ExecuteNonQuery: Connection property has not been initialized

Community
  • 1
  • 1
Tetsuya Yamamoto
  • 24,297
  • 8
  • 39
  • 61