I am trying to write some information to an access database using OleDB but I get an error. I dont know what is wrong with it. I it something to do with my SQL code?

// The program will make use of a text file for now but must use ms access database in the future.
            string name = textBoxFirstName.Text;
            string lname = textBoxLastName.Text;
            string gender = comboBoxGender.Text;
            string id = textBoxID.Text;
            string province = comboBoxProvince.Text;
            string city = textBoxCity.Text;
            string addr = textBoxAddress.Text;
            string field = textBoxField.Text;
            string degree = textBoxDegree.Text;

            if ((name == "") || (lname == "") || (gender == "") || (id == "") || (province == "") || (city == "")
               || (addr == "") || (field == "") || (degree == ""))
            {
                MessageBox.Show("Please make sure all the fields are filled in!");
                return;
            }

            OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\User\Documents\Databases\employeedb.accdb");
            conn.Open();
            using(OleDbCommand cmd = new OleDbCommand())
            {
                cmd.Connection = conn;
                cmd.CommandText = "Insert Into Employee (fname, lname, gender, iden, province, city, addr, field, degree)" 
                                  + " values (" + name + ", " + lname + ", " + gender + ", " + id + ", " + province + ", " 
                                  + city + ", " + addr + ", " + field + ", " + degree + ")";
                cmd.ExecuteNonQuery();
            }

            conn.Close();

The runtime error happens at cmd.ExecuteNonQuery(); how do I fix this problem?

When I put it in a try and catch statement it says: No value given for one ore more required paramaters

But I checked that all my variables get their values from the text boxes.

Try surrounding string values with single quotes. Or better yet, use parameters. Search for parameterized queries.

Edited 2 Years Ago by cgeier

This question has already been answered. Start a new discussion instead.