views:

453

answers:

3

I have a page with 3 dropdownlist, 2nd and 3rd dropdownlist are added with CascadingDropDown. 3rd dropdownlist will take parameters from 1st and 2nd dropdownlist. So, in current example for CascadingDropDown i have found from google, they are only passing one parameter to the WebService method. How can pass two parameters to the service method, so that my 3rd dropdownlist will based on the SelectedValue of 1st and 2nd dropdownlist?

<WebMethod()> _
Public Function GetTeams(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
    Dim strConnection As String = ConfigurationManager.ConnectionStrings("nerdlinessConnection").ConnectionString
    Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
    Dim strTeamQuery As String = "SELECT * FROM TEAM WHERE conf_id = @confid"
    Dim cmdFetchTeam As SqlCommand = New SqlCommand(strTeamQuery, sqlConn)

    Dim dtrTeam As SqlDataReader
    Dim kvTeam As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

    Dim intConfId As Integer

    If Not kvTeam.ContainsKey("Conference") Or Not Int32.TryParse(kvTeam("Conference"), intConfId) Then
        Return Nothing
    End If

    cmdFetchTeam.Parameters.AddWithValue("@confid", intConfId)
    Dim myTeams As New List(Of CascadingDropDownNameValue)

    sqlConn.Open()
    dtrTeam = cmdFetchTeam.ExecuteReader

    While dtrTeam.Read()
        Dim strTeamName As String = dtrTeam("team_name").ToString
        Dim strTeamId As String = dtrTeam("team_id").ToString

        myTeams.Add(New CascadingDropDownNameValue(strTeamName, strTeamId))
    End While

    Return myTeams.ToArray
End Function

This is the sample code i found! As you can see in the code, '@confid' will be passed from 2nd dropdownlist! So, hw do i modify this code to get the selected value from 1st dropdownlist as well??

A: 

Which web service are you referring to? Is it something you have written or someone else's webservice?

In case it is your webservice, update the method definition in it and pass two parameters. In case it is somone else's, contact the concerned person to know what best can be done.

danish
A: 

It appears the poster is not asking about Web Services really, but about SqlCommand and adding parameters.

First, you should never EVER run sql straight from your web application like that. Put it in a stored procedure.

Second, you should run checks on the values coming in, because this is a good way for your web site users to use SQL injection attacks.

Now... Here is what you were looking for:

Dim strTeamQuery As String = "SELECT * FROM TEAM WHERE conf_id = @confid"

becomes

Dim strTeamQuery As String = "SELECT * FROM TEAM WHERE conf_id = @confid AND second_id = @secondId"

Then just add another one of these:

cmdFetchTeam.Parameters.AddWithValue("@confid", intConfId)

(with the other value, of course, like this)

cmdFetchTeam.Parameters.AddWithValue("@confid", intConfId)
cmdFetchTeam.Parameters.AddWithValue("@secondId", intSecondId)
Gabriel McAdams
I modified my answer to solve your problem. Remember to accept this answer if it helps.
Gabriel McAdams
A: 

The AJAX CascadingDropDown control is designed to populate 1 dropdown based on the selected value from another dropdown. Not to populate 1 dropdown based on the selected value from two dropdowns.

There is a way to get around this - take a look at the "ContextKey". You could use some javascript on the client side to set the ContextKey of the CascadingDropDown extender on the selected index changed event.

In this case though, i would recommend not using the CascadingDropDown extender if you want to pass multiple values to a web service. Just use jQuery and AJAX to call your webservice, parse the response (preferably in JSON format) and bind to the dropdown. Plenty of examples around - look for $.getJSON.

RPM1984