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"
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.
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
Name LIKE '*test*'
will get every record with test in the Name field regardless of what is before or after it, where Name LIKE 'test*
will get you only the ones where Name field starts with test.The fact you are stating it is 'like' something without any wildcards or other signifiers may be causing issues.
Try just making them equal and see if it errors out.
Also you do not need the + in your string format for multiple strings as the sb.format works without them.
sb.AppendFormat("(Convert(ID,'System.String') LIKE '" + "{0}" + "')", strkeyword);
can be written as
sb.AppendFormat("(Convert(ID,'System.String') LIKE '{0}')", strkeyword);
I also not that the following line does not have and OR at the start which may be causing an error.
sb.AppendFormat("(Convert(Size,'System.String') LIKE '" + "{0}" + "')", strkeyword);
Maybe update your question with the final string that results from your append when it errors out.