1

I have a dropdown in my application, which should be filled with values which are based on a session variable.

<asp:SqlDataSource
      id="SqlDataSource3"
      runat="server"
      ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
      ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
      SelectCommand="SELECT ID_SHOP
                     FROM SHOP
                     WHERE ID_SHOP_CITY = ?">
      <SelectParameters>
          <asp:SessionParameter
            Name="selectedCityId"
            SessionField="selectedCityId"
            DefaultValue="5" />
      </SelectParameters>
  </asp:SqlDataSource>

based on this example: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sessionparameter.aspx

I get syntax error - invalid character.

If I change my code:

<asp:SqlDataSource
      id="SqlDataSource3"
      runat="server"
      ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
      ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
      SelectCommand="SELECT ID_SHOP
                     FROM SHOP
                     WHERE ID_SHOP_CITY = 1">
      <SelectParameters>
          <asp:SessionParameter
            Name="selectedCityId"
            SessionField="selectedCityId"
            DefaultValue="5" />
      </SelectParameters>
  </asp:SqlDataSource>

and it works. However I don't need that where clause, I need to use the session variable. How can I fix it?

2
  • I think all you need to do here is change the = 1 to = @selectedCityId Commented Jan 11, 2014 at 19:11
  • The other thing the example in the link does is end the sql statement with a ; Commented Jan 11, 2014 at 19:13

1 Answer 1

2

The example you are pointing is using Odbc Provider, which uses ? for parameter. But you are using SqlClient provider, which expects @ParamName for parameter. You can check the differences here: Using Parameters with the SqlDataSource Control.

You have to change your markup to this:

<asp:SqlDataSource
      id="SqlDataSource3"
      runat="server"
      ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
      ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
      SelectCommand="SELECT ID_SHOP
                     FROM SHOP
                     WHERE ID_SHOP_CITY = @selectedCityId">
    <SelectParameters>
        <asp:SessionParameter
            Name="selectedCityId"
            SessionField="selectedCityId"
            DefaultValue="5" />
    </SelectParameters>
</asp:SqlDataSource>
Sign up to request clarification or add additional context in comments.

1 Comment

+1 for pointing out the difference in specifying parameters with Odbc provider vs SqlClient provider

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.