Databindig issue, I think


Jul 25, 2013
Programming Experience
So, here's the scenario:
I have a web form application that is basically data entry forms that are entered into a SQL database. The issue I am having is the editing of these forms. If a submitted form is retrieved from the database, some records cause an error upon loading the data, always in drop down list values. What I'd like to be able to do is fill the form and, using basically the same form as the data entry, edit the record and save the edits. The error that occurs is (for example) ''DDL6' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value". I believe this has to do with the method I am trying to use to retrieve the data, but the error is not consistent as to which drop down list produces the error. It could be virtually any DDL. Here's the code I am using to retrieve the records (where the error occurs, but on seemingly random DDL's):
    Protected Sub DropDownList6_SelectedIndexChanged(sender As Object, e As EventArgs) Handles DropDownList6.SelectedIndexChanged
        Dim conn As New SqlConnection("Data Source=server;Initial Catalog=DBName;User ID=user;Password=password")
        Dim cmd As New SqlCommand
        Dim reader As SqlDataReader
        cmd.CommandText = "select * from forma where rptnum = @rptnum"
        cmd.Parameters.Add("@rptnum", SqlDbType.NVarChar).Value = DropDownList6.SelectedValue
        cmd.CommandType = Data.CommandType.Text
        cmd.Connection = conn
        reader = cmd.ExecuteReader()
        While reader.Read()
            dt.Text = reader("date")
            inspname.SelectedValue = reader("inspname")
            inspphone.Text = reader("inspphone")
            inspmail.Text = reader("inspmail")
            TextBox28.Text = reader("inspname")
            lastinsp.Text = reader("lastinspdate")
            eform.Text = reader("forme")
            caseno.Text = reader("caseno")
            insptype.SelectedValue = reader("insptype")
            RBL1.SelectedValue = reader("tier")
            RBL7.SelectedValue = reader("grade")
            enddt.Text = reader("enddate")
            sttime.Text = reader("starttime")
            endtime.Text = reader("endtime")
            RBL2.SelectedValue = reader("drift")
            Tank.Text = reader("tanksamp")
            drift.Text = reader("driftcard")
            txt1080.Text = reader("gwpl")
            other.Text = reader("other")
            photos.Text = reader("photo")
            docs.Text = reader("docs")
            DDL2.SelectedValue = reader("pgp")
            DDL3.SelectedValue = reader("aap")
            RBL3.SelectedValue = reader("golf")
            DDL4.SelectedValue = reader("catype")
            DropDownList2.SelectedValue = reader("ca")
            DDL5.SelectedValue = reader("putype")
            DDL6.SelectedValue = reader("pu")
            RBL4.SelectedValue = reader("appprog")
            TextBox25.Text = reader("caname")
            TextBox1.Text = reader("employer")
            TextBox26.Text = reader("handname")
            TextBox2.Text = reader("noatsite")
            TextBox27.Text = reader("puname")
            TextBox3.Text = reader("interviewee")
            TextBox4.Text = reader("inttitle")
            TextBox5.Text = reader("coaddress")
            TextBox6.Text = reader("city")
            TextBox7.Text = reader("state")
            TextBox8.Text = reader("zip")
            TextBox10.Text = reader("phone")
            RBL5.SelectedValue = reader("equipinfo")
            RBL6.SelectedValue = reader("equiptype")
            equiptag.Text = reader("equiptag")
            TextBox11.Text = reader("windspeed")
            direction.SelectedValue = reader("winddir")
            TextBox24.Text = reader("pgpname")
            DropDownList4.SelectedValue = reader("commodity")
            DropDownList5.SelectedValue = reader("pest")
            TextBox15.Text = reader("acres")
            TextBox9.Text = reader("volume")
            TextBox16.Text = reader("applocation")
            TextBox17.Text = reader("appcity")
            TextBox18.Text = reader("appzip")
            county.SelectedValue = reader("county")
            TextBox20.Text = reader("adjacentn")
            TextBox21.Text = reader("adjacents")
            TextBox22.Text = reader("adjacente")
            TextBox23.Text = reader("adjacentw")
            product.SelectedValue = reader("chemapplied")
            tlchem.Text = reader("tlchem")
            measure.SelectedValue = reader("measure")
            RUP.Text = reader("rup")
            AI.Text = reader("ai")
            REI.Text = reader("rei")
            EPA.Text = reader("epa")
            product1.SelectedValue = reader("chemapplied1")
            tlchem1.Text = reader("tlchem1")
            measure1.SelectedValue = reader("measure1")
            rup1.Text = reader("rup1")
            ai1.Text = reader("ai1")
            rei1.Text = reader("rei1")
            epa1.Text = reader("epa1")
            product2.SelectedValue = reader("chemapplied2")
            tlchem2.Text = reader("tlchem2")
            measure2.SelectedValue = reader("measure2")
            rup2.Text = reader("rup2")
            ai2.Text = reader("ai2")
            rei2.Text = reader("rei2")
            epa2.Text = reader("epa2")
            product3.SelectedValue = reader("chemapplied3")
            tlchem3.Text = reader("tlchem3")
            measure3.SelectedValue = reader("measure3")
            rup3.Text = reader("rup3")
            ai3.Text = reader("ai3")
            rei3.Text = reader("rei3")
            epa3.Text = reader("epa3")
            RadioButtonList1.SelectedValue = reader("pestreg")
            YN1.SelectedValue = reader("handlersppe")
            YN2.SelectedValue = reader("aguse")
            YN3.SelectedValue = reader("handlercompetant")
            YN4.SelectedValue = reader("peststored")
            YN5.SelectedValue = reader("decontaminationsite")
            YN6.SelectedValue = reader("commtreated")
            YN7.SelectedValue = reader("illness")
            YN8.SelectedValue = reader("appmethod")
            YN9.SelectedValue = reader("changearea")
            YN10.SelectedValue = reader("chemdilut")
            YN11.SelectedValue = reader("waterreq")
            YN12.SelectedValue = reader("apprate")
            YN13.SelectedValue = reader("eyewash")
            YN14.SelectedValue = reader("cautionlabel")
            YN15.SelectedValue = reader("handwash")
            YN16.SelectedValue = reader("warnsign")
            YN17.SelectedValue = reader("towels")
            YN18.SelectedValue = reader("labelsreadable")
            YN19.SelectedValue = reader("equipavail")
            YN20.SelectedValue = reader("appexclusion")
            YN21.SelectedValue = reader("equipused")
            YN22.SelectedValue = reader("clothing")
            YN23.SelectedValue = reader("avoiddrift")
            YN24.SelectedValue = reader("respirator")
            YN25.SelectedValue = reader("appcertif")
            YN26.SelectedValue = reader("piloteyes")
            YN27.SelectedValue = reader("ruphandler")
            YN28.SelectedValue = reader("resptraining")
            YN29.SelectedValue = reader("clearapparea")
            YN30.SelectedValue = reader("resprecords")
            YN31.SelectedValue = reader("appsuspend")
            YN32.SelectedValue = reader("transportation")
            YN33.SelectedValue = reader("hazardcomm")
            YN34.SelectedValue = reader("contamination")
            YN35.SelectedValue = reader("hazardposted")
            YN36.SelectedValue = reader("rinsed")
            YN37.SelectedValue = reader("notificationareas")
            YN38.SelectedValue = reader("disposed")
            YN39.SelectedValue = reader("sds")
            YN40.SelectedValue = reader("decontamination")
            YN41.SelectedValue = reader("warnprov")
            YN42.SelectedValue = reader("warnfollow")
            YN43.SelectedValue = reader("handlertrained")
            YN44.SelectedValue = reader("pestsafety")
            YN45.SelectedValue = reader("trainrecords")
            YN46.SelectedValue = reader("hotline")
            YN47.SelectedValue = reader("handlerage")
            YN48.SelectedValue = reader("medno")
            Comments.Text = reader("comments")
            TextBox13.Text = reader("correction")
            nextdt.Text = reader("reinsp")
            RadioButtonList50.SelectedValue = reader("agree")
            TextBox29.Text = reader("inspdate")
            TextBox30.Text = reader("signed")
            TextBox31.Text = reader("signdate")
        End While
    End Sub

So, I think this is a databind issue, but I am not sure how to get around it properly. Any help is appreciated


VB.NET Forum Moderator
Staff member
Aug 17, 2004
Sydney, Australia
Programming Experience
If you think it's a data-binding issue then surely it would make sense to explain how you've setup your data-binding? If setting the SelectedValue of a DropDownList causes the error you describe then that would suggest that it is NOT bound. Is it?

By the way, you should give some serious thought to how you name your controls. Reading your code gives very little insight into what it is actually for and that is always a bad thing. Names like "DDL6", for instance, are ALWAYS terrible. That goes doubly when you have another control named "DropDownList6" on the same form. It's like you're trying to make it confusing.
Top Bottom