Saturday, February 28, 2009

CascadingDropDown Method error 500 Solution

I got same error for fill DropDownlist using CascadingDropDown ajax control and web service.

I tried to solve this problem and i got the solution which i share with my friends.


I got the error because of two things

First I did not add attribute.

Second I wrote wrong logic.



Solution

• Add attribute
[System.Web.Script.Services.ScriptService]
. Check your logic in web method

Fill Dropdown list using CascadingDropDown Ajax Control and Web Service


I am taking example of fill dropdown list for state using CascadingDropDown and Web service.

  1. Web Service

When we create web service its code file which is located in App_Code folder looks like.

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols

‘WebService(Namespace:="http://tempuri.org/")’_

‘WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)’ _

‘Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()’ _

Public Class stateServices

Inherits System.Web.Services.WebService

‘WebMethod()’ _

Public Function HelloWorld() As String

Return "Hello World"

End Function

End Class

Import some other namespace listed below

Imports AjaxControlToolkit

Imports System.Data

Imports System.Collections

Imports System.Collections.Generic

Imports System.Collections.Specialized

After this add one more attribute abow to Class stateServices

System.Web.Script.Services.ScriptService()

Now I am going to create webmothod which return array of type CascadingDropDownNameValue() for fill up drop down list with state name.

‘WebMethod()’ _

Public Function GetState(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()

Dim tempTableAs New DataTable

With tempTable

.Columns.Add("id", GetType(String))

.Columns.Add("state_name", GetType(String))

End With

Dim drow As DataRow

drow = tempTable.NewRow()

drow("id") = "1"

drow("state_name") = "Gujarat"

tempTable.Rows.Add(drow)

drow = tempTable.NewRow()

drow("id") = "2"

drow("state_name") = "Maharastra"

tempTable.Rows.Add(drow)

drow = tempTable.NewRow()

drow("id") = "3"

drow("state_name") = "Rajastan"

tempTable.Rows.Add(drow)

Dim values As New List(Of CascadingDropDownNameValue)

For Each dr As DataRow In tempTable.Rows

values.Add(New CascadingDropDownNameValue(dr("state_name"), dr("id")))

Next

Return values.ToArray()

End Function

After finished all work our webservice file look like

For Vb.net

Imports System.Web

Imports System.Web.Services

Imports System.Web.Services.Protocols

Imports AjaxControlToolkit

Imports System.Data

Imports System.Collections

Imports System.Collections.Generic

Imports System.Collections.Specialized

‘WebService(Namespace:="http://tempuri.org/")’ _

‘WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)’ _

‘Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()’ _

‘System.Web.Script.Services.ScriptService()’ _

Public Class stateServices

Inherits System.Web.Services.WebService

‘WebMethod()’ _

Public Function GetState(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()

Dim tempTableAs New DataTable

With tempTable

.Columns.Add("id", GetType(String))

.Columns.Add("state_name", GetType(String))

End With

Dim drow As DataRow

drow = tempTable.NewRow()

drow("id") = "1"

drow("state_name") = "Gujarat"

tempTable.Rows.Add(drow)

drow = tempTable.NewRow()

drow("id") = "2"

drow("state_name") = "Maharastra"

tempTable.Rows.Add(drow)

drow = tempTable.NewRow()

drow("id") = "3"

drow("state_name") = "Rajastan"

tempTable.Rows.Add(drow)

Dim values As New List(Of CascadingDropDownNameValue)

For Each dr As DataRow In tempTable.Rows

values.Add(New CascadingDropDownNameValue(dr("state_name"), dr("id")))

Next

Return values.ToArray()

End Function

End Class

For C#

using System;

using System.Web;

using System.Collections;

using System.Collections.Generic;

using System.Collections.Specialized;

using System.Web.Services;

using System.Web.Services.Protocols;

using AjaxControlToolkit;

using System.Data;

using System.Data.SqlClient;

[WebService(Namespace = "http://tempuri.org/")]

[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]

[System.Web.Script.Services.ScriptService]

public class stateServices : System.Web.Services.WebService {

public stateServices () {

//Uncomment the following line if using designed components

//InitializeComponent();

}

[WebMethod]

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

{

DataTable tempTable=new DataTable();

tempTable.Columns.Add("id", typeof(string));

tempTable.Columns.Add("state_name", typeof(string));

DataRow drow;

drow = tempTable.NewRow();

drow["id"] = "1";

drow["state_name"] = "Gujarat";

tempTable.Rows.Add(drow);

drow = tempTable.NewRow();

drow["id"] = "1";

drow["state_name"] = " Maharastra";

tempTable.Rows.Add(drow);

drow = tempTable.NewRow();

drow["id"] = "1";

drow["state_name"] = " Rajastan";

tempTable.Rows.Add(drow);

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

foreach (DataRow dr in tempTable.Rows)

{

values.Add(new CascadingDropDownNameValue (string)dr["state_name"], (string)dr["id"]));

}

return values.ToArray();

}

  1. web Form

Add ScriptManager.

Add Dropdownlist rename it with ddstate.

Add CascadingDropDown Ajax Control rename it with cddstate.

Set TargetControlID Property of cddstate to ddstate

Set cddstate(CascadingDropDownproperty) properties for ddstate in its property window

BehaviorID property to cddstate

Category property to State

LoadingText property to Please wait ...........

PromptText property to Select State ............

ServiceMethod property to GetState

ServicePath property to stateServices.asmx

Start debugging application.

I use ‘ quote for attribute which is highlighted to use code replace starting quote with

'Greate then sign' and ending quote with 'Less then sign'