I am inserting data to access a 2000-2003 format database using C #. When I had a database with 2 fields, the query works fine, but when there are more fields, it doesn't work.
I have the same code for both, and I can not find the problem.
using System.Data.OleDb; // By using this namespace I can connect to the Access Database. namespace WindowsFormsApplication1 { public partial class Form1 : Form { private OleDbConnection myconn; public Form1() { InitializeComponent(); myconn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\leelakrishnan\Desktop\NewManageContacts.mdb"); } private void Form1_Load(object sender, EventArgs e) { // TODO: This line of code loads data into the 'newManageContactsDataSet.Contacts' table. You can move, or remove it, as needed. // this.contactsTableAdapter.Fill(this.newManageContactsDataSet.Contacts); // TODO: This line of code loads data into the 'newManageContactsDataSet.Contacts' table. You can move, or remove it, as needed. this.contactsTableAdapter.Fill(this.newManageContactsDataSet.Contacts); } private void button1_Click(object sender, EventArgs e) { OleDbCommand cmd = new OleDbCommand(); cmd.CommandType = CommandType.Text; // string query = "insert into Contacts (fname,lname,llnum,mobnum,e-mail,street,city,country) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')"; cmd.CommandText = @"insert into Contacts (fname,lname,llnum,mobnum,e-mail,street,city,country) values ('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "')"; cmd.Connection = myconn; myconn.Open(); cmd.ExecuteNonQuery(); System.Windows.Forms.MessageBox.Show("User Account Succefully Created", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information); myconn.Close(); } private void button2_Click(object sender, EventArgs e) { textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; textBox5.Text = ""; textBox6.Text = ""; textBox7.Text = ""; textBox8.Text = ""; } private void textBox1_TextChanged(object sender, EventArgs e) { } } }
This is the code for a table with two fields
public partial class Form1 : Form { private OleDbConnection myCon; public Form1() { InitializeComponent(); myCon = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\Users\leelakrishnan\Desktop\Database1.mdb"); } private void Form1_Load(object sender, EventArgs e) {