Avatar of Mo
Mo

asked on 

DropDown list returning error when null value

Hello;

I am having an issue when binding data to a drop down box if the value is null.

I have two hidden labels which shows the value of the fields on page load
I then pass this into to the databind and I call my function to retrieve the drop-down box
When there is a value found all works perfect but when there is no value selected it throws an error.

 FST.Text = dr("FST").ToString()
                    FST_ID.Text = dr("FST_ID").ToString()


                    '  If FST.Text <> "" Then
                    Dim sFST As String = FST.Text
                    Dim iFST_ID As String = FST_ID.Text
                    Call GetDropDown(sFST, iFST_ID)

Open in new window



Below is a function I use to get my dropdown box

 Public Sub GetDropDown(ByVal FST As String, ByVal FST_ID As String)

        Dim constr As String = ConfigurationManager.ConnectionStrings("xxx").ConnectionString

        Using con = New SqlConnection(constr)
            Try
                Dim cmd As SqlCommand = New SqlCommand()

                With cmd
                    .Connection = con
                    .Connection.Open()
                    .CommandText = "spANF_GET_FIBRE_SOLUTION_TYPE"
                    .CommandType = CommandType.StoredProcedure
                End With


                Using dr As SqlDataReader = cmd.ExecuteReader()
                    If dr.HasRows Then
                        ddlFST.DataSource = dr
                        ddlFST.DataTextField = "FST"
                        ddlFST.DataValueField = "FST_ID"
                        ddlFST.DataBind()


                        ' ddlFST.Items.Insert(0, New ListItem("--Select Item--", "0"))
                        ' Dim iFST_ID1 As String = Request.Form(ddlFST.UniqueID)
                        If (ddlFST.Items.Count > 0) Then
                            ddlFST.Items.FindByText(FST).Selected = True
                            ddlFST.Items.FindByValue(FST_ID).Selected = True
                        Else
                            ddlFST.Items.Insert(0, New ListItem("--Select Item--", "0"))
                            ddlFST.SelectedIndex = 0

                        End If
                        End If
                End Using

                con.Close()

            Catch ex As System.Data.SqlClient.SqlException
                Dim msg As String = "Fetch Error:"
                msg += ex.Message
                Throw New Exception(msg)
            Finally
                con.Close()
            End Try

        End Using

    End Sub

Open in new window


Any help with this would be greatly appreciated.

Thans
ASP.NET* DropdownList* databinding

Avatar of undefined
Last Comment
Mo

8/22/2022 - Mon