Click here to Skip to main content
15,921,452 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am using a dropdown and selecting record by database.

I want other records to display in the text box and label whatever record belongs to dropdown list.
Posted
Updated 9-Sep-10 21:21pm
v2
Comments
Dalek Dave 10-Sep-10 3:21am    
Edited as best I can, Question remains ambiguous.

I assume you are talking about an Asp.net implementation.

If that is so, here I have done a sample implementation for you. This shows a DropDownList control populated with some dummy data. When you change the selection of the DropDownList, system shows the selected item's data in a Label. Setting the DropDownList's AutoPostback="true" and setting an Event handler for SelectedIndexChanged lets you get the selected data in the CodeBehind and thus display detail properties in other controls.

The Aspx:

XML
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:DropDownList AutoPostBack="true" ID="DropDownList1" runat="server"
            onselectedindexchanged="DropDownList1_SelectedIndexChanged">
        </asp:DropDownList>
        <br />
        You selected : <asp:Label ID="txtName" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>


And, the CodeBehind:

C#
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;
public partial class _Default : System.Web.UI.Page
{
    class Person
    {
        public int Id
        {
            get;
            set;
        }
        public string Name
        {
            get;
            set;
        }
        public Person(int Id, string Name)
        {
            this.Id = Id;
            this.Name = Name;
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateData();
        }
    }
    /// <summary>
    /// Binds the DropDown list
    /// </summary>
    private void PopulateData()
    {
        //Populate some dummy data. In reality, these data should be retrieved from database
        List<Person> persons = new List<Person> { new Person(1, "Shubho"), new Person(2, "John"), new Person(3, "Tommy") };
        DropDownList1.DataTextField = "Name";
        DropDownList1.DataValueField = "Id";
        DropDownList1.DataSource = persons;
        DropDownList1.SelectedIndex = 0;
        DropDownList1.DataBind();
    }
    protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        int Id = Convert.ToInt32(DropDownList1.SelectedValue);
        //Id is the Id of the selected Person in the drop-down list
        Person selectedPerson = GetPerson(Id);
        txtName.Text = selectedPerson.Name;
    }
    /// <summary>
    /// Returns a person by Id
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    private Person GetPerson(int Id)
    {
        List<Person> persons = new List<Person> { new Person(1, "Shubho"), new Person(2, "John"), new Person(3, "Tommy") };
        //Find the person with the ID and return. In reality, this should be retrieved from the databas
        foreach (Person person in persons)
        {
            if (person.Id == Id) return person;
        }
        return null;
    }
}
 
Share this answer
 
Comments
Dalek Dave 10-Sep-10 3:28am    
Good Answer
Please try this:
Step1
First we create a table in database in my case I create a table:- filldata

In this table(filldata) we add three columns,stuname,city,state. With the help of stuname we find the city and state

Step2
add DropDownList (to show the stuname),TextBox(State) and Label (City)

Step3
On the Page_Load Event we fill the data(stuname) in DropDownList(DropDownList1) Like this:-
C#
SqlConnection conn1 = new SqlConnection
(ConfigurationManager.ConnectionStrings["conn"].ToString());
 if (!IsPostBack)
        {
           
            conn1.Open();
            SqlCommand cmd1 = new SqlCommand("select * from filldata", conn1);
            SqlDataReader dr1;
            dr1 = cmd1.ExecuteReader();
            while (dr1.Read())
            {
                DropDownList1.Items.Add(dr1["stuname"].ToString());

            }
            conn1.Close();
        }

Step4
Now we show the data in TextBox and Label with the help of DropDownlist :-
C#
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
    {
        conn1.Open();
        SqlCommand cmd1 = new SqlCommand("select * from filldata where stuname='" + DropDownList1.SelectedItem.Text + "'", conn1);
        SqlDataReader dr1;
        dr1 = cmd1.ExecuteReader();
        if (dr1.Read())
        {
            Label1.Text = dr1["city"].ToString();
            TextBox1.Text = dr1["state"].ToString();
        }
        conn1.Close();
    }
 
Share this answer
 
v2
Comments
Sandeep Mewara 10-Sep-10 8:01am    
Please use PRE tags from next time to format code part. That makes the question/answer readable.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900