Question

Here is my code to insert data into a database.

code:

private void button2_Click(object sender, EventArgs e)
{
//Insert Button
string connString = "Provider=Microsoft.ACE.OLEDB.12.0;" +
"Data Source = C:\project\patientDB.accdb";
OleDbConnection connection = new OleDbConnection(connString);
OleDbCommand cmd = connection.CreateCommand();

cmd.CommandText = "insert into Medical ([PatientID],[GeneralMedicalHistoryID],[Education] values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "' )";

connection.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Record Inserted");
connection.Close();
}

However it says syntax error in INSERT statement when I try to add. What am I doing wrong.

Here is a screenshot of the fields in the table.

Modica ntID -Maritaistatu:-Education . GrowthAndC Pregnancies ㅜ BehavioralHi- Tobacco -TobaccoHRF-TobaccoQ TobaccoHRF Tobaccoa New

0 0
Add a comment Improve this question Transcribed image text
Answer #1

You are missing to open connection before insert query .

you are calling "connection.Open();" after insert query. Remove from that place and place that code before insert query in same code.

Add a comment
Know the answer?
Add Answer to:
Here is my code to insert data into a database. code: private void button2_Click(object sender, EventArgs...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT