//---------------------------------------------------------------------------------------------------------------------------------------------------
//Program Listing for:  Service1.asmx.cs
//Project:  xml
//Namespace:  cs
//----------------------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Web;
using System.Web.Services;
using System.Data.OleDb;
using System.Data.SqlClient;
using System.Data.Common;
using System.Data.SqlTypes;
using Microsoft.Data.Odbc;

namespace remote_fx
{

         [WebService(Namespace="http://www.sboe.state.az.us/remote_fx/")]

         /// <summary>
         /// critical data services for remote program invocation
         /// </summary>
         public class DataService : System.Web.Services.WebService
         {

                  private static string lastError = "";

                  /// <summary>
                  /// returns Conn String
                  /// </summary>
                  public string GetConnectionString() 
                  {

                           string connstring = @"REDACTED;";
                           return connstring;
                  }

                  /// <summary>
                  /// containing class constructor, does default XML Service stuff
                  /// </summary>
                  public DataService()
                  {

                           /// <summary>
                           /// must set a security model to run on an unknown client
                           /// </summary>
                           AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.UnauthenticatedPrincipal);
                           System.Security.Permissions.PrincipalPermission myPerm = new
                                             System.Security.Permissions.PrincipalPermission(null, "BUILTIN\\Administrators");

                           //CODEGEN: This call is required by the ASP.NET Web Services Designer
                           InitializeComponent();
                  }

                  #region Component Designer generated code
                  
                  //Required by the Web Services Designer 
                  private IContainer components = null;
                                    
                  /// <summary>
                  /// Required method for Designer support - do not modify
                  /// the contents of this method with the code editor.
                  /// </summary>
                  private void InitializeComponent()
                  {
                  }

                  /// <summary>
                  /// Clean up any resources being used.
                  /// </summary>
                  protected override void Dispose( bool disposing )
                  {
                           if(disposing && components != null)
                           {
                                    components.Dispose();
                           }
                           base.Dispose(disposing);                  
                  }
                  
                  #endregion

                  // WEB SERVICE EXAMPLE

                  /// <summary>
                  /// basic function
                  /// </summary>
                  [WebMethod]
                  public string HelloWorld()
                  {
                           return "Hello World";
                  }

                  /// <summary>
                  /// process a select query, return a DataSet (you can return any .NET type 
                  /// via HTTP/SOAP mechanism).  verifyString is crude password mechanism
                  /// </summary>
                  [WebMethod]
                  public DataSet GetDataSetSelect(string selectString, string verifyString)
                  {

                           // password obfuscated for obvious reasons
                           if (verifyString != "@#$ASDF@DSF") 
                           {
                                    return null;
                           }
                           Microsoft.Data.Odbc.OdbcDataAdapter da1 = 
                                             new OdbcDataAdapter( selectString, GetConnectionString() ) ;

                      DataSet mydataset = new DataSet();
                           try 
                           {
                                    da1.Fill(mydataset, "view1");

                           }
                           catch 
                           {
                                    mydataset = null;

                           }
                           finally 
                           {

                           }

                           return mydataset;

                  }

                  /// <summary>
                  /// execute update or insert command, returns negative int for error
                  /// or number of rows updated (can be zero), verifyString crude password
                  /// </summary>
                  [WebMethod]
                  public Int32 ExecuteCommand(string commandString, string verifyString) 
                  {

                           // password obfuscated for obvious reasons
                           if (verifyString != "#$%@#$!@!#$@#$$") 
                           {
                                    return -2;
                           }
                           int records = -1;
                           OdbcConnection conn1 = new OdbcConnection( GetConnectionString() );
                           try 
                           {
                                    conn1.Open();
                                    OdbcCommand cmd1 = new OdbcCommand( commandString, conn1);
                                    records = cmd1.ExecuteNonQuery();
                           }
                           catch (System.Exception this_exception)
                           {
                                    string text = this_exception.ToString();
                                    SetLastError(text);
                           }
                           finally
                           {
                                    conn1.Close();
                            }
                           return records;
                  }

                  /// <summary>
                  /// basic error mechanism to get back last error info
                  /// </summary>
                  [WebMethod]
                  public string GetLastError()
                  {
                           return lastError;

                  }

                  /// <summary>
                  /// clear error mechanism
                  /// </summary>
                  [WebMethod]
                  public void ClearLastError()
                  {
                           lastError = "";

                  }

                  /// <summary>
                  /// set last error mechanism
                  /// </summary>
                  private void SetLastError(string sError)
                  {
                           lastError = sError;

                  }

         }

}