Data Binding - DropDownList
-
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 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
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)
-
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
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
-
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)
-
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
-
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
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)
-
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
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) -
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
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(); }