Stack Overflow is a community of 4.7 million programmers, just like you, helping each other.

Join them; it only takes a minute:

Sign up
Join the Stack Overflow community to:
  1. Ask programming questions
  2. Answer and help your peers
  3. Get recognized for your expertise

I would like to use a selected value of a dropdown list in a SqlDataSource SelectCommand.

                <asp:DropDownList ID="ddlSelectRole" runat="server" ClientIDMode="Static">
                    <asp:ListItem></asp:ListItem>
                    <asp:ListItem>Client</asp:ListItem>
                    <asp:ListItem>Programming</asp:ListItem>
                    <asp:ListItem>Guest</asp:ListItem>
                </asp:DropDownList>

Currently:

<asp:SqlDataSource ID="dsourceProgEmails" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>" 
    SelectCommand="SELECT  [Email] + ',' AS [text()] FROM [SiteUsers] WHERE [Role] = 'Programming' FOR XML PATH ('')">
</asp:SqlDataSource>

I'm thinking something like:

<asp:SqlDataSource ID="dsourceProgEmails" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ProjectConnectionString %>" 
    SelectCommand="SELECT  [Email] + ',' AS [text()] FROM [SiteUsers] WHERE [Role] = '" + ddlSelectRole.SelectedValue + "' FOR XML PATH ('')">
</asp:SqlDataSource>

Code-behind is C#.

share|improve this question
    
Did you try autopostback="true"? – yog2411 Jun 6 '13 at 20:38
    
Write your query with passing parameter using @Role and then try this inside selectparameters tag: <asp:ControlParameter ControlID="ddlSelectRole" DefaultValue="0" Name="Role" PropertyName="SelectedValue" Type="Int32" /> – yog2411 Jun 6 '13 at 20:45
up vote 4 down vote accepted

Your query should look like this in your SqlDataSource:

SelectCommand="SELECT [Email] + ',' AS [text] FROM [SiteUsers] WHERE [Role] = @RoleID FOR "

Then specify where to get the value for RoleID here

<SelectParameters>
   <asp:ControlParameter Name="RoleID" ControlID="ddlSelectRole" 
        PropertyName="SelectedValue" />
</SelectParameters>
share|improve this answer
    
Hi @codingbiz, I'm getting the error 'Could not find control 'ddlSelectRole' in ControlParameter 'RoleID'.' This is because the dropdown list is in an <InsertItemTemplate> of a FormView. How to fix this? – kyle_13 Jun 11 '13 at 17:45
1  
@kyle_13 the control you put in the InsertTemplate will only be visible at runtime and not in design mode. So, SqlDataSource would not be able to locate the control on the page. You should move the SqlDataSource also into the InsertTemplate. – codingbiz Jun 11 '13 at 18:40
    
Hi @codingbiz, it's not letting me go further because I reference that datasource in another spot in the code-behind, in the onInserted event of a record, which sends a confirmation email out: DataView dvProgEmails = (DataView)dsourceProgEmails.Select(DataSourceSelectArguments.Empty); string emailsTo = (string)dvProgEmails.Table.Rows[0][0]; emailsTo = emailsTo.TrimEnd(','); Gives me the error: Error - The name 'dsourceProgEmails' does not exist in the current context – kyle_13 Jun 11 '13 at 19:27
    
Hi @codingbiz, never mind, I figured it out, all I had to do was use FindControl after moving the datasource. SqlDataSource dsourceToEmails = (SqlDataSource)formViewNewItem.FindControl("dsourceToEmails"); Thanks so much, all is good now! – kyle_13 Jun 11 '13 at 20:13
    
Cool you figured that out. I should have mentioned that in my comment. – codingbiz Jun 12 '13 at 11:58

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.