Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I am new to programming, and I have started my first C# project (which is very basic), but I just need a little help with an error I am getting.

I have a C# front end and a MS Access 2003 back end (I will use SQL when I get a little more advanced.)

When I try updating a record, I get a message saying: Syntax error {missing operator) in query expression 'ContactId ='

My code is as follows:

private void btnSave_Click(object sender, EventArgs e)
        {
            OleDbConnection conn = new OleDbConnection();
            conn.ConnectionString = "PROVIDER=Microsoft.Jet.OleDb.4.0;Data Source=C:\\Users\\Antoine\\Documents\\Visual Studio 2010\\Projects\\myProject\\imyProject\\bin\\Debug\\myProject.mdb";
           
            conn.Open();

            string strUpdate = "UPDATE Contact SET ContactLastName = '" + txtLastName.Text + "' WHERE ContactId = " + txtContactId.Text + "";

            OleDbCommand cmd = new OleDbCommand(strUpdate, conn);

            cmd.ExecuteNonQuery();

            MessageBox.Show("Record updated", "Contacts", MessageBoxButtons.OK);
        }

It will be something very simple, I know, and I'm sure there's more efficient coding methods, but I'm having fun learning.

Thanks in advance for your help.

Antony

[edit]Code block added - OriginalGriff[/edit]
[edit2]Indentation corrected - OriginalGriff[/edit2]
Posted
Updated 24-Apr-11 1:50am
v3

try this
string strUpdate = "UPDATE Contact SET ContactLastName = '" + txtLastName.Text + "' WHERE ContactId = " + txtContactId.Text;

- verify ContactLastName,ContactId is valid format
- Read this Tutorials - http://msdn.microsoft.com/en-us/library/aa288436(v=vs.71.aspx
 
Share this answer
 
v3
Comments
Venkatesh Mookkan 24-Apr-11 7:56am    
The quote next to txtContactId.Text is not harmful and is not the actual problem.

His query and your solution does the same thing. :)
Seems like ContactId is not Numeric (Number) in the table. Check the table if it is String data type.
 
Share this answer
 
v2
The UPDATE command looks ok, if there is a valid Contact ID in your text box, and no special characters - a single space is all it would take to cause a problem. I would suggest that you use Parametrized queries anyway: they are a lot safer, and make your code easier to read:
string strUpdate = "UPDATE Contact SET ContactLastName = @CLN WHERE ContactId = @CID";
OleDbCommand cmd = new OleDbCommand(strUpdate, conn);
cmd.Parameters.AddWithValue("@CLN", txtLastName.Text);
cmd.Parameters.AddWithValue("@CID", txtContactId.Text);

This may well get rid of your error!
 
Share this answer
 
Hi,

Thanks for all your suggestions.

I've just discovered that when the ContactId textbox is visible the code works.

I had it hidden, as it would seem pointless having an Id field displayed.

In response to your suggestions, I tried:

string strUpdate = "UPDATE Contact SET ContactLastName = '" + txtLastName.Text + "' WHERE ContactId = " + txtContactId.Text; with the ContactId textbox hidden again, but that never worked. Thanks anyway.

ContactId is an AutoNumber (Primary key) field in the table (apologies, I should have mentioned this in the initial post.)

I will look up paramatized queries for future work.

Why would the code work with the textbox visible, but not when hidden? (Is this a stupid question?)

Antony
 
Share this answer
 
Comments
velvet7 24-Apr-11 12:32pm    
textbox.Visible = false, and string s = textbox.Text. It gives the correct result for me.

PS.: Why are you using hidden textboxes if they will never appear? Use a simple integer.
[no name] 25-Apr-11 10:00am    
Hi,

I have a hidden textbox that is bound to the ContactId field just to make my Where clause work. Like I say, I'm only learning, so any suggestions and advice will be taken on board.

Thanks

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