Click here to Skip to main content
11,805,604 members (47,310 online)
Rate this: bad
Please Sign up or sign in to vote.
Hello People,
Greetings from Anurag..
Ran into a problem today. I am trying to populate a dropdownlist via AJAX PageMethods,values for the DropDown list have to come from a SQL table.
Wrote a function in the Data Access Layer which actually returns a List after interacting with the SQL table.
Then, wrote a method in the Default.aspx.cs as a System.Web.Services.WebMethod, which actually will be called from a Javascript function.
When I start debugging, I encounter a NullReference exception when the Data Access Layer object is called in the static WebMethod in the Default.aspx.cs page.
I don't understand what am I missing over here. Experts please guide.

Below are my codes:

public class DAL
    SqlConnection xconn = new SqlConnection(ConfigurationManager.ConnectionStrings["Connect"].ToString());
    public List<Country> populateCountryDropDown()
        List<Country> listObj = new List<Country>();
        SqlDataAdapter da = new SqlDataAdapter("Select * from Sample.dbo.Country", xconn);
        DataTable dt = new DataTable();
            if (dt.Rows.Count > 0)
                for (int i = 0; i < dt.Rows.Count; i++)
                    Country obj = new Country();
           = dt.Columns[0].ToString();
           = dt.Columns[1].ToString();
            throw new ApplicationException("Some error has happened");
        return listObj;

public partial class _Default : System.Web.UI.Page
    public static List<Country> populate()
        DAL obj=new DAL();//Getting NullReference exception over here
        List<Country> listobject = obj.populateCountryDropDown();
        return listobject;
    protected void Page_Load(object sender, EventArgs e)

<html xmlns="">
<head runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=8"/>
    <link href="StyleSheet.css" rel="stylesheet" type="text/css" />
    <script type = "text/javascript">
    var countries;
    function CallMe() {
        countries= document.getElementById("<%=ddlCountry.ClientID %>");
        countries.options.length == 0;
    AddOption("Loading", "0");
window.onload = CallMe;
function OnSuccess(response) {
    countries.options.length = 0;
    AddOption("Please select", "0");
    for (var i in response) {
        AddOption(response[i].Name, response[i].value);
function AddOption(text, value22) {
    var option = document.createElement('<option value="' + value22 + '">');
    option.innerText = text;
    <form id="form1" runat="server">
    <h2>Let's populate DropDowns using Javascript and AJAX along with DataBases</h2>
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            <asp:DropDownList ID="ddlCountry" runat="server">
            <asp:DropDownList ID="ddlStates" runat="server">

public class Country
    private string _id;
    public string id
        get { return _id; }
        set { _id = value; }
    private string _name;
    public string name
        get { return _name; }
        set { _name = value; }

Is it that I can't populate values from a database using an AJAX PageMethod?
Any help or pointer would be highly appreciable.
Posted 18-Apr-13 2:20am
Sanjay K. Gupta at 18-Apr-13 8:48am
You are calling page method at page load. Why cannot you bind the Country dropdown at server side. After binding the Country, when user select country, bind State DropdownList by PageMethod.
Anurag Sinha V at 18-Apr-13 11:26am
Hi, I haven't called anything on Page_Load...let's forget about the states dropdown...i just wana populate the country dropdown using PageMethods.
Using server-side methods or even using a cascading dropdown is a cakewalk...wana do a different approach...Any idea where am I going wrong in the above code???

Sanjay K. Gupta at 18-Apr-13 12:37pm
window.onload = CallMe;

you are calling PageMethod at page load
Anurag Sinha V at 18-Apr-13 13:30pm
Ohhk..din't realize that..guess need to R&D u see any soln to my above code??

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 300
1 Richard Deeming 225
2 Sergey Alexandrovich Kryukov 170
3 ppolymorphe 125
4 CPallini 120
0 OriginalGriff 3,265
1 Maciej Los 1,990
2 KrunalRohit 1,907
3 CPallini 1,855
4 Richard MacCutchan 1,227

Advertise | Privacy | Mobile
Web02 | 2.8.151002.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100