I wriiten a insert query in C# and it works fine.But when i am trying to insert character like ' then its throwing exception.
Please let me know the correct way so that it should accept all characters.
Thanks
Loading
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend4days leftRegister
Gomathi PalaniswamyRank #714 · 3.3K reputationPosted Oct 10, 2010, 8:23 PM EDT
for insert character you should follow like
insert into tablename values('aaa','bbb');
while getting control value you should write like
insert into tablename values('"+textbox1.text+"','"+textbox2.text+"');
Apurva Mehta34 reputationPosted Oct 10, 2010, 8:29 PM EDT
charbel ghattas2 reputationPosted Mar 17, 2012, 10:11 AM EDT
for insert character you should follow like
insert into tablename values('aaa','bbb');
while getting control value you should write like
insert into tablename values('"+textbox1.text+"','"+textbox2.text+"');
wat will happen if the value of textbox1 is like ABCD's then while inserting it will be like 'ABCD's' and will throw exception.
if textbox1.text = ABCD's
then u can use this :
insert into tablename values('"+Regex.Replace(textbox1.text, "'", "''")+"','"+textbox2.text+"');
SenthilkumarRank #387 · 6.5K reputationPosted Mar 17, 2012, 10:42 PM EDT
INSERT INTO Example(Name,Address) VALUES('YYY''s',50)
Based on that, you can workaround in asp.net
Jignesh TrivediRank #16 · 50.3K reputationPosted Mar 18, 2012, 10:42 PM EDT
please try
insert into tablename values('"+textbox1.text.Replace("'","''")
+"','"+textbox2.text.Replace("'","''")
+"');
Replece you single quote to two single quote.
hope this help.