How to get a value from dropdownlist

badeye

New member
Joined
May 19, 2010
Messages
3
Location
UK
Programming Experience
Beginner
Hi All,

I have populated a dropdown list with results from my SQL database and then added an additional entry called 'New Date' which sits as the top value in the list.
My issue is that when I select a value in the dropdown list it only ever appears to pull the value 'New Date' instead of the selected value.

my code for when the page first loads to populate the dropdown list:
VB.NET:
        Dim sqlConnection1 As New SqlConnection("Server=*******; Database=nimbus; user=********; password=*******")
        Dim cmd As New SqlCommand

        cmd.CommandText = "SELECT startdate FROM availability"
        cmd.CommandType = CommandType.Text
        cmd.Connection = sqlConnection1

        sqlConnection1.Open()

        DeliveryList1.DataSource = cmd.ExecuteReader()
        DeliveryList1.DataTextField = "startdate"
        DeliveryList1.DataValueField = "startdate"
        DeliveryList1.DataBind()

        sqlConnection1.Close()

        DeliveryList1.Items.Insert(0, New ListItem("New Date"))

my code for when I changed the selection in the dropdown list:
VB.NET:
        Dim sqlConnection1 As New SqlConnection("Server=**********; Database=nimbus; user=**********; password=*********")
        Dim cmd As New SqlCommand
        Dim reader As SqlDataReader
        Dim test As String

        cmd.CommandText = "SELECT * FROM availability WHERE startdate = " + DeliveryList1.SelectedValue
        cmd.CommandType = CommandType.Text
        cmd.Connection = sqlConnection1

        sqlConnection1.Open()

        reader = cmd.ExecuteReader()

        While reader.Read
            If reader("monam").ToString = True Then monam.Checked = True Else monam.Checked = False

        End While

P.S I will changing the connection strings to read from the web.config before I finish.

If anyone can help that would be great.

Thanks,

Simon
 
I'm not sure about the solution to your issue but how can this ever work:
cmd.CommandText = "SELECT * FROM availability WHERE startdate = " + DeliveryList1.SelectedValue

You would need single quotes or hash symbols or something around that value but you really ought to be using a parameter anyway. It's less of an issue in this case because the user is not entering free text but a malicious user might still manipulate the web page and cause that code to delete your entire database.
 
Cheers, I finally figured it out, it was the autopostback that was creating an issue.

All sorted now.

Thanks,
 
Back
Top