Monday, March 31, 2014

vb.net generate Random password function

 Public Shared Function GenerateRandomPassword(size As Integer) As String
        'Const Alphabet As String = "abcdefghijklmnopqrstuvwyxzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
        Const Alphabet As String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        Const numberical As String = "1234567890565230"
        Dim rand As New Random()
        Dim chars As Char() = New Char(size - 1) {}
        For i As Integer = 0 To size - 1
            If (i <= 4) Then
                chars(i) = numberical(rand.Next(numberical.Length))
            Else
                chars(i) = Alphabet(rand.Next(Alphabet.Length))
            End If
        Next
        Return New String(chars)
    End Function
function call
GenerateRandomPassword(8)
will generate password with 8 characters( the first 5 characters are numeric others will be capital letters)




Sunday, March 23, 2014

sql server execute stored procedure with return value

To Execute stored procedure that return value and get result you should do th following
1- declare variable with Type match return value .
2- write Execute command EXEC
3- set variable declared = function call with parmeter.
example

declare @valback int;
exec @valback =VACATION_REPORTS_REQUEST_GET_INBOX_REQUESTS_COUNT 29 , 3;
SELECT  @valback;
------------------------------------------------------------------------------------------------
to get return value using stored procedure
you can use this template to get return value

 public string GetSingelvalue_SQLReturnValue(string Stored, params string[] p)
    {
        try
        {
            cmd = new SqlCommand(Stored, cn);
            cmd.CommandType = CommandType.StoredProcedure;
            for (int i = 0; i < p.Length; i = i + 2)
            {
                cmd.Parameters.Add(new SqlParameter(p[i], p[i + 1]));
            }
            var returnParameter = cmd.Parameters.Add("@ReturnVal", SqlDbType.Int);
            returnParameter.Direction = ParameterDirection.ReturnValue;
            if (cn.State == ConnectionState.Closed)
                cn.Open();
            cmd.ExecuteNonQuery();
            if (cn.State == ConnectionState.Open)
                cn.Close();
            var result = returnParameter.Value;
            return result.ToString();
        }
        catch (Exception ex)
        {
            return ex.Message;
        }
        finally
        {
            if (cn.State == ConnectionState.Open)
                cn.Close();

        }
    }

Saturday, March 22, 2014

SQL SERVER Stored Procedure Check parameter Value exists on query result and return value according to existence

i have stored procedure in sql server take parameter user_id
i need to check permission for user_id for specific query concept
i need to pass user_id parameter and check if this user exists on query result or not
if it is exists the stored will return 1 else the stored will return 0
so if result = 1 meaning that the user has permission i want else i will prevent him to complete action in my code side.

the stored Procedure Sample Code
USE [HES_Intranet]
GO
/****** Object:  StoredProcedure [dbo].[VACATION_REPORTS_REQUEST_GET_SENT_COUNT]    Script Date: 03/23/2014 01:49:24 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROC [dbo].[VACATION_REPORTS_REQUEST_GET_USER_STAGE_PERMISSION] 
 @USER_ID INT ,
 @POSITION_ID INT ,
 @STAGE_ID INT , 
 @WORK_FLOW_ID INT
AS 
BEGIN
SET NOCOUNT ON 
SET XACT_ABORT ON  
/*THE IF STATEMENT CHECK IF 1 IS SELECTED THIS MEANING THAT THE 
USER_ID PASSED EXISTS ON SELECT PERMISSION QUERY
ELSE MEANING THAT THAT USER POSITION NOT ALLOWED TO LOGIN AND ALSO 
NO PERMISSION FOR WORKFLOW GAVEN TO HIM BY SYSTEM ADMIN */

  IF(  
(SELECT 1
     WHERE @USER_ID IN ( select USER_ID from USERS U  where U.POSITION_ID=@POSITION_ID
union
select USER_ID from USER_ROLES UR 
where ur.WORK_FLOW_ID=@WORK_FLOW_ID
AND UR.REQUEST_STAGE_ID=@STAGE_ID)) =1)
RETURN 1
ELSE
RETURN 0;
END

the Stored Procedure Call Test  in sql server Side
declare @valback int;
exec @valback = VACATION_REPORTS_REQUEST_GET_USER_STAGE_PERMISSION 10,1,1;
SELECT  @valback; 

Wednesday, March 19, 2014

enum description in arabic and how to bind enum value to integer , get arabic description to enum in c#

the main purpose for this example
1- how to create Enum in separate Class
2- how to get enum integer value to bind it to function .
3- how to get enum name and enum arabic description and bind it to gridview function call.

first create class called Enum_Class to prepare enum sample as follow
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Reflection;
/// <summary>
/// Summary description for Enum_Class
/// </summary>
public class Enum_Class
{
    #region Enum For Medical Reports
       public enum VACATION_REPORTS_REQUEST_STATUS_Enum : int
    {
        
        [Description("&quot;التحضير&quot;")]
        Preparation = 1,
        /// <summary>
        ///
        /// </summary>
         [Description("مكتمل")]
        Complete = 2,
         [Description("مقبول")]
        Acceptable = 3,
          [Description("غير مكتمل-مرفوض")]
        Incomplete_OR_Rejected = 4,
       
         [Description("موافقة نهائية")]
        Final_approved = 5,
     
         [Description("أرشفةوإغلاق")]
         Archiving_And_Close = 6
    }

   
    #endregion

    #region Methods
    public static string GetDescription(object enumValue, string defDesc)
    {
        FieldInfo fi = enumValue.GetType().GetField(enumValue.ToString());

        if (null != fi)
        {
            object[] attrs = fi.GetCustomAttributes(typeof(DescriptionAttribute), true);
            if (attrs != null && attrs.Length > 0)
                return ((DescriptionAttribute)attrs[0]).Description;
        }

        return defDesc;
    }
    #endregion
}
--------------------------------------------------
second Step
how to parse Enum value to integer value
int X =  (int)Enum_Class.VACATION_REPORTS_REQUEST_STATUS_Enum.Preparation;
-----------------------------------------------------
third
how to get Enum string using it's value , and get Description arabic text
and bind value to gridview
 <asp:TemplateField HeaderText="الحالة ">
                                    <ItemTemplate>
                                        <asp:Label ID="LBL_Status" runat="server" Text='<%#VACATION_REPORTS_REQUEST_GET_STATUS(Eval("REQUEST_STATUS_ID")) %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>


  public string VACATION_REPORTS_REQUEST_GET_STATUS(object STATUS_ID)
    {
        string result = string.Empty;
        Enum_Class.VACATION_REPORTS_REQUEST_STATUS_Enum enumStatus = ((Enum_Class.VACATION_REPORTS_REQUEST_STATUS_Enum)int.Parse(STATUS_ID.ToString()));
        result = Enum_Class.GetDescription(enumStatus, result);
         return result;
    }

 
 

Wednesday, March 5, 2014

jquery set input text value property example

   $("#txtLong").prop('value' , result[result.length - 2].toString());

                    $("#txtLat").prop('value',result[result.length - 1].toString());