problem with Select in datatable

  • Thread starter Thread starter perspolis
  • Start date Start date
P

perspolis

Hi all
I want to select from a datatable like below:
mydatatable.Select("Name='"+this.textbox1.Text+ "'");
it works properly but when I edit textbox1 with something like this re'fd
that contains ' or " the Select gives me an error "Missing Operand".
how can I solve this problem.
thanks in advance
 
Hi all
I want to select from a datatable like below:
mydatatable.Select("Name='"+this.textbox1.Text+ "'");
it works properly but when I edit textbox1 with something like this re'fd
that contains ' or " the Select gives me an error "Missing Operand".
how can I solve this problem.
thanks in advance

Try using the @ like this :
mydatatable.Select(@"Name='"+this.textbox1.Text+ "'");
 
Perspolis - you need to "escape" the single quotes in query filters in order
for them to be interpreted correctly.

Here's a method I use for querying with strings.

public string ToSqlString(string value, bool emptyStringIsNull)
{
if(value != null && (!emptyStringIsNull || value.Length > 0))
{
return "'" + value.Replace("'", "''") + "'";
}
else
{
return "NULL";
}
}

Then you could query your datatable as such...

mydatatable.Select("Name=" + ToSqlString(this.textbox1.Text));

Hope that helps.
 
Back
Top