Hi vyas,
Please take reference the below code and change your code.
Database
I have made use of the table Customers. You can download the database table SQL by clicking the download link below.
Download SQL file
HTML
Customer:<asp:DropDownList ID="ddlCustomers" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlCustomers_SelectedIndexChanged">
</asp:DropDownList>
<br />
Name:
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
Country:
<asp:RadioButtonList ID="rblCountries" runat="server">
</asp:RadioButtonList>
Namespaces
C#
using System.Data.SqlClient;
using System.Configuration;
VB.Net
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.PopulateCustomersDropDownList();
}
}
protected void ddlCustomers_SelectedIndexChanged(object sender, EventArgs e)
{
string customerID = ddlCustomers.SelectedItem.Value;
this.PopulateCountriesRadioButtonList();
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("SELECT Name,Country FROM Customers WHERE CustomerId = @CustomerId", con))
{
con.Open();
cmd.Parameters.AddWithValue("@CustomerId", customerID);
SqlDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
txtName.Text = dr["Name"].ToString();
}
if (rblCountries.Items.FindByText(dr["Country"].ToString()) != null)
{
rblCountries.Items.FindByText(dr["Country"].ToString()).Selected = true;
}
con.Close();
}
}
}
private void PopulateCustomersDropDownList()
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand("select CustomerID, Name from Customers", con))
{
con.Open();
ddlCustomers.DataSource = cmd.ExecuteReader();
ddlCustomers.DataTextField = "Name";
ddlCustomers.DataValueField = "CustomerID";
ddlCustomers.DataBind();
con.Close();
}
}
}
private void PopulateCountriesRadioButtonList()
{
string constr = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection con = new SqlConnection(constr);
SqlCommand cmd = new SqlCommand("SELECT DISTINCT Country FROM Customers", con);
con.Open();
rblCountries.DataSource = cmd.ExecuteReader();
rblCountries.DataTextField = "Country";
rblCountries.DataValueField = "Country";
rblCountries.DataBind();
con.Close();
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Me.PopulateCustomersDropDownList()
End If
End Sub
Protected Sub ddlCustomers_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim customerID As String = ddlCustomers.SelectedItem.Value
Me.PopulateCountriesRadioButtonList()
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("SELECT Name,Country FROM Customers WHERE CustomerId = @CustomerId", con)
con.Open()
cmd.Parameters.AddWithValue("@CustomerId", customerID)
Dim dr As SqlDataReader = cmd.ExecuteReader()
If dr.Read() Then
txtName.Text = dr("Name").ToString()
End If
If rblCountries.Items.FindByText(dr("Country").ToString()) IsNot Nothing Then
rblCountries.Items.FindByText(dr("Country").ToString()).Selected = True
End If
con.Close()
End Using
End Using
End Sub
Private Sub PopulateCustomersDropDownList()
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand("select CustomerID, Name from Customers", con)
con.Open()
ddlCustomers.DataSource = cmd.ExecuteReader()
ddlCustomers.DataTextField = "Name"
ddlCustomers.DataValueField = "CustomerID"
ddlCustomers.DataBind()
con.Close()
End Using
End Using
End Sub
Private Sub PopulateCountriesRadioButtonList()
Dim constr As String = ConfigurationManager.ConnectionStrings("conString").ConnectionString
Dim con As SqlConnection = New SqlConnection(constr)
Dim cmd As SqlCommand = New SqlCommand("SELECT DISTINCT Country FROM Customers", con)
con.Open()
rblCountries.DataSource = cmd.ExecuteReader()
rblCountries.DataTextField = "Country"
rblCountries.DataValueField = "Country"
rblCountries.DataBind()
con.Close()
End Sub
Screenshot
