Click here to Skip to main content
15,917,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is my deal... i have some products in my database that have special characters in the names. I need to remove these before inserting into a table.

i have some dropdownlists for the values of the product and id and such.

here is one of the products: 3'x5'x1/2" USG NEXT GENERATION DUROCK. I need to remove the ' after the 3 and 5.

you can see below i tired to remove these special characters before inserting into the table. - look at product1.selecteditem.text.remove("'", "")) here is where i am trying to remove these characters. But i cannot figure it out.

please help! VB

SQL
cmd.CommandText = "INSERT INTO Table(blahblahblah) values('" & Trim(Literature.SelectedValue) & "', '" & Trim(Literature.SelectedItem.Text) & "', '" & Trim(Product1.SelectedValue) & "', '" & Trim(Product1.SelectedItem.Text.Remove("'", "")) & "', '" & Session("UserInfo") & "')"
cmd.ExecuteNonQuery()
Posted
Comments
[no name] 11-Oct-12 14:17pm    
That's because the remove method returns a new string object so you are essentially calling the remove method and simply throwing the results away.

instead of remove i used replace("'", "") works good!
 
Share this answer
 
Use Regex.Replace[^] method. Example[^]
 
Share this answer
 
I would suggest these two ways:

1) Front End Handling:

In your case, you have to use string.Replace() method. Personally, I prefer RegEx iff there is a condition to look for a specific pattern like finding a string that start with number and replace it with something else, if its a static text string.Replace is enough.

2) Back End Handling: (If using SQL)

You can rely on QUOTENAME function. Recommended Read
 
Share this answer
 
That's the wrong way of handling it. The single quotes cause problems because you create the query by concatenating strings. When you create a parameterized query instead, and then add the parameters' values, those special characters will be handled correctly automatically.
 
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