Hi tanweeruddinb...,
You need to call the DropDownList DataBind method before accessing the selected item.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:DropDownList ID="ddlEmployees" runat="server" DataSourceID="SqlDataSourceEmployees"
DataTextField="FirstName" DataValueField="EmployeeID"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceEmployees" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
SelectCommand="SELECT [EmployeeID], [FirstName] FROM [Employees]"></asp:SqlDataSource>
<asp:DropDownList ID="ddlCountries" runat="server" DataSourceID="SqlDataSourceCountries"
DataTextField="Country" DataValueField="EmployeeID"></asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSourceCountries" runat="server" ConnectionString="<%$ ConnectionStrings:conString %>"
SelectCommand="SELECT [EmployeeID], [Country] FROM [Employees]"></asp:SqlDataSource>
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
this.ddlEmployees.DataBind();
string empId = ddlEmployees.SelectedValue;
this.ddlCountries.DataBind();
string country = ddlCountries.SelectedItem.Text;
ClientScript.RegisterStartupScript(this.GetType(), "alert", "alert('Employee Id : " + empId + "\\nCountry: " + country + "');", true);
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
Me.ddlEmployees.DataBind()
Dim empId As String = ddlEmployees.SelectedValue
Me.ddlCountries.DataBind()
Dim country As String = ddlCountries.SelectedItem.Text
ClientScript.RegisterStartupScript(Me.GetType(), "alert", "alert('Employee Id : " & empId & "\nCountry: " & country & "');", True)
End Sub
Screenshot