How to Use DropDownList in ASP.NET
-
Hi Everyone, I am facing a problem while using DropDownList. Actually I am retrieveing company name from Sql server and displaying on DropDownList (value + id). If I select DropDownList(just like combo box), it displays me 1st records corresponding to that record(ID). But if I click 2nd,3rd and fourth and so on from DropDownList company name , every time it displays me only 1st record on textbox. //////////////This is server side program(Update.aspx)////////////// <%@ Page language="c#" Codebehind="Update.aspx.cs" AutoEventWireup="false" Inherits="Myprogram.Update" %>
Select Company Name:
asp:DropDownList id="DropDownList1" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" runat="server" Width="160px" Height="16px" DataTextField="Display" DataValueField="Value"> /asp:DropDownList>
Customer Name:
/////////Update.aspx.cs//////////// protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { if(this.SelectedItemCombo) { DetailsOfRecords(); } } protected void DetailsOfRecords() { thisConnection.Open(); thisCommand = thisConnection.CreateCommand(); thisCommand.CommandText = " SELECT * from Customers where CustomerID=" + this.DropDownList1.SelectedValue.ToString(); thisReader = thisCommand.ExecuteReader(); while(thisReader.Read()) { string strID = thisReader["CustomerID"].ToString(); if(strID == this.DropDownList1.SelectedValue.ToString()) { this.txtCustomerName.Text = thisReader["CustomerName"].ToString(); this.txtCompany.Text = thisReader["Company"].ToString(); } } thisReader.Close(); thisConnection.Close(); }
-
Hi Everyone, I am facing a problem while using DropDownList. Actually I am retrieveing company name from Sql server and displaying on DropDownList (value + id). If I select DropDownList(just like combo box), it displays me 1st records corresponding to that record(ID). But if I click 2nd,3rd and fourth and so on from DropDownList company name , every time it displays me only 1st record on textbox. //////////////This is server side program(Update.aspx)////////////// <%@ Page language="c#" Codebehind="Update.aspx.cs" AutoEventWireup="false" Inherits="Myprogram.Update" %>
Select Company Name:
asp:DropDownList id="DropDownList1" AutoPostBack="True" OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged" runat="server" Width="160px" Height="16px" DataTextField="Display" DataValueField="Value"> /asp:DropDownList>
Customer Name:
/////////Update.aspx.cs//////////// protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { if(this.SelectedItemCombo) { DetailsOfRecords(); } } protected void DetailsOfRecords() { thisConnection.Open(); thisCommand = thisConnection.CreateCommand(); thisCommand.CommandText = " SELECT * from Customers where CustomerID=" + this.DropDownList1.SelectedValue.ToString(); thisReader = thisCommand.ExecuteReader(); while(thisReader.Read()) { string strID = thisReader["CustomerID"].ToString(); if(strID == this.DropDownList1.SelectedValue.ToString()) { this.txtCustomerName.Text = thisReader["CustomerName"].ToString(); this.txtCompany.Text = thisReader["Company"].ToString(); } } thisReader.Close(); thisConnection.Close(); }
-
where is your databind() ? in page_load ? if I understand your problem, I'm quite sure you've not checked if ispostback before databind(). try (in Page_Load):
/* ... */ if(IsPostBack == false) { /* ... */ DropDownList1.DataBind(); } /* ... */
Thanks for solving my one problem. But I am still facing problem. If I select company name from Dropdownlist it does not showing me corresponding record on the textbox(like customer name,address,phone number and so on. protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { if(this.SelectedItemCombo) { DetailsOfRecords(); } } protected void DetailsOfRecords() { thisConnection.Open(); thisCommand = thisConnection.CreateCommand(); thisCommand.CommandText = " SELECT * from Customers where CustomerID=" + this.DropDownList1.SelectedValue; thisReader = thisCommand.ExecuteReader(); while(thisReader.Read()) { string strID = thisReader["CustomerID"].ToString(); if(strID == this.DropDownList1.SelectedValue.ToString()) { this.txtCustomerName.Text = thisReader["CustomerName"].ToString(); this.txtCompany.Text = thisReader["Company"].ToString(); this.txtEmailAddress.Text = thisReader["Email"].ToString(); } } }
-
Thanks for solving my one problem. But I am still facing problem. If I select company name from Dropdownlist it does not showing me corresponding record on the textbox(like customer name,address,phone number and so on. protected void DropDownList1_SelectedIndexChanged(object sender, System.EventArgs e) { if(this.SelectedItemCombo) { DetailsOfRecords(); } } protected void DetailsOfRecords() { thisConnection.Open(); thisCommand = thisConnection.CreateCommand(); thisCommand.CommandText = " SELECT * from Customers where CustomerID=" + this.DropDownList1.SelectedValue; thisReader = thisCommand.ExecuteReader(); while(thisReader.Read()) { string strID = thisReader["CustomerID"].ToString(); if(strID == this.DropDownList1.SelectedValue.ToString()) { this.txtCustomerName.Text = thisReader["CustomerName"].ToString(); this.txtCompany.Text = thisReader["Company"].ToString(); this.txtEmailAddress.Text = thisReader["Email"].ToString(); } } }
hum... 1- if that snippet is in a codebehind, than your class inherits from
System.Web.UI.Page
which does not containsSelectedItemCombo
). So the question is: what isSelectedItemCombo
? 2- doing sql like that in callbacks is a bad idea 3- doing sql withouttry{}catch{}finally{}
is even worst 4- replace while(thisReader.Read()) with: if(thisReader.Read()): is less confusing. But: why notDataSet
? 5- replaceif(strID == this.DropDownList1.SelectedValue.ToString())
withif(strID.Equals(this.DropDownList1.SelectedValue.ToString()))
hope this helps -
hum... 1- if that snippet is in a codebehind, than your class inherits from
System.Web.UI.Page
which does not containsSelectedItemCombo
). So the question is: what isSelectedItemCombo
? 2- doing sql like that in callbacks is a bad idea 3- doing sql withouttry{}catch{}finally{}
is even worst 4- replace while(thisReader.Read()) with: if(thisReader.Read()): is less confusing. But: why notDataSet
? 5- replaceif(strID == this.DropDownList1.SelectedValue.ToString())
withif(strID.Equals(this.DropDownList1.SelectedValue.ToString()))
hope this helps