2

Need your help in below.

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 ' (single quote).

Below is the 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 <= 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.

This code fails when LastName contains single quotes.

I need a solution where I am not removing quotes from last name.

Thanks

RePierre
  • 9,358
  • 2
  • 20
  • 37
user1717270
  • 755
  • 3
  • 16
  • 26

3 Answers3

6

Instead of using dt.Select() in the first place, I'd strongly suggest using LINQ. It avoids the whole "embedding queries in strings" problem which is so ghastly and reminiscent of SQL injection attacks.

So:

var lastNameToFind = dgRow.Cells[2].Text.Replace("&amp;", "");
var matched = dt.AsEnumerable()
                .Where(dr => dr["LastName"].Equals(lastNameToFind))
                .Any();

if (matched)
{
    DataRow newRow = dt.NewRow();
    dt.Rows.Add(newRow);
    newRow["FirstName"] = dgRow.Cells[1].Text;
    newRow["LastName"] = dgRow.Cells[2].Text;
    dt.AcceptChanges();
}
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • I didn't want to suggest this as an edit in case I've just misunderstood. I implemented your code above but got an error that 'dr' was undefined. After some reading to improve my understanding of LINQ (so thanks for providing a prompt to do so!) I changed it to .Where(dr => dr["LastName"].Equals(lastNameToFind)) and this solved it. Thanks for the solution. – Kate Oct 28 '13 at 15:09
0

Try changing:

DataRow[] dr = dt.Select("LastName='" + dgRow.Cells[2].Text.Replace
("amp;", "")+ "'");

to this:

DataRow[] dr = dt.Select("LastName='" + dgRow.Cells[2].Text.Replace
("amp;", "").Replace("\'", "\'\'") + "'");

(untested)

Nigel Whatling
  • 2,371
  • 1
  • 16
  • 22
  • Better implementation (as mentioned by @Rawling) in [Correct way to escape characters in a DataTable Filter Expression](http://stackoverflow.com/questions/386122/correct-way-to-escape-characters-in-a-datatable-filter-expression) – Nigel Whatling Feb 07 '13 at 07:17
  • Thanks, this is working however its failing in newRow["LastName"] = dgRow.Cells[2].Text; also in my datatable only single quote should go – user1717270 Feb 07 '13 at 07:42
0

You can replace Single quote with Double Quote :

Dim LastName As String = Replace(txtLastName.Text, "'", "''")

but it is not good practice...

check this out : Replacing apostrophe in asp.net to prevent SQL error

Community
  • 1
  • 1
Sachin
  • 2,152
  • 1
  • 21
  • 43