Click here to Skip to main content
15,889,216 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to pass the long text in update query in c#. i am getting the error while passing big text value to the database. I have tried the below query when the values are passing like single word it is working fine. when the values are too lengthy and contains some special characters the data is not updating..

please assist

Thanks in advance

What I have tried:

SqlCommand cmd1 = new SqlCommand("UPDATE ERP_Risk_Mgmt  set Account_name='" + strAccountName + "',Key_Risks='" + strkeyRisk + "',Mitigation_Plan ='" + strMitigationPlan + "',Contingency_plan='" + strContingencyPlan + "',Status_in_detail='" + srtStatusindetail + "',Rdate='" + strMonth + "',Pool_name='" + strPoolName + "',Group_name='" + strGroupName + "',Lastupdatedate='" + strlastupdatedate + "' where Account_name ='" + strAccountName + "'", con);
          
            con.Open();
            cmd1.ExecuteNonQuery();
            con.Close();

            lblSuccess.Visible = true;
Posted
Updated 8-Aug-17 2:37am
v2
Comments
F-ES Sitecore 8-Aug-17 8:13am    
Use parameterised queries. Google "ado.net parameterised queries". It won't only fix your problem but it will remove your code's vulnerability to possible SQL injection attacks.

Absolutely not like that.
Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

Using parameters will not only prevent you from your current SQL Injection risk, but will also solve your existing problem at the same time:
C#
using (SqlCommand cmd1 = new SqlCommand("UPDATE ERP_Risk_Mgmt  set Account_name=@AN, Key_Risks=@KR ,Mitigation_Plan=@MP, Contingency_plan=@CP, Status_in_detail=@SID, Rdate=@RD, Pool_name=@PN, Group_name=@GN, Lastupdatedate=@LUD where Account_name=@AN", con))
   {
   cmd1.Parameters.AddWithValue("@AN", strAccountName);
   ...
 
Share this answer
 
Quote:
How to pass special characters in update query

Use parameters, it will protect you from "SQL injection" and solve your problem too.

Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
 
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