am trying to insert data into Access 2007 using C#, i got some code from stackoverflow and it is really helping to insert data to a single table but i was trying to insert data into two tables which has one to many relationship and the code will run, it has no warring or error but when i click insert button there will be some message box which tells me failed due toSyntax error in insert into statement.can some one fix this thing??? other i have some buttons on form2 such us update,search and when i click one of this button the window will be open n the previous window will not get closed if i click buttons 10 time thr will be ten windows .... pleas am new to this language n i need some help with the codes.
here is my code...
namespace WindowsFormsApplication1
{
public partial class Form2 : Form
{
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
Form3 f3 = new Form3();
f3.ShowDialog();
}
private void button3_Click(object sender, EventArgs e)
{
Form4 f4 = new Form4();
f4.ShowDialog();
}
private void button4_Click(object sender, EventArgs e)
{
Form1 f1 = new Form1();
f1.Show();
}
private void button1_Click(object sender, EventArgs e)
{
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data source= C:\Users\user\Desktop\crt_db.accdb";
try
{
conn.Open();
String Name = txtName.Text.ToString();
String AR = txtAr.Text.ToString();
String Wereda = txtWereda.Text.ToString();
String Kebele = txtKebele.Text.ToString();
String House_No = txtHouse.Text.ToString();
String P_O_BOX = txtPobox.Text.ToString();
String Tel = txtTel.Text.ToString();
String Fax = txtFax.Text.ToString();
String Email = txtEmail.Text.ToString();
String Item = txtItem.Text.ToString();
String Dep = txtDep.Text.ToString();
String Remark = txtRemark.Text.ToString();
String Type = txtType.Text.ToString();
String Brand = txtBrand.Text.ToString();
String License_No = txtlicense.Text.ToString();
String Date_issued = txtDate.Text.ToString();
String my_querry = "INSERT INTO crtPro(Name,AR,Wereda,Kebele,House_No,P_O_Box,Tel,Fax,Email,Item,Dep,Status,Remark,)VALUES('" + Name + "','" + AR + "','" + Wereda + "','" + Kebele + "','" + House_No + "','" + P_O_BOX + "','" + Tel + "','" + Fax + "','" + Email + "','" + Item + "','" + Dep + "','" + Remark + "')" +
"AND INSERT INTO crtItemLicense (" +
"Type,Brand,License_No,Date_issued) VALUES('" + Type + "','" + Brand + "','" + License_No + "','" + Date_issued + "') ";
OleDbCommand cmd = new OleDbCommand(my_querry, conn);
cmd.ExecuteNonQuery();
MessageBox.Show("Data saved successfuly...!");
}
catch (Exception ex)
{
MessageBox.Show("Failed due to" + ex.Message);
}
finally
{
conn.Close();
}
}
}
}