Click here to Skip to main content
15,886,258 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I have a problem in Calling the WCF Service from JavaScript.

I am using ajax autocomplete extender, a stored procedure, to fetch a bunch of records from sql table.
I am unable to figure out where I made a mistake.
Can anyone help me?

C#
designer.cs
   public System.Data.Linq.Table<icdcode> ICDCODEs
   {
      get
      {
         return this.GetTable<icdcode>();
      }
   }
}
[global::System.Data.Linq.Mapping.TableAttribute(Name="dbo.ICDCODE")]
public partial class ICDCODE
{
    private string _CODE;
    private string _DESCRIPTION;
    public ICDCODE()
    {
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_CODE", DbType="NVarChar(20) NOT NULL", CanBeNull=false)]
    public string CODE
    {
        get
        {
            return this._CODE;
        }
        set
        {
            if ((this._CODE != value))
            {
                this._CODE = value;
            }
        }
    }
    [global::System.Data.Linq.Mapping.ColumnAttribute(Storage="_DESCRIPTION", DbType="NVarChar(200) NOT NULL", CanBeNull=false)]
    public string DESCRIPTION
    {
        get
        {
            return this._DESCRIPTION;
        }
        set
        {
            if ((this._DESCRIPTION != value))
            {
                this._DESCRIPTION = value;
            }
        }
        
GetDataCode.cs  //service name.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Windows.Forms;
// NOTE: You can use the "Rename" command on the "Refactor" menu to change the class name "GetDataCode" in code, svc and config file together.

    [ServiceContract(Namespace = "QAWorks")]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    public class GetDataCode
    {
        public void DoWork()
        {
        }
        public String[] getResult(string input)
        {
            string description=input;
            String[] result;
            using(DATADataContext db=new DATADataContext())
            {
                result = (from p in db.ICDCODEs
                          where p.DESCRIPTION.Contains(input)
                          select p.DESCRIPTION).ToArray();
            }
            return result;

        }
    }
    
<asp:TextBox ID="txtDiagnosis" runat="server"  Width="150px" />;
<ajaxToolkit:TextBoxWatermarkExtender ID="AJXPDIAGNOSIS"  runat="server" TargetControlID="txtDiagnosis"  WatermarkCssClass="watermarkedSingleLine" WatermarkText="DIAGNOSIS" />;
<ajaxToolkit:AutoCompleteExtender ID="AJXFILLDIAGNOSIS"  runat="server" CompletionInterval="100" DelimiterCharacters=" " Enabled="True" FirstRowSelected="True" MinimumPrefixLength="1"  önClientItemSelected="FillDiagnosis(this)" ServiceMethod="getResult" ServicePath="~/Services/GetDataCode.svc" TargetControlID="txtDiagnosis">

Java script
function FillDiagnosis() {
   var input = document.getElementById('<%=txtDiagnosis.ClientID%>').value;
   GetDataCode.getResult(input, OnWSRequestComplete);
}
function OnWSRequestComplete(results) {
   alert(results);
}
</icdcode></icdcode>
Posted
Updated 12-Jan-11 21:33pm
v3
Comments
Anupama Roy 12-Jan-11 10:39am    
Can you specify what the problem is?Does it throw any exception?
fjdiewornncalwe 12-Jan-11 22:15pm    
OK. Now that you have dumped all your code here, could you please highlight where the problem is. We are not here to take your code and develop a solution from it to debug it for you. If you highlight where an error occurs and the error that is thrown, you will likely get a very quick and valid solution to your query.
HimanshuJoshi 12-Jan-11 22:39pm    
Edited to remove extra code blocks that seemed to be messing with the formatting of the entire page.
Hiren solanki 13-Jan-11 1:43am    
OP:hi,
i am unable to call the method in WCF service. i have a problem in this line.
Collapse
GetDataCode.getResult(input, OnWSRequestComplete);
Dalek Dave 13-Jan-11 3:33am    
Edited for Grammar and Readability.

Try changing the following:
JavaScript
var input = document.getElementById('<%=txtDiagnosis.ClientID%>').value;

to:
JavaScript
var input = document.getElementById('txtDiagnosis').value; // edited from .Text to .value


[EDIT]
Things to look at:
1. [OperationContract] is missing from getResult function.
2. getResult method signature does not match the necessary one for the AutoCompleteExtender according to the documentation[^] for use as a ServiceMethod.
3. GetDataCode.getResult is passing two parameters, however, the method signature only shows one.
[/EDIT]
 
Share this answer
 
v2
Comments
karthi34 13-Jan-11 2:36am    
I tried but results Negative .

thanks,
JOAT-MON 13-Jan-11 3:32am    
Sorry it did not help. Try looking at this tutorial here:

http://dotnetbyexample.blogspot.com/2008/02/calling-wcf-service-from-javascript.html

and making sure you have all the right attributes added, etc.
Hurrah!!
I completed my task finally. Thanks for the help to everyone. ;)
 
Share this answer
 

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