Thursday, October 25, 2012

Tree View bind dynamically in asp.net

aspx:
 
<asp:TreeView ID="tvNavigator" ExpandDepth="1" PopulateNodesFromClient="true" ShowLines="true"
ShowExpandCollapse="true" runat="server" SelectedNodeStyle-BackColor="AliceBlue" />
aspx.cs:

private void ShowNavigatorMenu(int ID,int UserID){

ResponseStore<List<ECS.Business.Foundation.Navigator>> navigatorList = objNavigatorBroker.SelectNodes(ID,UserID);
if (navigatorList.Object != null){
objParentNodesList = navigatorList.Object;

foreach (ECS.Business.Foundation.Navigator treeNode in objParentNodesList.Where(item => item.ID == item.ParentID && item.Type.ToLower() == "Lev".ToLower())){

TreeNode ParentNode = new TreeNode();ParentNode.Text = treeNode.Name.ToString();
ParentNode.Value = treeNode.ID.ToString();
ParentNode.NavigateUrl =
"javascript:LevelClick('" + treeNode.ID + "')";tvNavigator.Nodes.Add(ParentNode);
RecursiveChild(ParentNode);
}
}
}

private void RecursiveChild(TreeNode ParentNode){

if (objParentNodesList.Count(item => item.ID != item.ParentID && item.ParentID.ToString() == ParentNode.Value) > 0){

foreach (ECS.Business.Foundation.Navigator chTreeNode in objParentNodesList.Where(item => item.ID != item.ParentID && item.ParentID.ToString() == ParentNode.Value)){

TreeNode childNode = new TreeNode();childNode.Text = chTreeNode.Name.ToString();
childNode.Value = chTreeNode.ID.ToString();
childNode.NavigateUrl =
"javascript:LevelClick('" + chTreeNode.ID + "')";ParentNode.ChildNodes.Add(childNode);

if (chTreeNode.Type.ToLower() == "Lev".ToLower()){
RecursiveChild(childNode);
}

else{
// show some image for location childNode.NavigateUrl = "javascript:LocationClick('" + chTreeNode.ID+ "')";childNode.ImageUrl =
"~/App_Images/Location.png";}
}
}
}

Treeview with radio button in asp.net

aspx:

<asp:TreeView ID="tvLevel" ExpandDepth="1" PopulateNodesFromClient="true" ShowLines="true"
ShowExpandCollapse="true" runat="server" Width="255px" Height="120px" Style="overflow: auto;
border: 1px solid #999999; border-radius: 4px 4px 4px 4px;"></asp:TreeView>
aspx.cs:

private void ShowLevelTreeview(int ParentID)
{
tvLevel.Nodes.Clear();

ResponseStore<List<ECS.Business.Foundation.Level>> LevelList = objLevelBroker.LevelSelectByParentID(ParentID);
if (LevelList.Object != null){
objParentNodesList = LevelList.Object;

foreach (ECS.Business.Foundation.Level treeNode in objParentNodesList.Where(item => item.ID == item.ParentID)){

TreeNode ParentNode = new TreeNode();ParentNode.Text =
"<input type='radio' name='rbLevel' value ='" + treeNode.ID.ToString() + "' id='rbLevel" + radindex + "'/>" + treeNode.Name;radindex++;

// ParentNode.Text =treeNode.Name.ToString();ParentNode.Value = treeNode.ID.ToString();
tvLevel.Nodes.Add(ParentNode);
RecursiveChild(ParentNode);
}
}
}
private void RecursiveChild(TreeNode ParentNode){

if (objParentNodesList.Count(item => item.ID != item.ParentID && item.ParentID.ToString() == ParentNode.Value) > 0){

foreach (ECS.Business.Foundation.Level chTreeNode in objParentNodesList.Where(item => item.ID != item.ParentID && item.ParentID.ToString() == ParentNode.Value)){

TreeNode childNode = new TreeNode();childNode.Text =
"<input type='radio' name='rbLevel' value ='" + chTreeNode.ID.ToString() + "' id='rbLevel" + radindex + "'/>" + chTreeNode.Name;radindex++;
childNode.Value = chTreeNode.ID.ToString();
ParentNode.ChildNodes.Add(childNode);
RecursiveChild(childNode);
}
}
}

