Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I want to accept ' also means India's but it throws exception


C#
private void SearchText(string strSearchText)
   {
       DataTable dt = GetRecords();
       DataView dv = new DataView(dt);
       string SearchExpression = null;
       if (!String.IsNullOrEmpty(strSearchText))
       {
           SearchExpression = string.Format("{0} '%{1}%'", grdSearch.SortExpression, strSearchText);


           dv.RowFilter = "country_name like" + SearchExpression;

           grdSearch.DataSource = dv;
           grdSearch.DataBind();
       }
       else
           ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('Enter Search Text')", true);

   }
Posted
Comments
Sergey Alexandrovich Kryukov 11-Dec-12 0:40am    
Not clear. How apostrophe could mean "India"? What does it mean, "accept"? How is this related to your code? What happens, exactly?
--SA

Check the below code

just replace the ' by ''

C#
private void SearchText(string strSearchText) 
    { 
        DataTable dt = GetRecords(); 
        DataView dv = new DataView(dt); 
        string SearchExpression = null;
        if (!String.IsNullOrEmpty(strSearchText))
        {
            SearchExpression = string.Format("{0} '%{1}%'", grdSearch.SortExpression, strSearchText.Replace("'","''"));
          
          
            dv.RowFilter = "country_name like" + SearchExpression;
 
            grdSearch.DataSource = dv;
            grdSearch.DataBind();
        }
        else
            ScriptManager.RegisterStartupScript(this, this.GetType(), "msg", "alert('Enter Search Text')", true);
            
    }
 
Share this answer
 
v2
Comments
Member-515487 11-Dec-12 1:17am    
not working error at .replace
Vindhyachal_Kumar 11-Dec-12 1:29am    
replace this code.

SearchExpression = string.Format("{0} '%{1}%'", grdSearch.SortExpression, strSearchTert.Replace("'","''"));
Vindhyachal_Kumar 11-Dec-12 1:30am    
SearchExpression = string.Format("{0} '%{1}%'", grdSearch.SortExpression, strSearchText.Replace("'","''"));
Replace the single quote with a double quote.
Example - like '%''something''%'

You can look at examples here -
http://www.windowsitpro.com/article/john-savills-windows-faqs/how-do-i-insert-a-single-quote-into-a-microsoft-sql-server-database-[^]
 
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