ASP.NET DropDownList posting ""

Posted by Daniel on Stack Overflow See other posts from Stack Overflow or by Daniel
Published on 2010-06-01T10:47:00Z Indexed on 2010/06/01 10:53 UTC
Read the original article Hit count: 308

Filed under:
|
|

I am using ASP.NET forms version 3.5 in VB

I have a dropdownlist that is filled with data from a DB with a list of countries

The code for the dropdown list is

         <label class="ob_label">
            <asp:DropDownList ID="lstCountry" runat="server" CssClass="ob_forminput">
            </asp:DropDownList>
         Country*</label>

And the code that the list is

 Dim selectSQL As String = "exec dbo.*******************"

    ' Define the ADO.NET objects.
    Dim con As New SqlConnection(connectionString)
    Dim cmd As New SqlCommand(selectSQL, con)
    Dim reader As SqlDataReader

    ' Try to open database and read information.
    Try
        con.Open()
        reader = cmd.ExecuteReader()

        ' For each item, add the author name to the displayed
        ' list box text, and store the unique ID in the Value property.

        Do While reader.Read()
            Dim newItem As New ListItem()
            newItem.Text = reader("AllSites_Countries_Name")
            newItem.Value = reader("AllSites_Countries_Id")
            CType(LoginViewCart.FindControl("lstCountry"), DropDownList).Items.Add(newItem)
        Loop
        reader.Close()

        CType(LoginViewCart.FindControl("lstCountry"), DropDownList).SelectedValue = 182

    Catch Err As Exception
        Response.Redirect("~/error-on-page/")

        MailSender.SendMailMessage("*********************", "", "", OrangeBoxSiteId.SiteName & " Error Catcher", "<p>Error in sub FillCountry</p><p>Error on page:" & HttpContext.Current.Request.Url.AbsoluteUri & "</p><p>Error details: " & Err.Message & "</p>")
        Response.Redirect("~/error-on-page/")
    Finally
        con.Close()
    End Try

When the form is submitted an error occurs which says that the string "" cannot be converted to the datatype integer. For some reason the dropdownlist is posting "" rather than the value for the selected country.

© Stack Overflow or respective owner

Related posts about ASP.NET

Related posts about forms