Hi nauna,
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:DropDownList ID="ddlCustomers" runat="server">
</asp:DropDownList>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>
<script type="text/javascript">
$(function () {
var loadedDropDownList = false;
$('[id*=ddlCustomers]').on('click', function () {
if (loadedDropDownList) {
return;
} else {
$.ajax({
type: "POST",
url: "Handler.ashx/GetCustomers",
data: '{}',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (response) {
var ddlCustomers = $("[id*=ddlCustomers]");
ddlCustomers.empty().append('<option selected="selected" value="0">Please select</option>');
$.each(response, function () {
ddlCustomers.append($("<option></option>").val(this['Value']).html(this['Text']));
});
loadedDropDownList = true;
}
});
}
});
});
</script>
Handler
C#
<%@ WebHandler Language="C#" Class="Handler" %>
using System;
using System.Web;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Web.UI.WebControls;
using System.Web.Script.Serialization;
public class Handler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
List<ListItem> customers = new List<ListItem>();
string query = "SELECT TOP 5 CustomerID, ContactName FROM Customers";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
while (sdr.Read())
{
customers.Add(new ListItem
{
Value = sdr["CustomerID"].ToString(),
Text = sdr["ContactName"].ToString()
});
}
}
con.Close();
}
}
JavaScriptSerializer serializer = new JavaScriptSerializer();
context.Response.ContentType = "text/plain";
context.Response.Write(serializer.Serialize(customers));
}
public bool IsReusable
{
get
{
return false;
}
}
}
VB.Net
<%@ WebHandler Language="VB" Class="Handler" %>
Imports System
Imports System.Web
Imports System.Collections.Generic
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Web.UI.WebControls
Imports System.Web.Script.Serialization
Public Class Handler : Implements IHttpHandler
Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
Dim customers As New List(Of ListItem)()
Dim query As String = "SELECT TOP 5 CustomerID, ContactName FROM Customers"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As New SqlConnection(constr)
Using cmd As New SqlCommand(query)
cmd.CommandType = CommandType.Text
cmd.Connection = con
con.Open()
Using sdr As SqlDataReader = cmd.ExecuteReader()
While sdr.Read()
customers.Add(New ListItem() With {
.Value = sdr("CustomerID").ToString(),
.Text = sdr("ContactName").ToString()
})
End While
End Using
con.Close()
End Using
End Using
Dim serializer As JavaScriptSerializer = New JavaScriptSerializer()
context.Response.ContentType = "text/plain"
context.Response.Write(serializer.Serialize(customers))
End Sub
Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
Get
Return False
End Get
End Property
End Class
Screenshot