troubleshooting Question

DropDown list returning error when null value

Avatar of Mo
Mo asked on
ASP.NET* DropdownList* databinding
2 Comments1 Solution21 ViewsLast Modified:
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
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros