Wednesday, August 29, 2012

Webservices in asp.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data.SqlClient;
using System.Data;
using System.Configuration;

namespace Activayt.UI
{
    /// <summary>
    /// Summary description for WebService1
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // 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 WebService1 : System.Web.Services.WebService
    {
       [System.Web.Services.WebMethod(EnableSession=true)]
       [System.Web.Script.Services.ScriptMethod]
        public  string[] GetGroupName(string prefixText)
        {
            string s = Session["UserName"].ToString();
            SqlConnection cn = new SqlConnection();
            DataSet ds = new DataSet();
            DataTable dt = new DataTable();
            String strCn = ConfigurationManager.ConnectionStrings["SQLConString"].ToString();
            cn.ConnectionString = strCn;
            SqlCommand cmd = new SqlCommand();
            cmd.Connection = cn;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "select Distinct Name from tbl_Groups G left outer join  tbl_GroupXUsers GU on G.GroupID=GU.GroupID where G.Status=1 and ((GU.UserID=(select UserID from tbl_Users where Email='" + Session["UserName"].ToString() + "') and MasterAccountID=(select AccountID from tbl_Accounts  where Name='" + Session["AccountName"].ToString() + "')) or CreatedBy='" + Session["UserName"].ToString() + "') and Name like @myParameter";
            cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");

            try
            {
                cn.Open();
                cmd.ExecuteNonQuery();
                SqlDataAdapter da = new SqlDataAdapter(cmd);
                da.Fill(ds);
            }
            catch
            {
            }
            finally
            {
                cn.Close();
            }
            dt = ds.Tables[0];

            List<string> txtGroupNames = new List<string>();
            String dbValues;

            foreach (DataRow row in dt.Rows)
            {

                dbValues = row["Name"].ToString();
                dbValues = dbValues.ToLower();
                txtGroupNames.Add(dbValues);
            }

            return txtGroupNames.ToArray();
        }
       [System.Web.Services.WebMethod(EnableSession = true)]
       [System.Web.Script.Services.ScriptMethod]
       public string[] GetManagers(string prefixText)
       {
           string s = Session["UserName"].ToString();
           SqlConnection cn = new SqlConnection();
           DataSet ds = new DataSet();
           DataTable dt = new DataTable();
           String strCn = ConfigurationManager.ConnectionStrings["SQLConString"].ToString();
           cn.ConnectionString = strCn;
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = cn;
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "select distinct Email from tbl_GroupXUsers GU inner join tbl_Groups G on G.GroupID=GU.GroupID inner join tbl_Users U on U.UserID=GU.UserID where GU.UserID=(select UserID from tbl_Users where Email='" + Session["UserName"].ToString() + "' and Status=1) and Role='manager' and G.Status=1 and MasterAccountID=(select AccountID from tbl_Accounts where Name='" + Session["AccountName"].ToString() + "') and Email like @myParameter";
           cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");
           try
           {
               cn.Open();
               cmd.ExecuteNonQuery();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(ds);
           }
           catch
           {
           }
           finally
           {
               cn.Close();
           }
           dt = ds.Tables[0];

           List<string> txtManagers = new List<string>();
           String dbValues;

           foreach (DataRow row in dt.Rows)
           {

               dbValues = row["Email"].ToString();
               dbValues = dbValues.ToLower();
               txtManagers.Add(dbValues);
           }

           return txtManagers.ToArray();
       }
       [System.Web.Services.WebMethod(EnableSession = true)]
       [System.Web.Script.Services.ScriptMethod]
       public string[] GetAnalyst(string prefixText)
       {
           string s = Session["UserName"].ToString();
           SqlConnection cn = new SqlConnection();
           DataSet ds = new DataSet();
           DataTable dt = new DataTable();
           String strCn = ConfigurationManager.ConnectionStrings["SQLConString"].ToString();
           cn.ConnectionString = strCn;
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = cn;
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "select distinct Email from tbl_GroupXUsers GU inner join tbl_Groups G on G.GroupID=GU.GroupID inner join tbl_Users U on U.UserID=GU.UserID where GU.UserID=(select UserID from tbl_Users where Email='" + Session["UserName"].ToString() + "' and Status=1) and Role='analyst' and G.Status=1  and MasterAccountID=(select AccountID from tbl_Accounts where Name='" + Session["AccountName"].ToString() + "') and  Email like @myParameter";
           cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");

           try
           {
               cn.Open();
               cmd.ExecuteNonQuery();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(ds);
           }
           catch
           {
           }
           finally
           {
               cn.Close();
           }
           dt = ds.Tables[0];

           List<string> txtManagers = new List<string>();
           String dbValues;

           foreach (DataRow row in dt.Rows)
           {

               dbValues = row["Email"].ToString();
               dbValues = dbValues.ToLower();
               txtManagers.Add(dbValues);
           }

           return txtManagers.ToArray();
       }
       [System.Web.Services.WebMethod(EnableSession = true)]
       [System.Web.Script.Services.ScriptMethod]
       public string[] GetAdministrator(string prefixText)
       {
           string s = Session["UserName"].ToString();
           SqlConnection cn = new SqlConnection();
           DataSet ds = new DataSet();
           DataTable dt = new DataTable();
           String strCn = ConfigurationManager.ConnectionStrings["SQLConString"].ToString();
           cn.ConnectionString = strCn;
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = cn;
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "select distinct Email from tbl_GroupXUsers GU inner join tbl_Groups G on G.GroupID=GU.GroupID inner join tbl_Users U on U.UserID=GU.UserID where GU.UserID=(select UserID from tbl_Users where Email='" + Session["UserName"].ToString() + "' and Status=1) and Role='administrator' and G.Status=1 and MasterAccountID=(select AccountID from tbl_Accounts where Name='" + Session["AccountName"].ToString() + "') and Email like @myParameter";
           cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");

           try
           {
               cn.Open();
               cmd.ExecuteNonQuery();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(ds);
           }
           catch
           {
           }
           finally
           {
               cn.Close();
           }
           dt = ds.Tables[0];

           List<string> txtManagers = new List<string>();
           String dbValues;

           foreach (DataRow row in dt.Rows)
           {

               dbValues = row["Email"].ToString();
               dbValues = dbValues.ToLower();
               txtManagers.Add(dbValues);
           }

           return txtManagers.ToArray();
       }
       [System.Web.Services.WebMethod(EnableSession = true)]
       [System.Web.Script.Services.ScriptMethod]
       public string[] GetMembers(string prefixText)
       {
           SqlConnection cn = new SqlConnection();
           DataSet ds = new DataSet();
           DataTable dt = new DataTable();
           String strCn = ConfigurationManager.ConnectionStrings["SQLConString"].ToString();
           cn.ConnectionString = strCn;
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = cn;
           cmd.CommandType = CommandType.Text;
           cmd.CommandText = "select distinct Email from tbl_Roles R inner join tbl_RoleXUsers RU on RU.RoleID=R.RoleID inner join tbl_Users U on U.UserID=RU.UserID where RU.UserID="+Convert.ToInt64(Session["UserID"])+" and R.MasterAccountID="+Convert.ToInt64(Session["AccountID"])+" and Email like @myParameter";
           cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%");

           try
           {
               cn.Open();
               cmd.ExecuteNonQuery();
               SqlDataAdapter da = new SqlDataAdapter(cmd);
               da.Fill(ds);
           }
           catch
           {
           }
           finally
           {
               cn.Close();
           }
           dt = ds.Tables[0];
           List<string> txtMembers = new List<string>();
           String dbValues;
           foreach (DataRow row in dt.Rows)
           {
               dbValues = row["Email"].ToString();
               dbValues = dbValues.ToLower();
               txtMembers.Add(dbValues);
           }
           return txtMembers.ToArray();
       }
    }
}

No comments:

Post a Comment