We help IT Professionals succeed at work.
Get Started

DropDown list returning error when null value

Last Modified: 2020-05-29

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)
                Dim cmd As SqlCommand = New SqlCommand()

                With cmd
                    .Connection = con
                    .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.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
                            ddlFST.Items.Insert(0, New ListItem("--Select Item--", "0"))
                            ddlFST.SelectedIndex = 0

                        End If
                        End If
                End Using


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

        End Using

    End Sub

Open in new window

Any help with this would be greatly appreciated.

Watch Question
Distinguished Expert 2020
This problem has been solved!
Unlock 1 Answer and 2 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE