Atif Ul Islam
Atif Ul Islam

Reputation: 11

Microsoft Access Engine

I'm trying to add data from Visual Studio to Access in C#. Every time I click the button to save the data an error message pops up saying "Microsoft Database Engine". I have no clue where the problem is. I pasted the code below:

    private void btnsave_Click(object sender, EventArgs e)
    {

        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\My Monroe\Semester 5\Advanced Programming\Final Project\WindowsFormsApplication1\WindowsFormsApplication1\Final exam .accdb";
        string fname = first_NameTextBox.Text;
        string lname = last_NameTextBox.Text;
        string snum = sSNTextBox.Text;
        string city = cityTextBox.Text;
        string state = stateTextBox.Text;
        string telnum = telephone__TextBox.Text;
        OleDbCommand cmd = new OleDbCommand("INSERT into Customers(First Name, Last Name, SSN,City,State,Telephone# )" + " values(@fname,@lname,@snum,@city,@state,@telnum)", connect);
        cmd.Connection = conn;
        conn.Open();
        if (conn.State == ConnectionState.Open)
        {
            cmd.Parameters.Add("@fname", OleDbType.Char, 20).Value = fname;
            cmd.Parameters.Add("@lname", OleDbType.Char, 20).Value = lname;
            cmd.Parameters.Add("@snum", OleDbType.Numeric, 20).Value = snum;
            cmd.Parameters.Add("@city", OleDbType.Char, 20).Value = city;
            cmd.Parameters.Add("@state", OleDbType.Char, 20).Value = state;
            cmd.Parameters.Add("@telnum", OleDbType.Numeric, 20).Value = telnum;
            try
            {
                cmd.ExecuteNonQuery();
                MessageBox.Show("Data Added");
                conn.Close();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Source);
                conn.Close();
            }
        }
        else
        {
            MessageBox.Show("Connection Failed");
        }
    }

Upvotes: 0

Views: 282

Answers (1)

Jonathan Willcock
Jonathan Willcock

Reputation: 5235

A few things to check. Firstly change the catch to

MessageBox.Show(ex.Message);

This will be much more informative!

Secondly on which line does the error get thrown? Thirdly, please check your connection string. When I attach to access my string is always of the form:

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DBFullPath\DBName.accdb;Jet OLEDB:Engine Type=5;Persist Security Info=False;"

if there is no password or

@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=DBFullPath\DBName.accdb;Jet OLEDB:Engine Type=5;Jet OLEDB:Database Password = password;"

if there is one.

Finally, do you really have a telephone field as numeric? What happens with numbers that start with 0 or international ones with +?

EDIT

Sorry I think you misunderstood me. What I wanted you to do, was to amend the catch so that it reads (in full):

catch (OleDbException ex)
{
    MessageBox.Show(ex.Message);
    conn.Close();
}

Upvotes: 1

Related Questions