what is wrong with my code?

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        MyConnection.Open()
       Dim strSQL As String = "SELECT * FROM Question WHERE SurveyID=83 Order By QuestionNum"
        Dim cnCommand1 As SqlCommand = New SqlCommand(strSQL, MyConnection)
        Dim Rdr As SqlDataReader = cnCommand1.ExecuteReader()    'execute reader to catch data from db to drop dwon lists

        With qnum
            .DataSource = Rdr
            .DataValueField = "QuestionID"
            .DataTextField = "QuestionNum"
            .DataBind()
        End With

        'Rdr.NextResult()
        'With lstOptions
        '    .DataSource = Rdr
        '    .DataValueField = "OptionID"
        '    .DataTextField = "OptionList"
        '    .DataBind()
        'End With
        'close the reader
        Rdr.Close()

        

    End Sub

    Sub SubmitBtn_Click(ByVal sender As Object, ByVal e As EventArgs)
        Dim questid As String = qnum.SelectedItem.Value
        For Each s As String In questid
            Dim sql As String = "SELECT * From AnswerList WHERE SurveyID=83 AND QuestionID='" & questid & "' Order By QuestionNum"
            Dim cmd As SqlCommand = New SqlCommand(sql, MyConnection)
            Dim reader As SqlDataReader
            reader = cmd.ExecuteReader

            With lstOptions
                .DataSource = reader
                .DataValueField = "OptionID"
                .DataTextField = "OptionList"
                .DataBind()
            End With

            reader.Close()

        Next

        'Close the DB Connection
        MyConnection.Close()
    End Sub

i want to populate the lstOptions (a listbox) based on the item selected in frop down list named "qnum"

the problem is..the data in lstOptions changed only at first time the dropdown list value change...the 2nd,3rd change of the dropdownlist, the lstOptions value doesn't change

plzz help me...

The code you post has no handler for the SelectedIndexChange event of your drop down control, instead you have a button click handler. So to cause the List box to be rebound, in your programs current state, you need to make a selection in the drop down and then click the submit button every time to get a change in the list box.

The code you post has no handler for the SelectedIndexChange event of your drop down control, instead you have a button click handler. So to cause the List box to be rebound, in your programs current state, you need to make a selection in the drop down and then click the submit button every time to get a change in the list box.

can u elaborate more? i can't figured out where to add the handler? does it mean that i don't need a button click handler?
hope u can help me....your help is very much appreciated...

can u elaborate more? i can't figured out where to add the handler?

Same place you have the button click handler. You have managed to create a click event handler for your button, making a SelectedIndexChange handler for your drop down list is no different.

does it mean that i don't need a button click handler?

That's up to you and your design. Do you want the user to have to click a button after selecting a question? or do you want their selection to be actioned immediately they select it in the drop down control?

ok..
i made up my mind that their selection to be actioned immediately they select it in the drop down control...
if so, then i don't need the button right?
so..does it mean that i should add SelectedIndexChanged into my drop down control?

<asp:Dropdownlist id=qnum runat="server" DataValueField="QuestionID" DataTextField="QuestionNum" OnSelectedIndexChanged="populateOption()"/>

then at
sub populateOption(......)
--generate OptionList to lstOptions----

am i right until here?

ok...thanks hollystyles

now i've already got the solution...

i'm jumping all over my tv hall...yey!!!!

Thats it !

There you go, happy days.:cool:

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.