Board index » Web Programming » Dynamic drop down list

Dynamic drop down list

Web Programming191
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


-
 

Re:Dynamic drop down list

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)





<jagdishl@gmail.com>wrote in message

Quote
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







-

Re:Dynamic drop down list

"jagdishl@gmail.com" wrote:

Quote
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



--

Sent via .NET Newsgroups

www.dotnetnewsgroups.com">www.dotnetnewsgroups.com

-

Re:Dynamic drop down list

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



-