Click here to Skip to main content
15,892,480 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i am working on visual studio framework to manipulate my database using ado.net connectivity this is my update statement

C#
string stdupdt = "update std set  name='"+txtNM.Text+"' ,addr= '"+txtAD.Text+"' where id='"+txtID.Text+"'"; 


what is wrong with this statement please
Posted
Updated 1-Jan-13 2:00am
v2
Comments
ridoy 1-Jan-13 9:15am    
what error you get on that?

Without knowing anything about teh error message you get, it is very difficult to be sure, but...

Probably, you problem is to do with the database definition - you have used an Access reserved word[^] as a column name, and this is likely to confuse Access when you try to update as it isn't expecting to see the word in that context.
In this case, you should not name any column "name" - if you do, then you need to enclose it in square brackets:
C#
string stdupdt = "update std set [name]='"+txtNM.Text+"' ,addr= '"+txtAD.Text+"' where id='"+txtID.Text+"'"; 
 
Share this answer
 
Comments
Abhinav S 1-Jan-13 10:12am    
May be a good catch. 5.
abbasnafiu 1-Jan-13 15:35pm    
this is my code statement

OleDbConnection myConnection2 = new OleDbConnection(con);
//string stUpd = "update std2 +"set names=+txtNm.Text+ ,schs= +txtSch.Text+" + where ids=+txtId.Text+";
string stUpd = "update std2 set [names]='" + txtNm.Text + "' ,schs= '" + txtSch.Text + " where ids=txtId.Text";
myConnection2.Open();
OleDbCommand stCmd2 = new OleDbCommand(stUpd, myConnection2);
stCmd2.ExecuteNonQuery();
myConnection2.Close();
MessageBox.Show("updated");

after debugging it highlights stCmd2.ExecuteNonQuery();

and the error statement is: THE UPPDATE SYNTAX IS INCORRECT
OriginalGriff 1-Jan-13 16:37pm    
You missed a closing quote:
Text + "',Sachs='"+txtSch.Text + " where
Becomes:
Text + "',Sachs='"+txtSch.Test + "' where
But please don't do it like that - you are leaving yourself open to a deliberate or accidental SQL Injection Attack which can destroy your entire database. Use parameterized queries instead.
Make sure Id is not numeric.

Try and debug this code. When execution comes to this line, pick up the query, paste in management studio and run it.
See what type of error you get. It should give you a better idea of how to fix this problem.

Also note that passing parameters directly into a query can lead to security concerns.
Try and use parameters - http://csharp-station.com/Tutorial/AdoDotNet/Lesson06[^].
 
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