Solved

How should I do this?

Posted on 2008-10-02
7
146 Views
Last Modified: 2012-05-05
I have a "generic" search dialog that I am able to fill a datatable with two columns for searching. These two
columns may be different types from Integer to String to DateTime

There is one textbox for entering a search term. How would I format the DataTable's RowFilter according to the
text entered based on the types of the two columns in the datatable?

Currently, we have it like the code below and it is causing problems when the first column is an Integer column.
The objColl is a collection with an ISearchableWrapper interface that returns the NAME of the particular column in
the SearchItem_DataPropertyName and SearchDesc_DataPropertyName properties. The items in the collection are
what is loaded into the DataTable.

Any ideas on how I should approach this? Have you done something like this before?
Private Sub txtFilter_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles txtFilter.TextChanged
    If Me.tbl IsNot Nothing Then 
      Me.tbl.DefaultView.RowFilter = Me.objColl.SearchItem_DataPropertyName & " like '" &  Me.txtFilter.Text & "%' OR " & 
                                     Me.objColl.SearchDesc_DataPropertyName & " like '" & Me.txtFilter.Text & "%'"
End Sub

Open in new window

0
Comment
Question by:EddieShipman
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
7 Comments
 
LVL 10

Accepted Solution

by:
MoreHeroic earned 500 total points
ID: 22626660
Well, doing a string comparison like that you might try casting the unknown type into a string prior to performing the like so that your rowexpression would look something like this:

Me.tbl.DefaultView.RowFilter = "Convert(" & Me.objColl.SearchItem_DataPropertyName & ", 'System.String') like '" &  Me.txtFilter.Text & "%' OR " &
                                     "Convert(" & Me.objColl.SearchDesc_DataPropertyName & ", 'System.String') like '" & Me.txtFilter.Text & "%'"
0
 
LVL 26

Author Comment

by:EddieShipman
ID: 22627832
Nah, we've tried casting and it still gives the error because the value in the DataTable is
still an Integer and you can't use the like operator on integers.
0
 
LVL 10

Expert Comment

by:MoreHeroic
ID: 22628079
That's what I'm saying.  Cast it as part of the expression so that you are in fact using a string.  That's what the Convert(col_name, 'System.String') in the expression would do.  It would still be an integer in the data, you would just cast it for the like comparison.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 26

Author Comment

by:EddieShipman
ID: 22628997
Oh, I see, but how about datetime values? I can't use LIKE with a partial date
like '06/*' can I? And I certainly can't convert that to a datetime value.

I AM able to determine what the datatype is for each column and do something different for each column, however, knowing what the user is actually searching for is the problem.
He may have an integer column and a datetime column and, in the txtFilter_TextChanged event, the user may enter '06' which would work ok for the Integer but when, he typed the '/', it would break for both datatypes.

Also, in the Convert, wouldn't you convert to the SQL type .vs System.String?



0
 
LVL 10

Expert Comment

by:MoreHeroic
ID: 22633062
You should be able to if the column you're searching is always cast to a string.  As long as the partial search string is the same syntax as your dates you should be fine.
0
 
LVL 10

Expert Comment

by:MoreHeroic
ID: 22633097
You may find this link helpful when dealing with expressions:

http://msdn.microsoft.com/en-us/library/system.data.datacolumn.expression.aspx
0
 
LVL 26

Author Closing Comment

by:EddieShipman
ID: 31502473
Works like a charm. I never thought about using Convert.
0

Featured Post

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to repeat the data 4 43
Send SMS from vb.net desktop app 30 51
Read oracle BLOB in vb.net 2 24
vb.net make textbox not visible after 1 minute elapsed 6 26
I think the Typed DataTable and Typed DataSet are very good options when working with data, but I don't like auto-generated code. First, I create an Abstract Class for my DataTables Common Code.  This class Inherits from DataTable. Also, it can …
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question