user2927881
user2927881

Reputation: 27

Error in update query in c# for access database

I am facing error in following Query.According to my knowledge I have written everything perfectly fine. But its giving error that:

"there is an error in update query"

string insert_query = "update aho set read=?,pick=? where Cont_no='" + contract_no + "'";
OleDbCommand ocmd = new OleDbCommand();
ocmd.CommandText = insert_query;
//ocmd.Parameters.AddWithValue("@contrct_no", contract.Text.ToString());
ocmd.Parameters.AddWithValue("@read_val", Convert.ToInt32(read.Text));
ocmd.Parameters.AddWithValue("@pic_val", Convert.ToInt32(pick.Text));
ocmd.Connection = conn;
ocmd.ExecuteNonQuery();

Upvotes: 1

Views: 113

Answers (2)

Soner Gönül
Soner Gönül

Reputation: 98868

You didn't gave us too much information but..

I think your Cont_no type is some numerical type, not one of the character type. Looks like that's why you get error when you try to add it with ''.

For example like;

Cont_no = '123'

Try this one;

string insert_query = "update aho set [read]=?,pick=? where Cont_no=?";
OleDbCommand ocmd = new OleDbCommand();
ocmd.CommandText = insert_query;
ocmd.Parameters.AddWithValue("@read_val", Convert.ToInt32(read.Text));
ocmd.Parameters.AddWithValue("@pic_val", Convert.ToInt32(pick.Text));
ocmd.Parameters.AddWithValue("@contrct_no", contract_no);
ocmd.Connection = conn;
ocmd.ExecuteNonQuery();

EDIT: HansUp is totally right. Read is a reserved keyword. You should use it with square brackets like [Read] in your query.

Upvotes: 1

wind39
wind39

Reputation: 451

In your query string you consider parameters by priority, but when you create them you are giving them a name.

According to http://msdn.microsoft.com/en-us/library/system.data.oledb.oledbcommand.parameters.aspx, OleDbCommand does not support named parameters.

Look at this example (source: http://www.java2s.com/Code/CSharp/Database-ADO.net/PassparametertoOleDbCommand.htm):

    using System;
    using System.Data;
    using System.Data.OleDb;

    public class Prepare {    
     public static void Main () { 
       String connect = "Provider=Microsoft.JET.OLEDB.4.0;data source=.\\Employee.mdb";
       OleDbConnection con = new OleDbConnection(connect);
       con.Open();  
       Console.WriteLine("Made the connection to the database");

       OleDbCommand cmd1 = con.CreateCommand();
       cmd1.CommandText = "SELECT ID FROM Employee "
                        + "WHERE id BETWEEN ? AND ?";
       OleDbParameter p1 = new OleDbParameter();
       OleDbParameter p2 = new OleDbParameter();
       cmd1.Parameters.Add(p1);
       cmd1.Parameters.Add(p2);
       p1.Value = "01";
       p2.Value = "03";
       OleDbDataReader reader = cmd1.ExecuteReader();
       while(reader.Read()) 
         Console.WriteLine("{0}", reader.GetInt32(0));
       reader.Close();
       con.Close();
     }
    }

Upvotes: 0

Related Questions