Hi bhushan98,
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
<asp:TextBox runat="server" ID="txtCountry" />
<asp:Button Text="Search" runat="server" OnClick="OnSearch" />
<hr />
<asp:DropDownList runat="server" ID="ddlCustomers" Width="220px">
</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 (!this.IsPostBack)
{
this.BindDropDownList();
}
}
protected void OnSearch(object sender, EventArgs e)
{
this.BindDropDownList();
}
private void BindDropDownList()
{
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT CustomerID,ContactName FROM Customers WHERE Country = @Country OR @Country IS NULL ORDER BY ContactName ASC";
cmd.Parameters.AddWithValue("@Country", !string.IsNullOrEmpty(txtCountry.Text) ? txtCountry.Text : (object)DBNull.Value);
DataTable dt = GetData(cmd);
ddlCustomers.DataSource = dt;
ddlCustomers.DataTextField = "ContactName";
ddlCustomers.DataValueField = "CustomerID";
ddlCustomers.DataBind();
ddlCustomers.Items.Insert(0, new ListItem { Text = "Select", Value = "0" });
// Get value to be selected from datrabase.
// Select using FindByText.
if (ddlCustomers.Items.FindByText("Elizabeth Brown") != null)
{
ddlCustomers.ClearSelection();
ddlCustomers.Items.FindByText("Elizabeth Brown").Selected = true;
}
// Select using FindByValue.
if (ddlCustomers.Items.FindByValue("LONEP") != null)
{
ddlCustomers.ClearSelection();
ddlCustomers.Items.FindByValue("LONEP").Selected = true;
}
}
private DataTable GetData(SqlCommand cmd)
{
string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataTable dt = new DataTable())
{
sda.Fill(dt);
return dt;
}
}
}
}
VB.Net
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
If Not Me.IsPostBack Then
Me.BindDropDownList()
End If
End Sub
Protected Sub OnSearch(ByVal sender As Object, ByVal e As EventArgs)
Me.BindDropDownList()
End Sub
Private Sub BindDropDownList()
Dim cmd As SqlCommand = New SqlCommand()
cmd.CommandText = "SELECT CustomerID,ContactName FROM Customers WHERE Country = @Country OR @Country IS NULL ORDER BY ContactName ASC"
cmd.Parameters.AddWithValue("@Country", If(Not String.IsNullOrEmpty(txtCountry.Text), txtCountry.Text, CObj(DBNull.Value)))
Dim dt As DataTable = GetData(cmd)
ddlCustomers.DataSource = dt
ddlCustomers.DataTextField = "ContactName"
ddlCustomers.DataValueField = "CustomerID"
ddlCustomers.DataBind()
ddlCustomers.Items.Insert(0, New ListItem With {.Text = "Select", .Value = "0"})
' Get value to be selected from datrabase.
' Select using FindByText.
If ddlCustomers.Items.FindByText("Elizabeth Brown") IsNot Nothing Then
ddlCustomers.ClearSelection()
ddlCustomers.Items.FindByText("Elizabeth Brown").Selected = True
End If
' Select using FindByValue.
If ddlCustomers.Items.FindByValue("LONEP") IsNot Nothing Then
ddlCustomers.ClearSelection()
ddlCustomers.Items.FindByValue("LONEP").Selected = True
End If
End Sub
Private Function GetData(ByVal cmd As SqlCommand) As DataTable
Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(conString)
Using sda As SqlDataAdapter = New SqlDataAdapter()
cmd.Connection = con
sda.SelectCommand = cmd
Using dt As DataTable = New DataTable()
sda.Fill(dt)
Return dt
End Using
End Using
End Using
End Function
Screenshot