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. Data Binding - DropDownList

Data Binding - DropDownList

Scheduled Pinned Locked Moved Web Development
csharpasp-netdatabasevisual-studiowpf
3 Posts 2 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 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.DataColumnM

    S 1 Reply Last reply
    0
    • B brian55

      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.DataColumnM

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

      Your code is all fine except that you can't set the SelectedIndex to 0. As far as I know that doesn't work. Comment out that line and I bet it will work. Your initial value then will be the first row in your table. Here is an example from some code that I had wrote a while back. Note this is NOT using Visual Studio to assist me: public void buildlocation() { string ConnectionString = "yourconnection"; //Command String string location_cmd = "yoursqlcommand"; SqlDataAdapter dataAdapter = new SqlDataAdapter(location_cmd, ConnectionString); DataSet ds = new DataSet(); //Fill the datatable object dataAdapter.Fill(ds,"yourtable"); location.DataSource = ds.Tables[0]; location.DataTextField = "location"; location.DataValueField = "location"; location.DataBind(); } If you want to initialize a value other than a row in your table do this: location.SelectedItem.Text = "Please Select a Location"; location.SelectedItem.Value = "select"; Hope that helps. Scott Stocker

      B 1 Reply Last reply
      0
      • S sstocker

        Your code is all fine except that you can't set the SelectedIndex to 0. As far as I know that doesn't work. Comment out that line and I bet it will work. Your initial value then will be the first row in your table. Here is an example from some code that I had wrote a while back. Note this is NOT using Visual Studio to assist me: public void buildlocation() { string ConnectionString = "yourconnection"; //Command String string location_cmd = "yoursqlcommand"; SqlDataAdapter dataAdapter = new SqlDataAdapter(location_cmd, ConnectionString); DataSet ds = new DataSet(); //Fill the datatable object dataAdapter.Fill(ds,"yourtable"); location.DataSource = ds.Tables[0]; location.DataTextField = "location"; location.DataValueField = "location"; location.DataBind(); } If you want to initialize a value other than a row in your table do this: location.SelectedItem.Text = "Please Select a Location"; location.SelectedItem.Value = "select"; Hope that helps. Scott Stocker

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

        Thanks for the response. I took out the line in question. Still no binding. I added code to make sure that the datareader was in fact reading data. It was. I still get the following error Index 0 is not non-negative and below total rows count. Any more suggestions would be helpful.

        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