Dynamic drop down list

J

jagdishl

Hi:
I have to basically populate another dropdown list based on the first
dropdown list.My first dropdown list contains only two values i.e
1.City
2.County
So the second dropdownlist should contain the values of either one of
those based on the selection.I have used the followind code in the
"Indexchanged" event of the first dropdown list.

Private Sub ddlprogtype_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlprogtype.SelectedIndexChanged
Connection.Open()
Dim X As String = ddlprogtype.SelectedValue
lbldisplay.Text = ddlprogtype.SelectedValue
Command.CommandText = "Select names from tblregions where type
= " + "@x"
DataReader = Command.ExecuteReader
ddlnames.DataSource = DataReader
ddlnames.DataBind()
Connection.Close()

End Sub


Upon compiling I am getting the following error message

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object with error being pointed to the
following line
\Line 79: Command.CommandText = "Select places from tblRegions
where type = " + "@x"

Kindly advise on how I should tackle this error.Thanks
Jagdish.l
 
B

Bruce Barker

try setting Command to a valid object. then it'll blow up because you
haven't added the parameter referenced in the commandtext.

-- bruce (sqlwork.com)
 
J

Jagdish

Hi:
I have to basically populate another dropdown list based on the first
dropdown list.My first dropdown list contains only two values i.e
1.City
2.County
So the second dropdownlist should contain the values of either one of
those based on the selection.I have used the followind code in the
"Indexchanged" event of the first dropdown list.

Private Sub ddlprogtype_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlprogtype.SelectedIndexChanged
Connection.Open()
Dim X As String = ddlprogtype.SelectedValue
lbldisplay.Text = ddlprogtype.SelectedValue
Command.CommandText = "Select names from tblregions where type
= " + "@x"
DataReader = Command.ExecuteReader
ddlnames.DataSource = DataReader
ddlnames.DataBind()
Connection.Close()

End Sub


Upon compiling I am getting the following error message

Exception Details: System.NullReferenceException: Object reference not
set to an instance of an object with error being pointed to the
following line
\Line 79: Command.CommandText = "Select places from tblRegions
where type = " + "@x"

Kindly advise on how I should tackle this error.Thanks
Jagdish.l

Hi:

I decided to make the program much more simple by making the following
modifications

Private Sub ddlprogtype_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlprogtype.SelectedIndexChanged
Connection.Open()
lbldisplay.Text = ddlprogtype.SelectedValue
Dim X As String = ddlprogtype.SelectedValue
If X = "County" Then
Command.CommandText = "Select Countyname from tblcounties"
ElseIf X = "City" Then
Command.CommandText = "Select Cityname from tblcities"
End If
DataReader = Command.ExecuteReader
ddlnames.DataSource = DataReader
ddlnames.DataBind()
Connection.Close()
This took care of the error message but the dropdownlist is not being
populated by the values..What is wrong?
Thanks



Jagdish.l
 
J

jagdishl

Hi:
Thanks for the above tip.To make it more simple I replaced it with
the following code

Private Sub ddlprogtype_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs) Handles
ddlprogtype.SelectedIndexChanged
Connection.Open()
lbldisplay.Text = ddlprogtype.SelectedValue
Dim X As String = ddlprogtype.SelectedValue
If X = "County" Then
Command.CommandText = "Select Countyname from tblcounties"
ElseIf X = "City" Then
Command.CommandText = "Select Cityname from tblcities"
End If
DataReader = Command.ExecuteReader
ddlnames.DataSource = DataReader
ddlnames.DataBind()
Connection.Close()

This took care of the error message but the dropdown list is not
getting populated by the values.It is still blank..Do I need to add any
further piece of code to the seconddrop down list...such as databinding
etc...I would really appreciate your advice..
Thanks once again.

Jagdish.l
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,998
Messages
2,570,242
Members
46,834
Latest member
vina0631

Latest Threads

Top