Results 1 to 3 of 3

Thread: How to get a value from dropdownlist

  1. #1
    .NET Framework
    .NET 2.0
    Join Date
    May 2010
    Location
    UK
    Posts
    3
    Reputation
    0

    How to get a value from dropdownlist

    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

  2. #2
    .NET Framework
    .NET 4.0
    Join Date
    Aug 2004
    Location
    Sydney, Australia
    Posts
    14,001
    Reputation
    1730
    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.

  3. #3
    .NET Framework
    .NET 2.0
    Join Date
    May 2010
    Location
    UK
    Posts
    3
    Reputation
    0
    Cheers, I finally figured it out, it was the autopostback that was creating an issue.

    All sorted now.

    Thanks,

Similar Threads

  1. If else in a dropdownlist
    By Vadz in forum VB.NET General Discussion
    Replies: 1
    Last Post: 03-18-2013, 12:37 AM
  2. Replies: 2
    Last Post: 05-16-2010, 1:52 PM
  3. edittable Dropdownlist
    By liptonIcedTea in forum ASP.NET General Discussion
    Replies: 3
    Last Post: 08-30-2007, 4:19 AM
  4. How can I select a dropdownlist?
    By Ennio in forum ASP.NET General Discussion
    Replies: 3
    Last Post: 03-13-2006, 9:54 PM
  5. Replies: 0
    Last Post: 02-24-2006, 2:23 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •