Escaping special character in string in C#
<p>Need your help in below.</p>

<p>I have code where I am checking that a particular row exists in datatable or not; if row doesn't exist then I add that row to datatable. This is working fine however failing when row value contains special char link <code>'</code> (single quote).</p>

<p>Below is the code:</p>

<pre><code>string lastName = dgRow.Cells[2].Text.Replace("amp;", "");
DataRow[] dr = dt.Select("LastName='" + lastName + "'"); //check whether row is available in datatable or not

if (dr.Length &lt;= 0)// Condition to check if row is there in data table
{
    dt.Rows.Add();
    dt.Rows[dt.Rows.Count - 1]["FirstName"] = dgRow.Cells[1].Text;
    dt.Rows[dt.Rows.Count - 1]["LastName"] = dgRow.Cells[2].Text;
    dt.AcceptChanges();
}
return dt; //Return modified data table to calling function.
</code></pre>

<p>This code fails when LastName contains single quotes.</p>

<p>I need a solution where I am not removing quotes from last name.</p>

<p>Thanks</p>
