Click here to Skip to main content
15,884,472 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
i am building a win form app using access database as data-source , and in the search button of my winform i have this code"
C#
private void searchAccessDatabase()
        {
            if (string.IsNullOrEmpty(KeywordTextBox.Text.Trim()))
            {
                Refreshdata();
                return;
            }
            string strkeyword = KeywordTextBox.Text.Trim().ToString();

            StringBuilder sb = new StringBuilder();
            sb.AppendFormat("(Convert(ID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
            sb.AppendFormat("OR (Customer_Name LIKE '*" + "{0}" + "*')", strkeyword);
            sb.AppendFormat("OR (Complaint_Number LIKE '*" + "{0}" + "*')", strkeyword);
            sb.AppendFormat("(Convert(Size,'System.String') LIKE '" + "{0}" + "')", strkeyword);
            sb.AppendFormat("OR (Material_Number LIKE '*" + "{0}" + "*')", strkeyword);
            sb.AppendFormat("OR (Nature_Of_Problem LIKE '*" + "{0}" + "*')", strkeyword);
            sb.AppendFormat("OR (Spool_Type LIKE '*" + "{0}" + "*')", strkeyword);
            string strFilter = sb.ToString();
           material_Return_DataBindingSource.Filter = strFilter;

            if (material_Return_DataBindingSource.Count != 0)
            {
                dataGridView1.DataSource = material_Return_DataBindingSource;
            }
            else
            {
                MessageBox.Show("No Records Found", "Search Result", MessageBoxButtons.OK,
                    MessageBoxIcon.Exclamation);

                Refreshdata();
                return;
            }
        }

But during running the below error is coming : 'Cannot perform 'Like' operation on System.Single and System.String.'

I know that the error is related to the format type of the cells and the search type i am using but i am unable to correct them as i am still learning on this : Anybody please correct me where i am wrong.

What I have tried:

This is the sample data table : which will help you see the sample data i am entering to the table , with which i want to search

ID		10
Returned_Date		23-Mar-20
Customer_Name		XXXX
Complaint_Number		C20-02
Spool_Number		QF18087829
Size		330
12_NC		Q123456
Material_Number		12345P
DOP		12-03-2020
Qty_Sent_Grams		1.371000051
Qty_Sent_Meter		1.371000051
Qty_Returned_Grams		0.305000007
Qty_Returned_Meter		0.305000007
Difference_Qty_Grams		0
Difference_Qty_Meters		0
Nature_Of_Problem		wwwww
Type _of_material		EPS
Spool_Type		S200(ABS)
Remarks		wwwww
Posted
Updated 11-Aug-20 20:24pm
Comments
Sandeep Mewara 12-Aug-20 2:31am    
I am not sure, Think, this should not be in single quotes: 'System.String'
Sandeep Mewara 12-Aug-20 2:47am    
BTW, I would use String.format() for readability here at the cost of performance.
GenJerDan 12-Aug-20 3:45am    
How would LIKE work on a number? Is 3 like 13? Or is 4 like 3, only a little bigger?
Anyway, check out CAST and CONVERT and turn the number into a string and do the comparison on the newly converted string.

1 solution

Think, this should not be in single quotes: 'System.String'

Further, can you make sure the strFilter looks like as expected?
Surely following corrections:
1. Is it not missing spaces between all the OR conditions?
2. Missing OR in line:
sb.AppendFormat("(Convert(Size, System.String) LIKE '" + "{0} " + "')", strkeyword);


One more thing, you sure, you want to append '*' to searchwords with LIKE. Or that would be "%" ? (below code does not have this change)

Try below:
C#
string strkeyword = KeywordTextBox.Text.Trim().ToString();

StringBuilder sb = new StringBuilder();
sb.AppendFormat("(Convert(ID, System.String) LIKE '" + "{0}" + "') ", strkeyword);
sb.AppendFormat("OR (Customer_Name LIKE '*" + "{0}" + "*') ", strkeyword);
sb.AppendFormat("OR (Complaint_Number LIKE '*" + "{0}" + "*') ", strkeyword);
sb.AppendFormat("OR (Convert(Size, System.String) LIKE '" + "{0} " + "')", strkeyword);
sb.AppendFormat("OR (Material_Number LIKE '*" + "{0}" + "*') ", strkeyword);
sb.AppendFormat("OR (Nature_Of_Problem LIKE '*" + "{0}" + "*') ", strkeyword);
sb.AppendFormat("OR (Spool_Type LIKE '*" + "{0}" + "*')", strkeyword);
string strFilter = sb.ToString();
material_Return_DataBindingSource.Filter = strFilter;

Reference: BindingSource.Filter Property (System.Windows.Forms) | Microsoft Docs[^]
 
Share this answer
 
v4
Comments
Sandeep Mewara 12-Aug-20 2:33am    
CodeProject is throwing error... not sure it got posted.
Member 14898617 12-Aug-20 3:37am    
Thank you so much for your response , I added the Missing OR , but as i am new i have on doubt : if the field contains both number , text and symbol like = S200(ABS) , how should we write the like statement?
Sandeep Mewara 12-Aug-20 3:44am    
Can you elaborate? What is the data and what do you want to do with it as a search?

ex: data is "S200(ABC)". Now, user can put 200 or ABC, both should return this data. Right?
Member 14898617 12-Aug-20 4:49am    
Exactly sir ..Thank you for understanding ..very sorry for my late reply.
Sandeep Mewara 12-Aug-20 4:58am    
I already shared that too in my answer. Change the * to % for LIKE keywords and see
Exampled: "OR (Customer_Name LIKE '%" + "{0}" + "%') "

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