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