Hi I am trying to use replace function in c# but it doesnt seems to work.Am i doing anything wrong.Thanks

private void btnSpanish_Click(object sender, EventArgs e)
            SimpleSpanishDict.SimpleSpanishEnglishDictionary ins = new SimpleSpanishEnglishDictionary();

            Hashtable aaa = ins.AllWords();
            StringBuilder insertCommand = new StringBuilder();
            IDictionaryEnumerator en = aaa.GetEnumerator();
            if (aaa.Count > 0)

                while (en.MoveNext())
                    string al = en.Value.ToString();
                    string al2 = "";
                        al2 = al.Replace("'", "''");
                        myString = @"INSERT INTO TblSpanish (word,definition) Values('" + en.Key + "','" + al2 + "')";
                        SqlCommand myCmd = new SqlCommand(myString, cn);




I actually solved it.There is no problem in the code.I was actually using replace function only for values not for key.Now that i have replace function for keys too its working fine.Thanks


I don't get the point of inserting al and al2 (which will definitely have similar values when inserted in word and definition field) but it seems that you use the Replace() function correctly... and might not be the source of your issue...

You might want to consider using Parameters.AddWithValue if you want to properly escape the single quote character. I have used Replace("''") only in circumstances that Parameters.AddWithValue is not available in methods like Table.Select( "<filterExpression>" )...

Anyway here's an example:

@"INSERT INTO TblSpanish (word,definition) Values(@field1, @field2)"
sqlcommand.Parameters.AddWithValue("@field1", value1);
sqlcommand.Parameters.AddWithValue("@field2", value2);


This question has already been answered. Start a new discussion instead.