multiple records insert in single statement using cross jopin in sql server

INSERT INTO table_name(Coll1,Coll2,Coll3)

SELECT @Variable1,C.items,E.items
FROM dbo.Split(@Variables, ',') AS C CROSS JOINdbo.Split(@Variables1, ',') AS E


Ex:


INSERT
INTO tbl_LMS_SessionEmpCourse_Xref(SessionID,CourseID,EmployeeID)
SELECT @SessionID,C.items,E.items
FROM dbo.Split(@CourseIDs, ',') AS C CROSS JOINdbo.Split(@EmployeeIDs, ',') AS E

INSERT INTO tbl_LMS_SessionEmpCourse_Xref(SessionID,CourseID,EmployeeID)
SELECT 1,C.items,E.items
FROM dbo.Split('1,3', ',') AS C CROSS JOINdbo.Split('4,5', ',') AS E

Thursday, August 30, 2012

Ajax Calls in asp.net

asp:   
function CheckGroupName() {
            var name = $('#<%= txtGroupName.ClientID %>').val();
            $.ajax({
                url: "CreateGroups.aspx/CheckGroupName",   // Current Page, Method 
                data: JSON.stringify({ name: name }), // parameter map as JSON 
                type: "POST", // data has to be POSTed 
                contentType: "application/json", // posting JSON content     
                dataType: "JSON",  // type of data is JSON (must be upper case!) 
                timeout: 10000,    // AJAX timeout 
                success: function (result) {
                    if (result == '{"d":"Yes"}') {
                      
                        alert('Entered Group Name alredy Exists for the account.Please Use a Differenet name to register.');
                        return false;
                    }
                },
                error: function (xhr, status) {
                    alert(status + " - " + xhr.responseText);
                }
            });

        }

aspx.cs:
  [WebMethod]
        public static string CheckGroupName(string name)
        {
            if (Activayt.Business.Group.IsExists(AccountID, name, _parent))
            {
              
                return "Yes";
            }
            else
            {
                return "";
            }
        }

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();
       }
    }
}

Captcha in asp.net using c#.net

