Click here to Skip to main content
Rate this: bad
good
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:
 
DAL.cs:
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();
        try
        {
            xconn.Open();
            da.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    Country obj = new Country();
                    obj.id = dt.Columns[0].ToString();
                    obj.name = dt.Columns[1].ToString();
                    listObj.Add(obj);
                }
            }
        }
        catch
        {
            throw new ApplicationException("Some error has happened");
        }
        finally
        {
            xconn.Close();
        }
        return listObj;
    }
 
}
 
Default.aspx.cs:
public partial class _Default : System.Web.UI.Page
{
 
    [System.Web.Services.WebMethod]
    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)
    {
 
    }
}
 
Default.aspx:
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="X-UA-Compatible" content="IE=8"/>
    <title></title>
    <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");
    PageMethods.populate(OnSuccess);
}
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 + '">');
    countries.options.add(option);
    option.innerText = text;
}
</script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <h2>Let's populate DropDowns using Javascript and AJAX along with DataBases</h2>
        <p>&nbsp;</p>
        <p>
            <asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true">
            </asp:ScriptManager>
            <asp:DropDownList ID="ddlCountry" runat="server">
            </asp:DropDownList>
        </p>
        <p>&nbsp;</p>
        <p>
            <asp:DropDownList ID="ddlStates" runat="server">
            </asp:DropDownList>
        </p>
    </div>
    </form>
</body>
</html>
 
Country.cs:
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.
-Anurag
Posted 18-Apr-13 3:20am
Comments
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???
 
-regards
anurag
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 more..do 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 370
1 Sergey Alexandrovich Kryukov 329
2 BillWoodruff 235
3 Afzaal Ahmad Zeeshan 209
4 CPallini 195
0 OriginalGriff 5,560
1 DamithSL 4,476
2 Maciej Los 3,942
3 Kornfeld Eliyahu Peter 3,480
4 Sergey Alexandrovich Kryukov 3,175


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 18 Apr 2013
Copyright © CodeProject, 1999-2014
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