1

It doesn't have an error, but it has a message box showing Ms Access Database Engine and data not insert to database. Can anyone help me solve the problem??

namespace WindowsFormsApplication1
{
    public partial class SignUp : Form
    {
        public SignUp()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Junz\Documents\Register - Copy.mdb";
            conn.Open();
            String Username = textBox1.Text;
            String Password = textBox2.Text;
            String Email = textBox3.Text;
            String Address = textBox4.Text;

            OleDbCommand cmd = new OleDbCommand("INSERT INTO Register(Username,Password,Email,Address) Values(@Username, @Password,@Email,@Address)");
            cmd.Connection = conn;

            if (conn.State == ConnectionState.Open)
            {
                cmd.Parameters.Add("@Username", OleDbType.VarChar,20).Value = Username;
                cmd.Parameters.Add("@Password", OleDbType.VarChar,20).Value = Password;
                cmd.Parameters.Add("@Email", OleDbType.VarChar,20).Value = Email;
                cmd.Parameters.Add("@Address", OleDbType.VarChar,20).Value = Address;

                try
                {
                    cmd.ExecuteNonQuery();
                     MessageBox.Show("DATA ADDED");
                       conn.Close();
                }
                catch (OleDbException ex)
                {
                    MessageBox.Show(ex.Source);
                    conn.Close();
                }
            }
            else
            {
                MessageBox.Show("Connection Failed");
            }
        }
    }
}
1
  • If you want to find out the details of the exception, then use ex.ToString(). Commented Dec 5, 2013 at 4:59

3 Answers 3

1

ExecuteNonQuery() will return int value representing number of rows updated into Database.

if the returned value is 0 you can Display a Message saying DATA NOT ADDED

Try This: write your try block as below.

   try
   {
        if(cmd.ExecuteNonQuery()>0)
         MessageBox.Show("DATA ADDED");
        else
         MessageBox.Show("DATA NOT ADDED");

        conn.Close();
    }
Sign up to request clarification or add additional context in comments.

2 Comments

No, I don't know which part of my program went wrong.It just keep showing Ms Access Database Engine in the messagebox when i click the register button.
@user3068582: could you please attach screenshot?
1

how about this hope it helps

private void button1_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection();
        conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Junz\Documents\Register - Copy.mdb";
        conn.Open();
        String Username = textBox1.Text;
        String Password = textBox2.Text;
        String Email = textBox3.Text;
        String Address = textBox4.Text;

        conn.Open();
        cmd.Connection = conn;
        cmd.CommandType = CommandType.Text;
        cmd.CommandText = "INSERT INTO Register(Username,Password,Email,Address) Values(@Username, @Password,@Email,@Address)";


        if (conn.State == ConnectionState.Open)
        {
            cmd.Parameters.Add("@Username", OleDbType.VarChar,20).Value = Username;
            cmd.Parameters.Add("@Password", OleDbType.VarChar,20).Value = Password;
            cmd.Parameters.Add("@Email", OleDbType.VarChar,20).Value = Email;
            cmd.Parameters.Add("@Address", OleDbType.VarChar,20).Value = Address;

            try
            {
                cmd.ExecuteNonQuery();
                 MessageBox.Show("DATA ADDED");
                   conn.Close();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Source);
                conn.Close();
            }
        }
        else
        {
            MessageBox.Show("Connection Failed");
        }
    }

Comments

0

Add this to your catch

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

And try to insert again, maybe u can see the exact error now.

Good luck.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.