Click here to Skip to main content
14,836,727 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
In Ajax dropdown cascading i got these error.

System.NullReferenceException: Object reference not set to an instance of an object.
at DropdownWebService.BindCountrydropdown(String knownCategoryValues, String category)


My code is :

My code in webservice page is:
C#
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class DropdownWebService : System.Web.Services.WebService {

    [WebMethod]
    public CascadingDropDownNameValue[] BindCountrydropdown(string knownCategoryValues, string category)
    {
        SqlConnection concountry = new SqlConnection(ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString);
        concountry.Open();
        SqlCommand cmdcountry = new SqlCommand("select * from Country", concountry);
        SqlDataAdapter dacountry = new SqlDataAdapter(cmdcountry);
        cmdcountry.ExecuteNonQuery();
        DataSet dscountry = new DataSet();
        dacountry.Fill(dscountry);
        concountry.Close();
        List<cascadingdropdownnamevalue> countrydetails = new List<cascadingdropdownnamevalue>();
        foreach (DataRow dtrow in dscountry.Tables[0].Rows)
        {
            string CountryID = dtrow["CountryID"].ToString();
            string CountryName = dtrow["CountryName"].ToString();
            countrydetails.Add(new CascadingDropDownNameValue(CountryName, CountryID));
        }
        return countrydetails.ToArray();
    }
    [WebMethod]
    public CascadingDropDownNameValue[] BindStatedropdown(string knownCategoryValues, string category)
    {
        int CountryID;
        StringDictionary countrydetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        CountryID = Convert.ToInt32(countrydetails["Country"]);
        SqlConnection constate = new SqlConnection(ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString);
        constate.Open();
        SqlCommand cmdstate = new SqlCommand("select * from State where CountryID=@CountryID", constate);
        cmdstate.Parameters.AddWithValue("@CountryID", CountryID);
        cmdstate.ExecuteNonQuery();
        SqlDataAdapter dastate = new SqlDataAdapter(cmdstate);
        DataSet dsstate = new DataSet();
        dastate.Fill(dsstate);
        constate.Close();
        List<cascadingdropdownnamevalue> statedetails = new List<cascadingdropdownnamevalue>();
        foreach (DataRow dtstaterow in dsstate.Tables[0].Rows)
        {
            string stateID = dtstaterow["StateID"].ToString();
            string statename = dtstaterow["StateName"].ToString();
            statedetails.Add(new CascadingDropDownNameValue(statename, stateID));
        }
        return statedetails.ToArray();
    }
    [WebMethod]
    public CascadingDropDownNameValue[] BindRegiondropdown(string knownCategoryValues, string category)
    {
        int stateID;
        StringDictionary statedetails = AjaxControlToolkit.CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues);
        stateID = Convert.ToInt32(statedetails["State"]);
        SqlConnection conregion = new SqlConnection(ConfigurationManager.ConnectionStrings["cnnstring"].ConnectionString);
        conregion.Open();
        SqlCommand cmdregion = new SqlCommand("Select * from Region where StateID=@StateID", conregion);
        cmdregion.Parameters.AddWithValue("@StateID", stateID);
        cmdregion.ExecuteNonQuery();
        SqlDataAdapter daregion = new SqlDataAdapter(cmdregion);
        DataSet dsregion = new DataSet();
        daregion.Fill(dsregion);
        conregion.Close();
        List<cascadingdropdownnamevalue> regiondetails = new List<cascadingdropdownnamevalue>();
        foreach (DataRow dtregionrow in dsregion.Tables[0].Rows)
        {
            string regionID = dtregionrow["RegionID"].ToString();
            string regionname = dtregionrow["RegionName"].ToString();
            regiondetails.Add(new CascadingDropDownNameValue(regionname, regionID));

        }
        return regiondetails.ToArray();
    }
} 

so plz help me out to solve this error.
Posted
Updated 19-May-12 3:41am
v3

When the error is 500, that means it's an internal error, meaning internal to the service - the service threw an exception that was not caught.
Look in the Windows event logs on the service to see what went wrong.

Also, try:
- Go to menu Tools/Internet Options in your IE.
- Click on the Advanced tab, uncheck "Show friendly HTTP error messages" option and click Ok.
- Try accessing your web page again. Now you will see much more meaningful error message which will help you to troubleshoot the problem.

Other possibility:
There might be an issue in the way you have hosted your service. May be multiple config files?
   
v2
Comments
VJ Reddy 12-May-12 23:16pm
   
Good suggestion. 5!
Sandeep Mewara 13-May-12 1:06am
   
Thanks.
Since error 500 is a generic error and doesn't tell much about the actual problem, I suggest that you use try..catch blocks in your web services the get information abuot the root cause of the problem.
   

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




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900