Hi

I am using 2 dropdownlists such that corresponding to the item selected in the 1st dropdownlist only the related items will appear in the 2nd dropdownlist. The problem is that the 2nd dropdownlist is never populated. My code is:

Sub popdropdownlist1()
Dim myConnection1 As New SqlConnection(ConfigurationSettings.AppSettings("connectionString1"))
Const strSQLC As String = "select distinct name1, RTRIM(code1)as code1 from tabel1 where name1<> '' ORDER BY code1"
Dim myCommandC As New SqlCommand(strSQLC, myConnection1)
myConnectionC.Open()
Dim objDRC As SqlDataReader
objDRC=myCommandC.ExecuteReader(CommandBehavior.CloseConnection)

dropdownlist1.DataSource = objDRC
dropdownlist1.DataMember = "Table"
dropdownlist1.DataTextField = "name1"
dropdownlist1.DataValueField = "code1"
dropdownlist1.DataBind()
dropdownlist1.Items.Insert(0, New ListItem("-- Choose Name --"))
End Sub

The code to populate dropdownlist2 is:

Public Sub dropdownlist1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dropdownlist1.SelectedIndexChanged

Dim myConnectionD As New SqlConnection(ConfigurationSettings.AppSettings("connectionString1"))

Dim strV As String
strV = RTrim(dropdownlist1.SelectedItem.Value)
Const strSQLD As String = "select distinct deptCode,deptName From view1,table2 where deptCode like 'strV%' order by deptCode"
Dim myCommandD As New SqlCommand(strSQLD, myConnectionD)
myConnectionD.Open()
Dim objDRD As SqlDataReader
objDRD=myCommandD.ExecuteReader(CommandBehavior.CloseConnection)
dropdownlist2.DataSource = objDRD
dropdownlist2.DataMember = "Table"
dropdownlist2.DataTextField = "deptCode"
dropdownlist2.DataBind()
dropdownlist2.Items.Insert(0, New ListItem("-- Choose Dept --"))
end sub

So, i am passing the selected value of dropdownlist1 through string variable strV but never getting the dropdownlist2 populated although, strV is getting the value passed by dropdownlist1. If i hardcode it means if instaeed of parameter strV i pass some value in the select query of dropdownlist2, then the corresponding values do appear in dropdownlist2.
Also,AUTOPOSTBACK of both the dropdownlists is TRUE.

Please suggest some solution for this.

Regards
Monica

Recommended Answers

All 7 Replies

Is your column deptCode string (varchar, nvarchar ...) type?. LIKE operator should be used only for string values.

Is your column deptCode string (varchar, nvarchar ...) type?. LIKE operator should be used only for string values.

hello

try this
;)
Const strSQLD As String = "select distinct deptCode,deptName From view1,table2 where deptCode like '" + strV + "%' order by deptcode"

You have to keep strV outside the quotes as it is a variable

Yes that is right, I did not see that she put variable inside SELECT quotes. :)

hey Guys,

Thanks for your responses. Here is how i got it finally:

Earlier also I was trying the way ManicCW told me but i was getting the ERROR-'Constant expression required'.
So, now, i replaced Const strSQLD As String
with Dim strSQLD As String.

The 2nd dropdownlist is populated now.
Also deptCode is of type VARCHAR.

Thanks everybody!

Regards
Monica

The problem is in this line

Dim strV As String
strV = RTrim(dropdownlist1.SelectedItem.Value)
Const strSQLD As String = "select distinct deptCode,deptName From view1,table2 where deptCode like 'strV%' order by deptCode"

because It takes the value of deptCode like 'strV%' as it is .
It doesnt take the correctponding strV value from the Drop down list.

So try do like this

Const strSQLD As String = "select distinct deptCode,deptName From view1,table2 where deptCode like '" & strV & "%' order by deptCode"

All the very best.

The problem is in this line

Dim strV As String
strV = RTrim(dropdownlist1.SelectedItem.Value)
Const strSQLD As String = "select distinct deptCode,deptName From view1,table2 where deptCode like 'strV%' order by deptCode"

because It takes the value of deptCode like 'strV%' as it is .
It doesnt take the correctponding strV value from the Drop down list.

So try do like this

Const strSQLD As String = "select distinct deptCode,deptName From view1,table2 where deptCode like '" & strV & "%' order by deptCode"

All the very best.

Thanks!

hi to all, my problem is if a select any country from first dropdownlist then it should place only states of selected paticular country and if select another country it should place those states and so on..

can any one sought out my problem
thank youin advance.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.