mscaptcha.xml:
<?xml version="1.0"?>
<doc>
    <assembly>
        <name>MSCaptcha</name>
    </assembly>
    <members>
        <member name="M:MSCaptcha.CaptchaControl.ValidateCaptcha(System.String)">
            <summary>
            Validate the user's text against the CAPTCHA text
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaControl.HtmlColor(System.Drawing.Color)">
            <summary>
            returns HTML-ized color strings
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaControl.CssStyle">
            <summary>
            returns css "style=" tag for this control
            based on standard control visual properties
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaControl.Render(System.Web.UI.HtmlTextWriter)">
            <summary>
            render raw control HTML to the page
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaControl.GenerateNewCaptcha">
            <summary>
            generate a new captcha and store it in the ASP.NET Cache by unique GUID
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaControl.System#Web#UI#IPostBackDataHandler#LoadPostData(System.String,System.Collections.Specialized.NameValueCollection)">
            <summary>
            Retrieve the user's CAPTCHA input from the posted data
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaControl.BackColor">
            <summary>
            Background color for the captcha image
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaControl.FontColor">
            <summary>
            Color of captcha text
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaControl.NoiseColor">
            <summary>
            Color for dots in the background noise
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaControl.LineColor">
            <summary>
            Color for the background lines of the captcha image
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaControl.IsDesignMode">
            <summary>
            are we in design mode?
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.RenderImage">
            <summary>
            Forces a new Captcha image to be generated using current property value settings.
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.RandomFontFamily">
            <summary>
            Returns a random font family from the font whitelist
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.GenerateRandomText">
            <summary>
            generate random text for the CAPTCHA
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.RandomPoint(System.Int32,System.Int32,System.Int32,System.Int32)">
            <summary>
            Returns a random point within the specified x and y ranges
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.RandomPoint(System.Drawing.Rectangle)">
            <summary>
            Returns a random point within the specified rectangle
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.TextPath(System.String,System.Drawing.Font,System.Drawing.Rectangle)">
            <summary>
            Returns a GraphicsPath containing the specified string and font
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.GetFont">
            <summary>
            Returns the CAPTCHA font in an appropriate size
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.GenerateImagePrivate">
            <summary>
            Renders the CAPTCHA image
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.WarpText(System.Drawing.Drawing2D.GraphicsPath,System.Drawing.Rectangle)">
            <summary>
            Warp the provided text GraphicsPath by a variable amount
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.AddNoise(System.Drawing.Graphics,System.Drawing.Rectangle)">
            <summary>
            Add a variable level of graphic noise to the image
            </summary>
        </member>
        <member name="M:MSCaptcha.CaptchaImage.AddLine(System.Drawing.Graphics,System.Drawing.Rectangle)">
            <summary>
            Add variable level of curved lines to the image
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.UniqueId">
            <summary>
            Returns a GUID that uniquely identifies this Captcha
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.RenderedAt">
            <summary>
            Returns the date and time this image was last rendered
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.Font">
            <summary>
            Font family to use when drawing the Captcha text. If no font is provided, a random font will be chosen from the font whitelist for each character.
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.FontWarp">
            <summary>
            Amount of random warping to apply to the Captcha text.
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.BackgroundNoise">
            <summary>
            Amount of background noise to apply to the Captcha image.
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.TextChars">
            <summary>
            A string of valid characters to use in the Captcha text.
            A random character will be selected from this string for each character.
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.TextLength">
            <summary>
            Number of characters to use in the Captcha text.
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.Text">
            <summary>
            Returns the randomly generated Captcha text.
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.Width">
            <summary>
            Width of Captcha image to generate, in pixels
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.Height">
            <summary>
            Height of Captcha image to generate, in pixels
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.FontWhitelist">
            <summary>
            A semicolon-delimited list of valid fonts to use when no font is provided.
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.BackColor">
            <summary>
            Background color for the captcha image
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.FontColor">
            <summary>
            Color of captcha text
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.NoiseColor">
            <summary>
            Color for dots in the background noise
            </summary>
        </member>
        <member name="P:MSCaptcha.CaptchaImage.LineColor">
            <summary>
            Color for the background lines of the captcha image
            </summary>
        </member>
        <member name="T:MSCaptcha.CaptchaImage.FontWarpFactor">
            <summary>
            Amount of random font warping to apply to rendered text
            </summary>
        </member>
        <member name="T:MSCaptcha.CaptchaImage.BackgroundNoiseLevel">
            <summary>
            Amount of background noise to add to rendered image
            </summary>
        </member>
        <member name="T:MSCaptcha.CaptchaImage.LineNoiseLevel">
            <summary>
            Amount of curved line noise to add to rendered image
            </summary>
        </member>
    </members>
</doc>

web.config:

<system.web>
   <httpHandlers>
      <add verb="GET" path="CaptchaImage.axd" type="MSCaptcha.CaptchaImageHandler, MSCaptcha"/>
    </httpHandlers>
</system.web>


Adding Captcha dll :


  <cc1:CaptchaControl ID="Captcha1" runat="server" CaptchaBackgroundNoise="Low" CaptchaLength="5"
                                                        CaptchaHeight="60" CaptchaWidth="160" CaptchaLineNoise="None" CaptchaMinTimeout="5"
                                                        CaptchaMaxTimeout="240" FontColor="#529E00" BackColor="Silver" />