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

C#
[ServiceContract]
public interface IService
{
    [OperationContract(Name = "EmployeeDetailsByEmpNoJson")]
    [WebInvoke(Method = "POST",
        ResponseFormat = WebMessageFormat.Json,
     UriTemplate = "EmployeeDetailsByEmpNoJson")]
    string EmployeeDetailsByEmpNoJson(string Schema, string Connection_Type, string Empno);

}


CLASS
C#
public class Service : IService
{
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

  public string EmployeeDetailsByEmpNoJson(string schema, string connect_Test_or_Production, string Empno)
  {

      try
      {


          if (fnOpenConnection(schema, connect_Test_or_Production) == true)
          {
              _objDataSet = new DataSet();
              _objOleDBCommand = new OleDbCommand();
              _objOleDBDataAdapter = new OleDbDataAdapter();

              _objOleDBCommand.Connection = _objOleDBConnection;
              _objOleDBCommand.CommandType = CommandType.Text;
              string strQuery = "select empname,desig,department,branch,to_char(doj,'dd-Mon-yyyy')doj,impact_lvl,comp_email,mob_co, project  from   vw_emp_dtls where  dol is null and empno=" + Empno + "";

              _objOleDBCommand.CommandText = strQuery;

              _objOleDBDataAdapter.SelectCommand = _objOleDBCommand;
              _objOleDBDataAdapter.Fill(_objDataSet);

              if (_objDataSet.Tables.Count > 0)
              {

                  System.Web.Script.Serialization.JavaScriptSerializer serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
                  List<Dictionary<string, object>> rows = new List<Dictionary<string, object>>();
                  Dictionary<string, object> row;
                  foreach (DataRow dr in _objDataSet.Tables[0].Rows)
                  {
                      row = new Dictionary<string, object>();
                      foreach (DataColumn col in _objDataSet.Tables[0].Columns)
                      {
                          row.Add(col.ColumnName, dr[col]);
                      }
                      rows.Add(row);
                  }
                  return serializer.Serialize(rows);

             
              }
              else
              {
                  
                  return null;
              }
          }
          else
          {
             
              return null;
          }
      }
      catch (OleDbException dbEx)
      {
          return null;
      }
      catch (Exception ex)
      {
          return null;
      }
      finally
      {
          _objDataSet = null;
          _objOleDBCommand = null;
          _objOleDBDataAdapter = null;
          fnCloseConnection();
      }
  }

}
Posted
Updated 16-Mar-15 0:53am
v3
Comments
Sinisa Hajnal 16-Mar-15 7:16am    
Type the path to the service into the browser, you should see its methods listed...click on one of them, fill the parameters and read see the answer on the screen.

1 solution

If you want to just test the output of a service .Use fiddler and check the output or add a testclient and check the output.

If you want to call the service using Jquery. You could use something like this. You could change the type to "POST" if you are posting something to the server.
XML
function CallService() {

var ajaxRequest = $.ajax({
type: "GET",
url: "http://localhost:36161/Service1.svc/EmployeeDetailsByEmpNoJson",
contentType: false,
processData: false
});


ajaxRequest.done(function (xhr, textStatus) {
alert(textStatus);
});

ajaxRequest.fail(function (xhr, textStatus) {
alert(textStatus);
});
}
 
Share this answer
 
v3
Comments
Subramanyam Shankar 16-Mar-15 8:01am    
If you want to call the service from the Jquery. You could use something like this. You could change the type to "POST" if you are posting something to the server.

function CallService() {

var ajaxRequest = $.ajax({
type: "GET",
url: "http://localhost:36161/Service1.svc/EmployeeDetailsByEmpNoJson",
contentType: false,
processData: false
});


ajaxRequest.done(function (xhr, textStatus) {
alert(textStatus);
});

ajaxRequest.fail(function (xhr, textStatus) {
alert(textStatus);
});
}
Subramanyam Shankar 16-Mar-15 8:31am    
You have rejected both the solutions. Please can you tell me what went wrong?

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