Become Joomla Developer

how to save special string <'> to ms sql sever

2 replies.


kheang_chea
Joined: 19-01-2010 02:38 AM
Posts 3
how to save special string <'> to ms sql sever

19-01-2010 10:05 AM

SQLCommand = "INSERT INTO [ProdOrder]([LPO_Desc]) VALUES " + "(" + txtLPODesc.Text.Trim() +"')";
above is my sql command, I try to insert string data from txtLPODesc textbox in to ProdOrder table, field LPO_Desc.
my problem: when txtLPODesc.text = MEN's T-shirt. then i execute above command it show error msg

if txtLPODesc.text not contain <'> string it will not have problem.
can any one help me how to solve the problem?
best regards,
Kheang



Reply | Report Abuse


silverserey
Joined: 01-09-2009 08:52 AM
Posts 5
Re: how to save special string &lt;'&gt; to ms sql sever

19-01-2010 11:08 AM

ចំពោះបញ្ហានេះអ្នកអាចមានជំរើសច្រើនក្នុងការប្រើប្រាស់ Sql statement ដូចជា ការប្រើប្រាស់ Stored Procedures រឺជំនួសដោយការប្រើប្រាស់ Parametter ខាងក្រោមនេះគឹជាការរបៀបនៃការប្រើប្រាស់ Parametter៖

SqlCommand cmd = new SqlCommand("INSERT INTO [ProdOrder]([LPO_Desc]) VALUES (@1)", SqlConnectionString);
cmd.Parameters.Add("@1", SqlDbType.VarChar, 20).Value = txtLPODesc.Text.Trim() ;

បន្ទាប់មកអ្នកអាចបើក Connection និង ធ្វើការ Excute នូវ SqlCommand។

សិរី,



Reply | Report Abuse


eprasart
Joined: 01-12-2009 10:47 PM
Posts 20
Re: how to save special string &amp;lt;'&amp;gt; to ms sql sever

19-01-2010 01:45 PM

The best thing to handle this is to pass the value (from text box) through parameters as Serey did.
If you prefer your way, you just need to escape the quote by double quote like:
INSERT INTO table1 (column1) VALUES ('Someone''s name')



Reply | Report Abuse

1

Suggestion | Contact US | About US | Term of Use | Privacy Policy | Advertiseing