ok, thanks for getting back..
here's the answer..
first you create a table(here country is my table) in your DataBase (SQL or any other) with countryname, countryID as columns in the table..
you can add values for both the columns at the time of table creation..
then in your aspx page drag a dropdownlist (make it auto post back as true) & add two labels..
Initially, make the labels visible as false
then go to your code behind...
here's my c# code..
here I used MS Acess as my database, you can use any DB, just replace the keywords...
--------------------------------------------------------
using System;
using System.Collections;
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.Data.OleDb;
public partial class Default2 : System.Web.UI.Page
{
OleDbConnection con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\\sameer.mdb");
// here sameer is my database name..
// for ms acess, you should put your DB inside any drive as above..
// if it is on any other location, it might not properly.
OleDbCommand cmd = new OleDbCommand();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
databind();
}
}
public void databind()
{
con.Open();
cmd = new OleDbCommand("select * from country", con);
OleDbDataAdapter da = new OleDbDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
con.Close();
DropDownList1.DataSource = ds;
DropDownList1.DataTextField = "countryname";
DropDownList1.DataValueField = "countryID";
DropDownList1.DataBind();
// this dynamically(explicitly) adds items to dropdownlist.
// both the zero's indicate the index position in dropdownlist.
DropDownList1.Items.Insert(0, new ListItem("select", "0"));
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
Label1.Text = DropDownList1.SelectedItem.ToString();
Label2.Text = DropDownList1.SelectedValue;
Label1.Visible = true;
Label2.Visible = true;
}
}
--------------------------------------------------------------------------------
Keep Rocking...
shameer ali shaik
Ravinderk, if this helps please login to Mark As Answer. | Alert Moderator