1

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();
1
  • What is your insert_query looks like when you debug it? Commented Oct 31, 2013 at 12:27

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Cont_no is string. and your provided solution is still not working
@user2927881 What is your insert_query looks like when you debug it?
@user2927881 I'm not asking exception. I'm asking when you look at insert_query in debugger, what you see ?
i have put break points but its not taking me to debugger and poping up the exception.
Read is a reserved word. Bracket it in the statement ... set [read]=?
|
0

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();
     }
    }

1 Comment

when i am doing it with priority order.its still not working.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.