create table CountryMaster
(CountryID int,CountryName varchar(100))
go
insert into CountryMaster values(1,'India')
insert into CountryMaster values(2,'Germany')
insert into CountryMaster values(3,'Russia')
go
alter procedure [dbo].[CountryComboData]
as
select CountryID ,CountryName from CountryMaster
union
select CountryID=0,CountryName='-- Select --'
-------------
ASPX
---------------
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="dropDownTest.aspx.cs" Inherits="dropDownTest" %>
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<br />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
</form>
</body>
</html>
----------------------------
web.config
--------------------
<?xml version="1.0"?>
<!--
Note: As an alternative to hand editing this file you can use the
web admin tool to configure settings for your application. Use
the Website->Asp.Net Configuration option in Visual Studio.
A full list of settings and comments can be found in
machine.config.comments usually located in
\Windows\Microsoft.Net\Framework\v2.x\Config
-->
<configuration>
<appSettings>
<add key="RMSConnectionString" value="Data Source=localhost;Database=Forum;uid=sa;pwd=xxxxxx; pooling=false;Connect timeout=60000" />
</appSettings>
<connectionStrings/>
<system.web>
<!--
Set compilation debug="true" to insert debugging
symbols into the compiled page. Because this
affects performance, set this value to true only
during development.
-->
<compilation debug="true">
</compilation>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows"/>
<!--
The <customErrors> section enables configuration
of what to do if/when an unhandled error occurs
during the execution of a request. Specifically,
it enables developers to configure html error pages
to be displayed in place of a error stack trace.
<customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.htm" />
</customErrors>
-->
</system.web>
</configuration>
------------
ASPX.CS
---------------
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
public partial class dropDownTest : System.Web.UI.Page
{
public string thisConnectionString = ConfigurationManager.AppSettings["RMSConnectionString"];
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
this.DropDownList1.Focus();
getCountries();
}
}
protected void getCountries()
{
DataSet ds = new DataSet();
ds = spDataset("[CountryComboData]");
this.DropDownList1.DataSource = ds.Tables[0];
DropDownList1.DataTextField = "CountryName";
DropDownList1.DataValueField = "CountryID";
DropDownList1.DataBind();
}
public DataSet spDataset(string sp_name)
{
SqlDataAdapter dAd = new SqlDataAdapter(sp_name, thisConnectionString);
dAd.SelectCommand.CommandType = CommandType.StoredProcedure;
DataSet dSet = new DataSet();
try
{
dAd.Fill(dSet);
return dSet;
}
catch
{
throw;
}
finally
{
dSet.Dispose();
dAd.Dispose();
//thisConnectionString.Close();
//thisConnectionString.Dispose();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
this.Label1.Text = DropDownList1.SelectedItem.Text;
}
}
------------------------------------------------------------------
Try This.....
Thanks,
Sanjay
Ogipansrk, if this helps please login to Mark As Answer. | Alert Moderator