Hi SUJAYS,
I have modified your code. Refer below sample code.
Database
For this example I have used of Northwind database that you can download using the link given below.
Download Northwind Database
HTML
<asp:TextBox runat="server" ID="TextBox1" AutoPostBack="true" OnTextChanged="TextBox1_TextChanged" />
<asp:DropDownList runat="server" ID="ddlDescription" >
</asp:DropDownList>
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Code
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string strQuery = "SELECT c.Description FROM Products p INNER JOIN Categories c ON p.CategoryID = c.CategoryID";
PopulateDropDownList(ddlDescription, "Description", "Description", strQuery);
}
}
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
string strQuery = "SELECT c.Description FROM Products p INNER JOIN Categories c ON p.CategoryID = c.CategoryID";
if (!string.IsNullOrEmpty(TextBox1.Text))
{
strQuery += " WHERE SupplierID = '" + TextBox1.Text + "'";
}
PopulateDropDownList(ddlDescription, "Description", "Description", strQuery);
}
private void PopulateDropDownList(DropDownList ddl, string dataTextField, string dataValueField, string query)
{
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
using (SqlDataAdapter da = new SqlDataAdapter(cmd))
{
con.Open();
DataSet ds = new DataSet();
da.Fill(ds);
ddl.DataTextField = dataTextField;
ddl.DataValueField = dataValueField;
ddl.DataSource = ds;
ddl.DataBind();
ddl.Items.Insert(0, new System.Web.UI.WebControls.ListItem("Please Select", "0"));
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim strQuery As String = "SELECT c.Description FROM Products p INNER JOIN Categories c ON p.CategoryID = c.CategoryID"
PopulateDropDownList(ddlDescription, "Description", "Description", strQuery)
End If
End Sub
Protected Sub TextBox1_TextChanged(ByVal sender As Object, ByVal e As EventArgs)
Dim strQuery As String = "SELECT c.Description FROM Products p INNER JOIN Categories c ON p.CategoryID = c.CategoryID"
If Not String.IsNullOrEmpty(TextBox1.Text) Then
strQuery += " WHERE SupplierID = '" & TextBox1.Text & "'"
End If
PopulateDropDownList(ddlDescription, "Description", "Description", strQuery)
End Sub
Private Sub PopulateDropDownList(ByVal ddl As DropDownList, ByVal dataTextField As String, ByVal dataValueField As String, ByVal query As String)
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As SqlDataAdapter = New SqlDataAdapter(cmd)
con.Open()
Dim ds As DataSet = New DataSet()
da.Fill(ds)
ddl.DataTextField = dataTextField
ddl.DataValueField = dataValueField
ddl.DataSource = ds
ddl.DataBind()
ddl.Items.Insert(0, New System.Web.UI.WebControls.ListItem("Please Select", "0"))
End Using
End Using
End Using
End Sub
Screenshot