0

I have a database of items that are available, but the availability varies by company. There are about 10 companies, each with their own column and a bit value if it is available to them.

This is being done on a SharePoint site in Designer, and I don't have access to the code behind.

|  PhoneMake   |  PhoneModel   |  CompanyA   |  CompanyB   |
|--------------|---------------|-------------|-------------|
|    Apple     |   iPhone 5c   |   True      |   False     |
|    Apple     |   iphone 5s   |   True      |   True      |
|    Android   |   Note 3      |   False     |   False     |
|    Android   |   Note 4      |   False     |   True      |

On my webpage, I have a dropdown to select your company. I then want it to select the next level of filtering (ie. manufacturer)

<asp:DropDownList runat="server" id="Company" AutoPostBack="True" EnableViewState="False">
    <asp:ListItem></asp:ListItem>
    <asp:ListItem Value="CompanyA">Choose A Company</asp:ListItem>
    <asp:ListItem Value="CompanyB">Choose B Company</asp:ListItem>
</asp:DropDownList>

I need to take the selection of this dropdown and input it as a column to filter on in my SqlDataSource.

<asp:SqlDataSource id="SqlDataSource_PhoneMake" runat="server" __designer:commandsync="true" ProviderName="System.Data.SqlClient" ConnectionString="--yada yada--"
SelectCommand="SELECT DISTINCT [PhoneMake] FROM [table] WHERE (([PhoneMake] IS NOT NULL) AND ([@Company] = 'True'))">
    <SelectParameters>
        <asp:controlparameter ControlID="Company" PropertyName="SelectedValue" Name="Company" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
5
  • What is the problem you are facing with the attempt you made? Commented Jan 5, 2015 at 22:36
  • It does not work. When you make a selection an error gets thrown. Commented Jan 5, 2015 at 22:37
  • The reason is you can't use a parameter in order to define the column used in the query. Parameters can only be used to define column values in the predicates of the where clause. Commented Jan 5, 2015 at 22:45
  • That's what I was afraid of. Sounds like I need to re-do the Select statement somehow then Commented Jan 5, 2015 at 22:49
  • You can have a look at the query I suggest in my answer Commented Jan 5, 2015 at 23:35

2 Answers 2

1

Use the following SelectCommand instead:

SELECT DISTINCT [PhoneMake]
FROM [table]
WHERE ([PhoneMake] IS NOT NULL) AND
      ([CompanyA] = (CASE @Company WHEN 'CompanyA' THEN 1 ELSE [CompanyA] END)) AND 
      ([CompanyB] = (CASE @Company WHEN 'CompanyB' THEN 1 ELSE [CompanyB] END))

To explain the above a little bit, if, e.g. @Company = 'CompanyA' then the 3rd predicate in the WHERE clause:

 ([CompanyB] = (CASE @Company WHEN 'CompanyB' THEN 1 ELSE [CompanyB] END))

becomes:

([CompanyB] = [CompanyB])

and is thus ignored. What is left is exactly the conditions required:

 ([PhoneMake] IS NOT NULL) AND ([CompanyA] = 1)
Sign up to request clarification or add additional context in comments.

1 Comment

This does exactly what I wanted. Thank you!!
0

If someone else knows a better way, then please chime in.

But without access to the code behind I can only think of something like this:

 <% SqlDataSource_PhoneMake.SelectCommand = String.Format("SELECT DISTINCT [PhoneMake] FROM [table] WHERE (([PhoneMake] IS NOT NULL) AND ({0} = 'True'))", Company.SelectedValue); %>
<asp:SqlDataSource id="SqlDataSource_PhoneMake" runat="server" __designer:commandsync="true" ProviderName="System.Data.SqlClient" ConnectionString="--yada yada--"
SelectCommand="">
</asp:SqlDataSource>

I Think someone could technically change your dropdown values in Firebug and you'd be open to SQL Injection would be a possible concern.

So a SQL Query would be best with a parameter if possible.

2 Comments

What about the query proposed by my answer?
@GiorgosBetsos if he can get it to work then you're solution is much better than mine. I'm just not sure if the DataSource will accept it without giving the same error he's already facing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.