Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#
I am trying to perform insert, update, delete actions on table using DataGridView.
Reference: http://csharp.net-informations.com/datagridview/csharp-datagridview-database-operations.htm[^]
My database is Microsoft Access 2007. It inserts well but on updating and deleting gives an error. The Code is as following...
OleDbCommand sCommand;
        OleDbDataAdapter sAdapter;
        OleDbCommandBuilder sBuilder;
        DataSet sDs;
        DataTable sTable; 
 
        //Load Data...
        private void button1_Click(object sender, EventArgs e)
        {
            string connectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source= " + Application.StartupPath + @"\Label-Data.accdb";
            string sql = "SELECT * FROM _Default";
            OleDbConnection connection = new OleDbConnection(connectionString);
            connection.Open();
            sCommand = new OleDbCommand(sql, connection);
            sAdapter = new OleDbDataAdapter(sCommand);
            sBuilder = new OleDbCommandBuilder(sAdapter);
            sDs = new DataSet();
            sAdapter.Fill(sDs, "_Default");
            sTable = sDs.Tables["_Default"];
            connection.Close();
            dataGridView1.DataSource = sDs.Tables["_Default"];
            dataGridView1.ReadOnly = true;
            button4.Enabled = false;
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
 
        //Add or Edit...
        private void button2_Click(object sender, EventArgs e)
        {
            dataGridView1.ReadOnly = false;
            button4.Enabled = true;
            button2.Enabled = false;
            button3.Enabled = false;
        }
 
        //Delete...
        private void button3_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Do you want to delete this row ?", "Delete", MessageBoxButtons.YesNo) == DialogResult.Yes)
            {
                dataGridView1.Rows.RemoveAt(dataGridView1.SelectedRows[0].Index);
                sAdapter.Update(sTable);
            }
        }
 
        //Save...
        private void button4_Click(object sender, EventArgs e)
        {
            sAdapter.Update(sTable);
            dataGridView1.ReadOnly = true;
            button4.Enabled = false;
            button2.Enabled = true;
            button3.Enabled = true;
        }
 
It gives an error on sAdapter.Update(sTable); line. The error is "Syntax error in query expression '((ID = ?) AND ((? = 1 AND Prn IS NULL) OR (Prn = ?)) AND ((? = 1 AND _Copy IS NULL) OR (_Copy = ?)) AND ((? = 1 AND _Item IS NULL) OR (_Item = ?)) AND ((? = 1 AND _Design IS NULL) OR (_Design = ?)) AND ((? = 1 AND Tray_No IS NULL) OR (Tray_No = ?)) AND (('." Any suggestions?
Posted 24-Jan-13 19:03pm
Edited 24-Jan-13 19:07pm
v2
Comments
Azziet at 25-Jan-13 0:34am
   
error shows that problem with your query...check your query

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

your query is not proper. check it
  Permalink  

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

  Print Answers RSS
0 OriginalGriff 365
1 Sergey Alexandrovich Kryukov 334
2 CPallini 275
3 DamithSL 230
4 Maciej Los 215
0 OriginalGriff 5,455
1 DamithSL 4,457
2 Maciej Los 3,885
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,115


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100