Davin Martyn

Davin Martyn

  • NA
  • 7.5k
  • 3m

CascadingDropDownList with WebService

Jul 3 2012 1:46 AM
Hello Sir
I am working in AJAX and use CascadingDropDownList with WebService. I want to dispaly a record through sqlserve. when run our application then following exception show.
Exception is : Object reference not set to an instance of an object.

Webservice.asmx code : 

[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 am : System.Web.Services.WebService

{

   private static string connection = ConfigurationManager.AppSettings["a"].ToString();

    SqlConnection con = new SqlConnection(connection);

    public am () {

        //Uncomment the following line if using designed components

   //InitializeComponent();

    }

    [WebMethod]

    public CascadingDropDownNameValue[] BindCountryDetails(string knownCategoryValues, string category)

    {

        con.Open();

        SqlCommand cmd = new SqlCommand("select * from country",con);

        SqlDataAdapter da = new SqlDataAdapter(cmd);

       DataSet ds = new DataSet();

        da.Fill(ds);

        cmd.ExecuteNonQuery();

       con.Close();

        List<CascadingDropDownNameValue> cdetaile = new List<CascadingDropDownNameValue>();

        foreach ( DataRow dtrow in ds.Tables[0].Rows)

        {

            string name = dtrow ["name"].ToString();

            string id= dtrow["id"].ToString();

            cdetaile.Add(new CascadingDropDownNameValue(name,id));

        }

        return cdetaile.ToArray();

    }

    }

 Default.aspx code :

<asp:DropDownList ID="DropDownList1" runat="server">

        </asp:DropDownList>

        <asp:CascadingDropDown ID="CascadingDropDown1" runat="server" TargetControlID="DropDownList1" Category="country" ServiceMethod="BindCountryDetails" ServicePath="~/am.asmx" PromptText="Select Country" LoadingText="Loading Countries..">

        </asp:CascadingDropDown>

 please define what problem is create in my webservice code and also define in ASP.NET 4.0 add webservice refrence is important in particular application.
Thanks.

 

 

 

Answers (3)