Link to home
Start Free TrialLog in
Avatar of tnapolitano
tnapolitano

asked on

ASP.NET - multiple dropdownlist


I am an ASP.Net page (.aspx) with 3 dropdownlist objects. The DDL's are populated by a database.

The problem is only the first DDL is displaying entries correctly. Thought the page compiles, the 2nd and 3rd DDL's are empty.

Let me know if you need me to paste the code (I'm figuring this is an easy one, but don't really know).
Avatar of Sammy
Sammy
Flag of Canada image

are your dropdownlists dependent on each others? If Yes then you need to populate the second and the third based on the value of the ddl they are dependent on. If none of the DDLs is depending on the other then you need to bind the their datasource to the datafield from your table.

It doesnt hurt to post some HTML and code behind where the ddl are populated

HTH
pls post the code and make sure that the tables are filled and the SQL statement usesd does retreive the required values

Rejo
Avatar of tnapolitano
tnapolitano

ASKER

Fixed it myself. Just did a couple of <asp:dropdownlist> and <asp:ListItem Value="">. Thanks, anyway.
Okay, I'm not done with this yet. I'm going to increase the points 'cause this is urgent.

The functionality I'm looking for is to be able to query via the dropdownlists with cascading (is that the term?) results. I have 3 DDL's: Roles, Levels and Category. So, when I select Role - Analyst it returns all records for Analyst.

But when I select Level - 1 I get all Level 1 records, not the Level 1 records associated with the Analyst role.

Here is my code:


    Function qryRole(ByVal role As String) As System.Data.DataSet
        Dim connectionString As String = "server='(local)'; trusted_connection=true; database='deputization'"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

        Dim queryString As String = "SELECT [dp_material].* FROM [dp_material] WHERE (CASE WHEN @Role <> 'All' AND [role] = @Role THEN 1 WHEN @Role = 'All' THEN 1 ELSE 0  END) = 1"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_role As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_role.ParameterName = "@Role"
        dbParam_role.Value = role
        dbParam_role.DbType = System.Data.DbType.StringFixedLength
        dbCommand.Parameters.Add(dbParam_role)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function



    Function qryLevel(ByVal level As String) As System.Data.DataSet
        Dim connectionString As String = "server='(local)'; trusted_connection=true; database='deputization'"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

        Dim queryString As String = "SELECT [dp_material].* FROM [dp_material] WHERE (CASE WHEN @level <> 'All' AND [level] = @Level THEN 1 WHEN @Level = 'All' THEN 1 ELSE 0  END) = 1"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_level As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_level.ParameterName = "@Level"
        dbParam_level.Value = level
        dbParam_level.DbType = System.Data.DbType.StringFixedLength
        dbCommand.Parameters.Add(dbParam_level)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function



    Function qryCategory(ByVal category As String) As System.Data.DataSet
        Dim connectionString As String = "server='(local)'; trusted_connection=true; database='deputization'"
        Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)

        Dim queryString As String = "SELECT [dp_material].* FROM [dp_material] WHERE (CASE WHEN @Category <> 'All' AND [category] = @Category THEN 1 WHEN @Category = 'All' THEN 1 ELSE 0  END) = 1"
        Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
        dbCommand.CommandText = queryString
        dbCommand.Connection = dbConnection

        Dim dbParam_category As System.Data.IDataParameter = New System.Data.SqlClient.SqlParameter
        dbParam_category.ParameterName = "@Category"
        dbParam_category.Value = category
        dbParam_category.DbType = System.Data.DbType.StringFixedLength
        dbCommand.Parameters.Add(dbParam_category)

        Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
        dataAdapter.SelectCommand = dbCommand
        Dim dataSet As System.Data.DataSet = New System.Data.DataSet
        dataAdapter.Fill(dataSet)

        Return dataSet
    End Function


Sub ddlRole_SelectedIndexChanged(sender As Object, e As EventArgs)

MXDataGrid1.DataSource = qryRole(ddlRole.Items(ddlRole.SelectedIndex).Text)
MXDataGrid1.DataBind()

End Sub

Sub ddlLevel_SelectedIndexChanged(sender As Object, e As EventArgs)

MXDataGrid1.DataSource = qryLevel(ddlLevel.Items(ddlLevel.SelectedIndex).Text)
MXDataGrid1.DataBind()

End Sub

Sub ddlCategory_SelectedIndexChanged(sender As Object, e As EventArgs)

MXDataGrid1.DataSource = qryCategory(ddlCategory.Items(ddlCategory.SelectedIndex).Text)
MXDataGrid1.DataBind()

End Sub
I have solved this question myself.

Dim queryString As String = "SELECT [dp_material].* FROM [dp_material] WHERE ([dp_material].[uID] = @uID)"

            Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand

            dbCommand.CommandText = queryString

            dbCommand.Connection = dbConnection

   

   

   

            If ddlRole.SelectedValue > 0 Then

            queryString += " AND [dp_material].[role]  = '" & ddlRole.SelectedItem.Value & "' "

            End If

   

            If ddlLevel.SelectedValue > 0 Then

            queryString += " AND [dp_material].[Level] = '" & ddlLevel.SelectedItem.Value & "' "

            End If

   

            If ddlCategory.SelectedValue > 0 Then

            queryString += " AND [dp_material].[Category] = '" & ddlCategory.SelectedItem.value & "' "

            End If



ASKER CERTIFIED SOLUTION
Avatar of DarthMod
DarthMod
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial