Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Databinding to a Dropdownlist Problem

Databinding to a Dropdownlist Problem

Scheduled Pinned Locked Moved ASP.NET
help
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    AndyCapp
    wrote on last edited by
    #1

    I have a dropdown list control (lstState) that is contained within a user control (ucAddressInfo) which itself is contained on another user control (ucUserInfo) which finally is contained on an aspx page. So the heirachy is such Parent page is ASPX ------> User control ucUserInfo ------> contains ucAddressInfo --------> contains dropdownlist (lstState) as well as a datagrid (dglist) I then have the following code in the page_load event of the ucAddressInfo control (this is to create som test data. //Create a Datatable with some data DataTable oData = new DataTable(); oData.Columns.Add ("StateAbbrev", typeof(String)); oData.Columns.Add ("StateName", typeof(String)); DataRow oRow = oData.NewRow (); oRow["StateAbbrev"] = "AZ"; oRow["StateName"] = "Arizona"; oData.Rows.Add(oRow); lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; //Set the DataSource of the DropDownList lstState.DataSource = oData; //Clear the datagrid control on the ucAddressInfo //User Control dgStates.Columns.Clear(); dgStates.AutoGenerateColumns = false; BoundColumn bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateAbbrev"; dgStates.Columns.Add( bCol ); bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateName"; dgStates.Columns.Add( bCol ); dgStates.DataSource= oData; // Do the databind of the Datagrid //This works fine dgStates.DataBind(); //Now do the databind of the DropDown list box lstState.DataBind(); //This fails with the following error "Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value" Has Anyone else had this issue and how have you solved it. I have tried moving the lstState.Databind(); to the aspx page load, the ucAddressInfo OnInit event and numerous other places with no luck!!!! Thanks in Advance Carpe Diem

    S M 2 Replies Last reply
    0
    • A AndyCapp

      I have a dropdown list control (lstState) that is contained within a user control (ucAddressInfo) which itself is contained on another user control (ucUserInfo) which finally is contained on an aspx page. So the heirachy is such Parent page is ASPX ------> User control ucUserInfo ------> contains ucAddressInfo --------> contains dropdownlist (lstState) as well as a datagrid (dglist) I then have the following code in the page_load event of the ucAddressInfo control (this is to create som test data. //Create a Datatable with some data DataTable oData = new DataTable(); oData.Columns.Add ("StateAbbrev", typeof(String)); oData.Columns.Add ("StateName", typeof(String)); DataRow oRow = oData.NewRow (); oRow["StateAbbrev"] = "AZ"; oRow["StateName"] = "Arizona"; oData.Rows.Add(oRow); lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; //Set the DataSource of the DropDownList lstState.DataSource = oData; //Clear the datagrid control on the ucAddressInfo //User Control dgStates.Columns.Clear(); dgStates.AutoGenerateColumns = false; BoundColumn bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateAbbrev"; dgStates.Columns.Add( bCol ); bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateName"; dgStates.Columns.Add( bCol ); dgStates.DataSource= oData; // Do the databind of the Datagrid //This works fine dgStates.DataBind(); //Now do the databind of the DropDown list box lstState.DataBind(); //This fails with the following error "Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value" Has Anyone else had this issue and how have you solved it. I have tried moving the lstState.Databind(); to the aspx page load, the ucAddressInfo OnInit event and numerous other places with no luck!!!! Thanks in Advance Carpe Diem

      S Offline
      S Offline
      ScottSingleton
      wrote on last edited by
      #2

      Have you tried naming your data table when you create it and setting that name to the DataMember Property? Try this and see if it works: for your declaration: DataTable oData = new DataTable("TblStates"); during your binding setup, in this order: lstState.DataSource = oData; lstState.DataMember = "TblStates"; lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; lstState.DataBind();


      'Cause I'm living on things that excite me, be it pastries or lobsters or love...
      I'm just trying to get by, being quiet and shy, in a world full of push and shove...
      Jimmy Buffett - The Wino and I know

      A 1 Reply Last reply
      0
      • S ScottSingleton

        Have you tried naming your data table when you create it and setting that name to the DataMember Property? Try this and see if it works: for your declaration: DataTable oData = new DataTable("TblStates"); during your binding setup, in this order: lstState.DataSource = oData; lstState.DataMember = "TblStates"; lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; lstState.DataBind();


        'Cause I'm living on things that excite me, be it pastries or lobsters or love...
        I'm just trying to get by, being quiet and shy, in a world full of push and shove...
        Jimmy Buffett - The Wino and I know

        A Offline
        A Offline
        AndyCapp
        wrote on last edited by
        #3

        I have tried that and it is not the data source that is causing the problem as I have tried it with an item list, a collection base, a hashtable and and array, all with the same result. Carpe Diem

        J 1 Reply Last reply
        0
        • A AndyCapp

          I have tried that and it is not the data source that is causing the problem as I have tried it with an item list, a collection base, a hashtable and and array, all with the same result. Carpe Diem

          J Offline
          J Offline
          Jesse Squire
          wrote on last edited by
          #4

          Hrmm... this kind of leads me to think that the problem is actually elsewhere. Could you post the entire stack trace? It may help to shed some light. Also, just for simplification, have you tried commenting out the datagrid lines before testing? --Jesse

          1 Reply Last reply
          0
          • A AndyCapp

            I have a dropdown list control (lstState) that is contained within a user control (ucAddressInfo) which itself is contained on another user control (ucUserInfo) which finally is contained on an aspx page. So the heirachy is such Parent page is ASPX ------> User control ucUserInfo ------> contains ucAddressInfo --------> contains dropdownlist (lstState) as well as a datagrid (dglist) I then have the following code in the page_load event of the ucAddressInfo control (this is to create som test data. //Create a Datatable with some data DataTable oData = new DataTable(); oData.Columns.Add ("StateAbbrev", typeof(String)); oData.Columns.Add ("StateName", typeof(String)); DataRow oRow = oData.NewRow (); oRow["StateAbbrev"] = "AZ"; oRow["StateName"] = "Arizona"; oData.Rows.Add(oRow); lstState.DataValueField = "StateAbbrev"; lstState.DataTextField = "StateName"; //Set the DataSource of the DropDownList lstState.DataSource = oData; //Clear the datagrid control on the ucAddressInfo //User Control dgStates.Columns.Clear(); dgStates.AutoGenerateColumns = false; BoundColumn bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateAbbrev"; dgStates.Columns.Add( bCol ); bCol = new BoundColumn(); bCol.HeaderText = "StateProvinceName"; bCol.DataField = "StateName"; dgStates.Columns.Add( bCol ); dgStates.DataSource= oData; // Do the databind of the Datagrid //This works fine dgStates.DataBind(); //Now do the databind of the DropDown list box lstState.DataBind(); //This fails with the following error "Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values. Parameter name: value" Has Anyone else had this issue and how have you solved it. I have tried moving the lstState.Databind(); to the aspx page load, the ucAddressInfo OnInit event and numerous other places with no luck!!!! Thanks in Advance Carpe Diem

            M Offline
            M Offline
            minhpc_bk
            wrote on last edited by
            #5

            Hi there The exception might have been thrown from the OnDataBinding method, if the SelectedValue property is set before the DataBind method is called, the error as you said will be raised.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups