Hi Corobori,
Check this example. Now please take its reference and correct your code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
UserControl
Country:
<asp:DropDownList runat="server" ID="ddlCountries" AutoPostBack="true" OnSelectedIndexChanged="OnCountryChanged">
<asp:ListItem Value="0" Text="Select" />
<asp:ListItem Value="Germany" Text="Germany" />
<asp:ListItem Value="Mexico" Text="Mexico" />
<asp:ListItem Value="UK" Text="UK" />
<asp:ListItem Value="USA" Text="USA" />
<asp:ListItem Value="Canada" Text="Canada" />
</asp:DropDownList>
Default
<%@ Register Src="~/UC_Country.ascx" TagName="Country" TagPrefix="uc" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<uc:Country ID="ucCountry" runat="server"></uc:Country>
<hr />
<asp:GridView runat="server" ID="gvCustomers" AutoGenerateColumns="false">
<Columns>
<asp:BoundField DataField="CustomerID" HeaderText="Id" />
<asp:BoundField DataField="ContactName" HeaderText="Name" />
<asp:BoundField DataField="City" HeaderText="City" />
</Columns>
</asp:GridView>
</asp:Content>
Namepsaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
UserControl
C#
public event EventHandler OnChangeValue;
protected void OnCountryChanged(object sender, EventArgs e)
{
if (OnChangeValue != null)
{
OnChangeValue(sender, e);
}
}
VB.Net
Public Event OnChangeValue As EventHandler
Protected Sub OnCountryChanged(ByVal sender As Object, ByVal e As EventArgs)
RaiseEvent OnChangeValue(sender, e)
End Sub
Default
C#
protected void Page_Load(object sender, EventArgs e)
{
ucCountry.OnChangeValue += new EventHandler(ValueChanged);
}
protected void ValueChanged(object sender, EventArgs e)
{
DropDownList ddlCountry = (DropDownList)sender;
string country = ddlCountry.SelectedValue.Trim();
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
string query = "SELECT CustomerID,ContactName,City FROM Customers WHERE Country = @Country";
using (SqlConnection con = new SqlConnection(conString))
{
SqlCommand cmd = new SqlCommand(query);
cmd.Parameters.AddWithValue("@Country", country);
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
gvCustomers.DataSource = dt;
gvCustomers.DataBind();
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
ucCountry.OnChangeValue += New EventHandler(AddressOf ValueChanged)
End Sub
Protected Sub ValueChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim ddlCountry As DropDownList = CType(sender, DropDownList)
Dim country As String = ddlCountry.SelectedValue.Trim()
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Dim query As String = "SELECT CustomerID,ContactName,City FROM Customers WHERE Country = @Country"
Using con As SqlConnection = New SqlConnection(conString)
Dim cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Country", country)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
gvCustomers.DataSource = dt
gvCustomers.DataBind()
End Using
End Using
End Using
End Sub
Screenshot