Solved

Howto use multiple QueryStringParameters in a SelectCommand for a MySQL SqlDataSource

Posted on 2008-10-29
2
998 Views
Last Modified: 2012-05-05
I am trying to use multiple QueryStringParameters in a SelectCommand for a MySQL SqlDataSource. I can use 1 just fine like this:

[CODE]
<asp:SqlDataSource ID="db_faa" runat="server"
                ConnectionString="Dsn=my_faa;uid=root;pwd=root"
                ProviderName="System.Data.Odbc"
                SelectCommand="SELECT faa_part, faa_part_desc, faa_status FROM tbl_faa_tracking WHERE faa_pl_desc = ?">
                <SelectParameters>
                       <asp:QueryStringParameter Name="value" QueryStringField="value" />
                </SelectParameters>
            </asp:SqlDataSource>
[/CODE]

and a query string like this:  ?value=TE456

But I actually need to pass in the field name as well like this:  ?field=faa_pl_desc&value=TE456
so I tried changing the code to this:

[CODE]
<asp:SqlDataSource ID="db_faa" runat="server"
                ConnectionString="Dsn=my_faa;uid=root;pwd=root"
                ProviderName="System.Data.Odbc"
                SelectCommand="SELECT faa_part, faa_part_desc, faa_status FROM tbl_faa_tracking WHERE ? = ?">
                <SelectParameters>
                    <asp:QueryStringParameter Name="field" QueryStringField="field" />
                    <asp:QueryStringParameter Name="value" QueryStringField="value" />
                </SelectParameters>
            </asp:SqlDataSource>
[/CODE]

I get no error, I just get a blank data set. It is as if the Select is returning no data even tho it has the same field name and search value in both instances. I have tired reversing the order of the 2 QueryStringParameters just in case the parameters are in a stack instead of a FIFO. No joy...

I think I just must be passing the 2 Query strings with some syntax missing?

Dave
0
Comment
Question by:davecove
2 Comments
 
LVL 32

Assisted Solution

by:Daniel Wilson
Daniel Wilson earned 100 total points
ID: 22831528
You can't use a parameter value as a field name.  What you have is translating as:

SELECT faa_part, faa_part_desc, faa_status FROM tbl_faa_tracking WHERE 'faa_pl' = 'TE465'

And that is going to return no records.

The number of valid fields is going to be finite, so coding for each branch is reasonable:

SELECT faa_part, faa_part_desc, faa_status FROM tbl_faa_tracking
WHERE
 (@Field = 'faa_pl' and faa_pl = @Value)
OR   (@Field = 'Field2' and Field2 = @Value)
OR   (@Field = 'Field3' and Field3 = @Value)
0
 

Accepted Solution

by:
davecove earned 0 total points
ID: 22831809
Or something like that...
When you use MySQL you don't get to use things like @Field and @Value... you can only use a ? and then put the QueryStringParameters in the order you want to use them in the Select.
So your suggestion turns into:
SELECT faa_part, faa_part_desc, faa_status FROM tbl_faa_tracking
WHERE
(? = 'faa_pl' and faa_pl = ?)
OR   (? = 'Field2' and Field2 = ?)
OR   (? = 'Field3' and Field3 = ?)
followed by 6 QueryStringParameters to take care of the 6 question marks. Like so:
<asp:QueryStringParameter Name="field" QueryStringField="field" />
<asp:QueryStringParameter Name="value" QueryStringField="value" />
<asp:QueryStringParameter Name="field" QueryStringField="field" />
<asp:QueryStringParameter Name="value" QueryStringField="value" />
<asp:QueryStringParameter Name="field" QueryStringField="field" />
<asp:QueryStringParameter Name="value" QueryStringField="value" />  
Which is just fine with me... because it works!
Dave
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
Composite queries are used to retrieve the results from joining multiple queries after applying any filters. UNION, INTERSECT, MINUS, and UNION ALL are some of the operators used to get certain desired results.​
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now