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. General Programming
  3. C#
  4. How to Use DropDownList in ASP.NET

How to Use DropDownList in ASP.NET

Scheduled Pinned Locked Moved C#
csharpasp-netdatabasesql-serversysadmin
5 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.
  • D Offline
    D Offline
    dwark106
    wrote on last edited by
    #1

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

    S 1 Reply Last reply
    0
    • D dwark106

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

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

      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(); } /* ... */

      D 1 Reply Last reply
      0
      • S sgatto159

        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(); } /* ... */

        D Offline
        D Offline
        dwark106
        wrote on last edited by
        #3

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

        S 1 Reply Last reply
        0
        • D dwark106

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

          S Offline
          S Offline
          sgatto159
          wrote on last edited by
          #4

          hum... 1- if that snippet is in a codebehind, than your class inherits from System.Web.UI.Page which does not contains SelectedItemCombo). So the question is: what is SelectedItemCombo ? 2- doing sql like that in callbacks is a bad idea 3- doing sql without try{}catch{}finally{} is even worst 4- replace while(thisReader.Read()) with: if(thisReader.Read()): is less confusing. But: why not DataSet? 5- replace if(strID == this.DropDownList1.SelectedValue.ToString()) with if(strID.Equals(this.DropDownList1.SelectedValue.ToString())) hope this helps

          D 1 Reply Last reply
          0
          • S sgatto159

            hum... 1- if that snippet is in a codebehind, than your class inherits from System.Web.UI.Page which does not contains SelectedItemCombo). So the question is: what is SelectedItemCombo ? 2- doing sql like that in callbacks is a bad idea 3- doing sql without try{}catch{}finally{} is even worst 4- replace while(thisReader.Read()) with: if(thisReader.Read()): is less confusing. But: why not DataSet? 5- replace if(strID == this.DropDownList1.SelectedValue.ToString()) with if(strID.Equals(this.DropDownList1.SelectedValue.ToString())) hope this helps

            D Offline
            D Offline
            dwark106
            wrote on last edited by
            #5

            Thanks for your help. bool SelectedItemCombo. Actually I have ready made this program in C#(winform) So where I used it. Once I remove it from my program in ASP.NEt it is working fine. Once again Thanks.

            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