Click here to Skip to main content
15,897,291 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace WindowsFormsApplication35
{
    public partial class Form3 : Form
    {
        public Form3()
        {
            InitializeComponent();
        }
        private void button1_Click(object sender, EventArgs e)
        {
            OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\qq.accdb;Persist Security Info=False");
            cn.Open();
            OleDbCommand cm=new OleDbCommand("UPDATE qq SET ahmed='"+textBox1.Text+"',ali="+textBox2.Text+",NOTE='"+textBox3.Text+"' WHERE ID="+comboBox1.Text,cn);
            cm.ExecuteNonQuery();
            cn.Close();
            MessageBox.Show("done");
        }
        private void button2_Click(object sender, EventArgs e)
        {
            OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\qq.accdb;Persist Security Info=False");
            OleDbDataAdapter da = new OleDbDataAdapter("select *from qq where ID=" + comboBox1.Text, cn);
            DataSet ds = new DataSet();
            da.Fill(ds, "qq");
            textBox1.DataBindings.Add("text", ds, "qq.ahmed");
            textBox2.DataBindings.Add("text", ds, "qq.ali");
           textBox3.DataBindings.Add("text", ds, "qq.NOTE");
        }
        private void Form3_Load(object sender, EventArgs e)
        {
            OleDbConnection cn = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\qq.accdb;Persist Security Info=False");
            OleDbDataAdapter da = new OleDbDataAdapter("select ID from qq", cn);
            DataSet ds = new DataSet();
            da.Fill(ds, "qq");
            comboBox1.DataSource = ds.Tables["qq"];
            comboBox1.DisplayMember = "ID";
        }
    }
}
it work before add new column in acess (NOTE) after add messagebox (OleDbException was unhandled)inside the box Syntax error in UPDATE statement.
I checked every thing its Ok
Posted
Updated 13-Jun-10 9:56am
v2

You shouldnt be using

"UPDATE qq SET ahmed='"+textBox1.Text+"',ali="+textBox2.Text+",NOTE='"+textBox3.Text+"' WHERE ID="+comboBox1.Text



It means if I write Mc'Donald in textBox1.Text it will error me out in Update statement.

Rather you should always use
Update qq Set ahmed=@pahmed....

Which you later add as parameter using
cm.Parameters.Add(new SqlParameter("@pahmed", ...



I think this would be better practice as it will also take care of Sql Injection.

:cool:
 
Share this answer
 
It is probably because you have missing single quotes i.e. this line

OleDbCommand cm=new OleDbCommand("UPDATE qq SET ahmed='"+textBox1.Text+"',ali="+textBox2.Text+",NOTE='"+textBox3.Text+"' WHERE ID="+comboBox1.Text,cn);


Should be written like this

OleDbCommand cm=new OleDbCommand("UPDATE qq SET ahmed='"+textBox1.Text+"',ali='"+textBox2.Text+"',NOTE='"+textBox3.Text+"' WHERE ID="+comboBox1.Text,cn);


you missed the single quotes around textBox2.Text

Please, let me know if this solves your problem :) .
 
Share this answer
 
And i have to agree with Abhishek Sur this is a much better practice :).
 
Share this answer
 
Hello
IF your id colomn in database is int try this

OleDbCommand cm=new OleDbCommand("UPDATE qq SET ahmed='"+textBox1.Text+"',ali='"+textBox2.Text+"',NOTE='"+textBox3.Text+"' WHERE ID="+Convert.Toint32(comboBox1.Text),cn);

if your id colomn in database is sting try this

OleDbCommand cm=new OleDbCommand("UPDATE qq SET ahmed='"+textBox1.Text+"',ali='"+textBox2.Text+"',NOTE='"+textBox3.Text+"' WHERE ID='"+comboBox1.Text+"'",cn);

Hope for help
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900