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. Database & SysAdmin
  3. Database
  4. Data Binding - DropDownList

Data Binding - DropDownList

Scheduled Pinned Locked Moved Database
helpcsharpasp-netdatabasevisual-studio
8 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.
  • B Offline
    B Offline
    brian55
    wrote on last edited by
    #1

    I have posted this problem a few times now in other areas. I have received some repsoinses but all suggestions fail. I have placed many statments in the code to verify that the data is being read. IT is. The only issue is the dataBind statement. I'm getting very frustrated! I have many examples of how to bind a dropdownlist but NONE match the code that is generated in VS .NET. Since I am new to .NET and web programming I am having difficulty following the gerneated code. I am convinced my problem is in the way I am binding the data. The eror I get is "Index 0 is not non-negative and below total rows count." and it ocurs in the following line DropDownList1.DataBind(); Here is some of the code. If you are intereseted in helping me I can post all of it. private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { BindListToDropDown(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.dbListOfAirports1 = new dbListOfAirports(); this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand(); ((System.ComponentModel.ISupportInitialize)(this.dbListOfAirports1)).BeginInit(); this.DropDownList1.DataBinding += new System.EventHandler(this.Page_Load); // // oleDbDataAdapter1 // this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1; this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "tblAirport", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("AirportCode", "AirportCode"), new System.Data.Common.DataColumnMapping("AirportID", "AirportID"), new System.Data.Common.DataColumnMapping("AirportName", "AirportName"), new System.Data.Common.DataColumnMapping("City", "City"), new System.Data.Common.DataColumnMapping("Latitude", "Latitude"), new System.Data.Common.DataColumnMapping("Longitude", "Longitude"), new System.Data.Common.DataColumnMapping("State", "State")})}); // this.oleDbDataAdapter1.RowUpdated += new System.Data.OleDb.OleDbRowUpdatedEventHandle

    J I 2 Replies Last reply
    0
    • B brian55

      I have posted this problem a few times now in other areas. I have received some repsoinses but all suggestions fail. I have placed many statments in the code to verify that the data is being read. IT is. The only issue is the dataBind statement. I'm getting very frustrated! I have many examples of how to bind a dropdownlist but NONE match the code that is generated in VS .NET. Since I am new to .NET and web programming I am having difficulty following the gerneated code. I am convinced my problem is in the way I am binding the data. The eror I get is "Index 0 is not non-negative and below total rows count." and it ocurs in the following line DropDownList1.DataBind(); Here is some of the code. If you are intereseted in helping me I can post all of it. private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { BindListToDropDown(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.dbListOfAirports1 = new dbListOfAirports(); this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand(); ((System.ComponentModel.ISupportInitialize)(this.dbListOfAirports1)).BeginInit(); this.DropDownList1.DataBinding += new System.EventHandler(this.Page_Load); // // oleDbDataAdapter1 // this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1; this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "tblAirport", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("AirportCode", "AirportCode"), new System.Data.Common.DataColumnMapping("AirportID", "AirportID"), new System.Data.Common.DataColumnMapping("AirportName", "AirportName"), new System.Data.Common.DataColumnMapping("City", "City"), new System.Data.Common.DataColumnMapping("Latitude", "Latitude"), new System.Data.Common.DataColumnMapping("Longitude", "Longitude"), new System.Data.Common.DataColumnMapping("State", "State")})}); // this.oleDbDataAdapter1.RowUpdated += new System.Data.OleDb.OleDbRowUpdatedEventHandle

      J Offline
      J Offline
      Jerry Hammond
      wrote on last edited by
      #2

      Just a shot in the dark as I'm a sleft-taught hobbiest, but I would write your try catch like this:

      private void BindListToDropDown() { try { oleDbConnection1.Open(); OleDbDataReader OleDbReader = this.oleDbSelectCommand1.ExecuteReader(); DropDownList1.DataSource=OleDbReader; DropDownList1.DataTextField = "AirportName"; DropDownList1.DataValueField="AirportID"; DropDownList1.DataBind(); oleDbConnection1.Close(); } catch(OleDbException ex) {

      Notice I don't close the connection until after the DataBind. Let me know if that works. Jerry Most people are willing to pay more to be amused than to be educated--Robert C. Savage, Life Lessons Toasty0.com Ladder League (beta)

      B 1 Reply Last reply
      0
      • B brian55

        I have posted this problem a few times now in other areas. I have received some repsoinses but all suggestions fail. I have placed many statments in the code to verify that the data is being read. IT is. The only issue is the dataBind statement. I'm getting very frustrated! I have many examples of how to bind a dropdownlist but NONE match the code that is generated in VS .NET. Since I am new to .NET and web programming I am having difficulty following the gerneated code. I am convinced my problem is in the way I am binding the data. The eror I get is "Index 0 is not non-negative and below total rows count." and it ocurs in the following line DropDownList1.DataBind(); Here is some of the code. If you are intereseted in helping me I can post all of it. private void Page_Load(object sender, System.EventArgs e) { if (!Page.IsPostBack) { BindListToDropDown(); } } #region Web Form Designer generated code override protected void OnInit(EventArgs e) { // // CODEGEN: This call is required by the ASP.NET Web Form Designer. // InitializeComponent(); base.OnInit(e); } /// /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// private void InitializeComponent() { this.oleDbDataAdapter1 = new System.Data.OleDb.OleDbDataAdapter(); this.oleDbConnection1 = new System.Data.OleDb.OleDbConnection(); this.dbListOfAirports1 = new dbListOfAirports(); this.oleDbSelectCommand1 = new System.Data.OleDb.OleDbCommand(); ((System.ComponentModel.ISupportInitialize)(this.dbListOfAirports1)).BeginInit(); this.DropDownList1.DataBinding += new System.EventHandler(this.Page_Load); // // oleDbDataAdapter1 // this.oleDbDataAdapter1.SelectCommand = this.oleDbSelectCommand1; this.oleDbDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] { new System.Data.Common.DataTableMapping("Table", "tblAirport", new System.Data.Common.DataColumnMapping[] { new System.Data.Common.DataColumnMapping("AirportCode", "AirportCode"), new System.Data.Common.DataColumnMapping("AirportID", "AirportID"), new System.Data.Common.DataColumnMapping("AirportName", "AirportName"), new System.Data.Common.DataColumnMapping("City", "City"), new System.Data.Common.DataColumnMapping("Latitude", "Latitude"), new System.Data.Common.DataColumnMapping("Longitude", "Longitude"), new System.Data.Common.DataColumnMapping("State", "State")})}); // this.oleDbDataAdapter1.RowUpdated += new System.Data.OleDb.OleDbRowUpdatedEventHandle

        I Offline
        I Offline
        Illegal Operation
        wrote on last edited by
        #3

        If you want to bind the data to the dropdownlist you will need to use the dataset fill function. Page_Load sqlDataAdapter1.Fill(dataSet11); DropDownList1.DataBind(); that's it! Hope it helps... Illegal Operation WannaBe and GonnaBe Systems Developer

        B 1 Reply Last reply
        0
        • J Jerry Hammond

          Just a shot in the dark as I'm a sleft-taught hobbiest, but I would write your try catch like this:

          private void BindListToDropDown() { try { oleDbConnection1.Open(); OleDbDataReader OleDbReader = this.oleDbSelectCommand1.ExecuteReader(); DropDownList1.DataSource=OleDbReader; DropDownList1.DataTextField = "AirportName"; DropDownList1.DataValueField="AirportID"; DropDownList1.DataBind(); oleDbConnection1.Close(); } catch(OleDbException ex) {

          Notice I don't close the connection until after the DataBind. Let me know if that works. Jerry Most people are willing to pay more to be amused than to be educated--Robert C. Savage, Life Lessons Toasty0.com Ladder League (beta)

          B Offline
          B Offline
          brian55
          wrote on last edited by
          #4

          Didn't work. Thanks for the effort! i think it has something to do with the reader. Most examples I find do not use the reader. Brian

          J R 3 Replies Last reply
          0
          • I Illegal Operation

            If you want to bind the data to the dropdownlist you will need to use the dataset fill function. Page_Load sqlDataAdapter1.Fill(dataSet11); DropDownList1.DataBind(); that's it! Hope it helps... Illegal Operation WannaBe and GonnaBe Systems Developer

            B Offline
            B Offline
            brian55
            wrote on last edited by
            #5

            Tried this and got the message An invalid data source is being used for DropDownList1. A valid data source must implement either IListSource or IEnumerable. Thanks for the effort! Brian

            1 Reply Last reply
            0
            • B brian55

              Didn't work. Thanks for the effort! i think it has something to do with the reader. Most examples I find do not use the reader. Brian

              J Offline
              J Offline
              Jerry Hammond
              wrote on last edited by
              #6

              Ok, it ws a shot in the dar. Does the error point to either one of these lines of code? DropDownList1.DataTextField = "AirportName"; DropDownList1.DataValueField="AirportID"; Jerry Most people are willing to pay more to be amused than to be educated--Robert C. Savage, Life Lessons Toasty0.com Ladder League (beta)

              1 Reply Last reply
              0
              • B brian55

                Didn't work. Thanks for the effort! i think it has something to do with the reader. Most examples I find do not use the reader. Brian

                J Offline
                J Offline
                Jerry Hammond
                wrote on last edited by
                #7

                After my last post I though about some code I just wrote that might give you some ideas. if(IsPostBack) { string SQL=""; switch(ddlLibrarySelection.SelectedItem.Text) { case"The Whole Library": SQL=ddlLibrarySelection.SelectedValue.ToString(); break; case"ASP.NET": SQL=ddlLibrarySelection.SelectedValue.ToString(); break; case"C#": SQL=ddlLibrarySelection.SelectedValue.ToString(); break; case"C++": SQL=ddlLibrarySelection.SelectedValue.ToString(); break; case"DirectX": SQL=ddlLibrarySelection.SelectedValue.ToString(); break; case"Game Programming": SQL=ddlLibrarySelection.SelectedValue.ToString(); break; case"SQL": SQL=ddlLibrarySelection.SelectedValue.ToString(); break; case"Visual Basic": SQL=ddlLibrarySelection.SelectedValue.ToString(); break; } string DbPath = Server.MapPath("bin/LibBooksProg.mdb"); string ConnStr = "Provider=Microsoft.Jet.OLEDB.4.0;"+ @"Data Source="+DbPath+";"; OleDbConnection MyOleDbConn = new OleDbConnection(ConnStr); OleDbCommand Commandobj = new OleDbCommand( SQL,MyOleDbConn); MyOleDbConn.Open(); dgTestIt.DataSource= Commandobj.ExecuteReader(); dgTestIt.DataBind(); MyOleDbConn.Close(); } } Jerry Most people are willing to pay more to be amused than to be educated--Robert C. Savage, Life Lessons Toasty0.com Ladder League (beta)

                1 Reply Last reply
                0
                • B brian55

                  Didn't work. Thanks for the effort! i think it has something to do with the reader. Most examples I find do not use the reader. Brian

                  R Offline
                  R Offline
                  rudy net
                  wrote on last edited by
                  #8

                  I am surprised that the previous code didn't work. I tried it using the pubs database and it worked fine. Make sure you close the database after calling DataBind. I tried closing before and got an error. Following is my code: private void BindListToDropDown() { string query = "select * from Authors"; SqlCommand cmd = new SqlCommand(query, conn); //where conn is a SqlConnection to pubs database. conn.Open(); SqlDataReader dr = cmd.ExecuteReader(); this.DropDownList1.DataSource = dr; this.DropDownList1.DataTextField = "au_lname"; this.DropDownList1.DataValueField = "au_id"; this.DropDownList1.DataBind(); conn.Close(); }

                  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