Click here to Skip to main content
15,886,771 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
txtCustomerName = "Mc' Donald";
string strsql = "Update Tbproposal set CustomerName ='" +txtCustomerName +"'";
DataHelper.ExecuteQuery(strsql);

When I am executing this query, i am facing th`enter code here`e following problem. Incorrect syntax near 'Donald'.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ''.

I know what is the reason. but want to know how can i solve this.

What I have tried:

I want to handle it on my textbox filed.
Posted
Updated 23-Jun-16 4:11am

temporary fix is
C#
string txtCustomerName = "Mc'' Donald";


but the above code is not recommended, since it is vulnerable to SQL Injection[^] attacks.
always use Using Parameterized queries to prevent SQL Injection Attacks [^]

You will have to write a method inside your DataHelper class to handle the sql command as
C#
static void ExecuteCommand(SqlCommand cmd)
   {
       SqlConnection con = new SqlConnection();
       con.ConnectionString = "Your Connectoin string";
       cmd.Connection = con;
       con.Open();
       cmd.ExecuteNonQuery();
       con.Close();
   }

and invoke it like below
C#
string customerName = txtCustomerName.Text;
      string strsql = "Update Tbproposal set CustomerName = @customer";
      SqlCommand cmd = new SqlCommand(strsql);
      cmd.Parameters.Add("@customer", customerName);
      DataHelper.ExecuteCommand(cmd);
 
Share this answer
 
v6
Comments
Sergey Alexandrovich Kryukov 23-Jun-16 10:04am    
5ed.
—SA
Karthik_Mahalingam 23-Jun-16 10:12am    
Thank you Sergey
Sergey Alexandrovich Kryukov 23-Jun-16 10:15am    
I also added Solution 3, to explain some background and provide more links.
—SA
You already got some advice related to SQL injection. This is the bad thing: your query composed by concatenation with strings taken from UI. Not only repeated string concatenation is inefficient (because strings are immutable; do I have to explain why it makes repeated concatenation bad?), but it's more important that it opens the doors to SQL injection, a very well-known exploit. You just need to understand the background.

This is how it works: http://xkcd.com/327.

Are you getting the idea? The string taken from a control can be anything, including… a fragment of SQL code.

What to do? Just read about this problem and the main remedy: parametrized statements: http://en.wikipedia.org/wiki/SQL_injection.

With ADO.NET, use this: http://msdn.microsoft.com/en-us/library/ff648339.aspx.

Please see my past answers for some more detail:
EROR IN UPATE in com.ExecuteNonQuery();,
hi name is not displaying in name?.

See also (links provided by our member Richard Deeming):
Troy Hunt: Everything you wanted to know about SQL injection (but were afraid to ask),
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange,
Query Parameterization Cheat Sheet — OWASP[^],
SQL injection attack mechanics | Pluralsight — YouTube.

—SA
 
Share this answer
 
v2
Comments
Karthik_Mahalingam 23-Jun-16 10:20am    
Counter 5 for additional Information
Sergey Alexandrovich Kryukov 23-Jun-16 10:22am    
Thank you, Karthik.
—SA
Well, as always with SQL queries which receive some values from a GUI: use a parameterized query instead of simple string concatenation. That will handle all that escaping, DateTime/number formats, SQL injection avoidance etc. for you.
 
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