hi
i am new to .net and i am try to Inserting data into ms access database 2007 from visual studio 2005 using c#.net.
i got an error OleDbException was unhanded. Syntax error in INSERT INTO statement.
my code is here
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
using Microsoft.VisualBasic;
namespace WindowsApplication2
{
public partial class Form4 : Form
{
private OleDbConnection cn1;
private OleDbCommand cmd;
private string sql;
private OleDbDataReader reader;
int cnt = 0;
public Form4()
{
cn1 = new OleDbConnection();
cn1.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=c:\\Users\\hp\\Links\\Documents\\proposed system database\\new ATM Embed Database.mdb";
cn1.Open();
InitializeComponent();
}
private void savebtn_Click(object sender, EventArgs e)
{
Int32 Account_No, PIN_No;
Account_No = Int32.Parse(AccounttextBox.Text);
PIN_No = Int32.Parse(PINtextBox.Text);
sql = "Insert into ATM(C_Name,Account_No,Card_No,PIN_No)values('" + NametextBox.Text + "','" + AccounttextBox.Text + "','" + CardtextBox.Text + "','" + PINtextBox.Text + "',)";
cmd = new OleDbCommand(sql, cn1);
int n =cmd.ExecuteNonQuery();
if (n > 0)
{
MessageBox.Show("Record Has Been Saved !!");
AccounttextBox.Text = "" + (Account_No + 1);
NametextBox.Text = "";
CardtextBox.Text = "";
PINtextBox.Text = "";
}
else
{
MessageBox.Show("Record Not Saved !!");
}
cn1.Close();
}
}
}
Please help me.
suggest me solution of this problem.

SL GPosted May 8, 2015, 7:56 AM
Connect Ms Access Database & insert Values & Step by Step Explain
http://dotnetdrizzles.blogspot.in/2015/03/msaccess-database-connection-to-visual.html
Step By Step SCREENSHOTS
http://screenshotdrizzles.blogspot.in/2015/03/msaccess-database-connection-to-visual.html
Manoj BhoirPosted May 8, 2015, 8:04 AM
sql = "Insert into ATM(C_Name,Account_No,Card_No,PIN_No)values('" + NametextBox.Text + "','" + AccounttextBox.Text + "','" + CardtextBox.Text + "','" + PINtextBox.Text + "',)";
Also,
Try to use AddWithValues while inserting data into database.
Dipankar BiswasPosted May 8, 2015, 1:06 AM
try it...
private void insert_mark()
{
if (connectiondb.State == ConnectionState.Closed)
{
connectiondb.Open();
}
str = "insert into tbl_result (Main_id,SName,sub,test_no,mark,Mrk_R,Mrk_W,Exam_date) values (@Main_id,@SName,@sub,@test_no,@mark,@Mrk_R,@Mrk_W,@Exam_date)";
com = new OleDbCommand(str, connectiondb);
com.Parameters.AddWithValue("@Main_id", textBox1.Text);
com.Parameters.AddWithValue("@SName", label1.Text);
com.Parameters.AddWithValue("@sub", comboBox1.Text);
com.Parameters.AddWithValue("@test_no", comboBox1.Text);
com.Parameters.AddWithValue("@mark", textBox4.Text);
com.Parameters.AddWithValue("@Mrk_R", textBox2.Text);
com.Parameters.AddWithValue("@Mrk_W", textBox3.Text);
com.Parameters.AddWithValue("@Exam_date", dateTimePicker1.Text);
com.ExecuteNonQuery();
connectiondb.Close();
MessageBox.Show("Records Successfuly Inserted");
}
Thanks..