Welcome to Dream.In.Code
Become a VB.NET Expert!

Join 150,097 VB.NET Programmers for FREE! Get instant access to thousands of VB.NET experts, tutorials, code snippets, and more! There are 1,833 people online right now. Registration is fast and FREE... Join Now!




Dynamic Query on ColumnName in VB2008

 
Reply to this topicStart new topic

Dynamic Query on ColumnName in VB2008

robertelder
6 Jun, 2008 - 12:18 AM
Post #1

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 30



Thanked: 3 times
My Contributions
I posted this question on the Microsoft VB.Net forum a while ago, but nobody's replied yet.

I am working with a program that collects a lot of data about applicants, and saves that data as an XML file, 1 file per applicant. The program's main screen is a grid that displays a few columns in order to help the user select the correct record for viewing/editing. That grid was not designed to allow the user to determinie which columns will be displayed, to export the contents of the grid, or to be queryable.

I am building a tool application (using VB2008) that finds all of the XML files, loads them into a dataset and displays the data in a datagridview, allows users to choose which columns will be displayed, allows users to use simple queries to narrow the results, and allows the data to be exported to an Excel spreadsheet.

Everything else is working fine, but I am having problems with the query portion. If the user wants to run a query, the application opens up a new form called frmSimpleQuery which contains a listbox (has all column names) and a textbox. The user selects a column name from the listbox and provides a value to check against in the textbox. When the user clicks btnRUN, the program is supposed to create a simple query and execute it displaying the results in the datagridview.

The following is the code I currently use for finding data based on the Employer column:
CODE

Dim strValue As String = tbxValue.Text
    Dim PD = _
        From c In doc...<MAPT1> _
        Where c.<Employer>.Value = strValue _
        Order By c.<LNAM>.Value _
        Select New With {c.<FName>.Value, c.<MName>.Value, c.<LName>.Value}
    datagridview1.DataSource = PD.ToList


Although this query does work, it is not efficient. There are over 100 columns in the XML files i am working with. In order to select all columns, I would have to include all of the column names in the SELECT statement. Also, in order to allow the user to search against other columns, I would have to rewrite this code (well, copy and paste) modifying the WHERE clause for each of the columns. Like I said... it is not efficient.

Two questions:

1. Does anybody know how to make the WHERE clause dynamic? Maybe something like:
CODE

  Dim strValue As String = tbxValue.Text
        Dim PD = _
            From ... _
            [color=#FF0000]Where c.<ListboxColumnName.text>.value = strValue _[/color]
            Select ...
        datagridview1.DataSource = PD.ToList



2. Does anybody know how to make the SELECT statement grab the values from all columns and still be usable with a DataGridView? Maybe something like:
CODE

Dim strValue As String = tbxValue.Text
        Dim PD = _
            From ... _
            Where ... _
            [color=#FF0000]Select New With {c.<*>.Value}[/color]
        datagridview1.DataSource = PD.ToList


Thank you very much for any help you are able to provide.

-Rob

User is offlineProfile CardPM
+Quote Post

robertelder
RE: Dynamic Query On ColumnName In VB2008
6 Jun, 2008 - 12:26 AM
Post #2

New D.I.C Head
*

Joined: 5 Jun, 2008
Posts: 30



Thanked: 3 times
My Contributions
Oops.

This was my first time posting at DreamInCode.

I used the TEXT COLOR icon to try and highlight parts of the code sample. Instead of changing the color it looks like it just added the tags specifying the color around the text.

Turns out the tags do change the color of text, but they do not seem to work in code sections.

Please overlook those tags when considering a reply.

-Rob

This post has been edited by robertelder: 6 Jun, 2008 - 12:29 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 12:23AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live VB.NET Help!

VB.NET Tutorials

Reference Sheets

VB.NET Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month