• Hello and welcome to our new forums. We upgraded our forum sites to a more robust and modern system which we hope you will enjoy. Be sure to check out your profile by clicking the button on the top right and configure your preferences, signature, time zone, avatar, etc. as you wish. If you need help with using this new forum'ware try the help link on the bottom right.

    Click here to review your account now.

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:
Code:
        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:
Code:
        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
 

jmcilhinney

VB.NET Forum Moderator
Staff member
Joined
Aug 17, 2004
Messages
14,027
Location
Sydney, Australia
Programming Experience
10+
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.
 

badeye

New member
Joined
May 19, 2010
Messages
3
Location
UK
Programming Experience
Beginner
Cheers, I finally figured it out, it was the autopostback that was creating an issue.

All sorted now.

Thanks,
 
Top Bottom