Friday, October 30, 2015

search datatable using viewstate without go to database every postback and search using dataview filter option example using c#

first time you should fill viewstate with your database table.
 private void LoadData()
    {
        try
        {
            DataTable TblSections = new DataTable();

            if (ViewState["Pusers"] == null)
            {
                if (this.currentRequest.REQUEST_ID != null)
                {
                    TblSections = workflowGetData.WORK_FLOWS_PERMISSION_MANAGERIAL_GET_STAGE_USERS(this.currentRequest.WORK_FLOW_ID.ToString(), this.currentRequest.WORK_FLOW_TYPE.ToString(), this.currentRequest.CURRENT_STAGE_IDs[0].ToString(), "");
                    ViewState["Pusers"] = TblSections;
                    //else
                    //    TblSections = workflowGetData.WORK_FLOWS_PERMISSION_MANAGERIAL_GET_STAGE_USERS_BY_REQUEST_ID(hid_workflow_id.Value, hid_workflow_type.Value, hid_stage_id.Value, "", hid_request_id.Value);
                }
            }
            else
                TblSections = (DataTable)ViewState["Pusers"];

            DataView DV = new DataView();
            DV = TblSections.DefaultView;
            if (TXT_USER_ID.Text.Length > 0)
                DV.RowFilter = "USER_ID=" + TXT_USER_ID.Text;
            if (TXT_USER_FULL_NAME.Text.Length > 0)
                DV.RowFilter = String.Format("emp_name LIKE '*{0}*'", EscapeLikeValue(TXT_USER_FULL_NAME.Text));

            GV_USERS.DataSource = TblSections;
            GV_USERS.DataBind();
        }
        catch (Exception ex)
        {
            basepage.FormShowMessage(lblmessage, Enum_Class.MessageStatus.failure, Resources.GlobalErrorMessages.Error_GeneralException);
        }
    }
---------------------------------------------
  public static string EscapeLikeValue(string valueWithoutWildcards)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < valueWithoutWildcards.Length; i++)
        {
            char c = valueWithoutWildcards[i];
            if (c == '*' || c == '%' || c == '[' || c == ']')
                sb.Append("[").Append(c).Append("]");
            else if (c == '\'')
                sb.Append("''");
            else
                sb.Append(c);
        }
        return sb.ToString();
    } 

Wednesday, October 28, 2015

Json pass object to webmethod using c#

javascript function:

function Archive_insert_Save_DB(saved_Doc_Extension) {
    ///////////////////////////////////////////////////////////////////////////////////////////////////////////////
    var datajsonparam = {
        hidLibrary: $('#hidLibrary').prop('value'),
        hidFormID: $("#hidFormID").prop('value'),
        hidProfileID: "" + $("#hidProfileID").prop('value'),
        hidDocID: $("#hidDocID").prop('value'),
        hidDocName: $("#hidDocName").prop('value'),
        hidIsVersion: $("#hidIsVersion").prop('value'),
        hidDocComment: $("#hidDocComment").prop('value'),
        hidFTPGuidName: $("#hidFTPGuidName").prop('value'),
        hidUserID: $("#hidUserID").prop('value'),
        HidMRTYPE: $("#HidMRTYPE").prop('value'),
        hidDOC_DATE_EN: $("#hidDOC_DATE_EN").prop('value'),
        hidDOC_DATE_AR: $("#hidDOC_DATE_AR").prop('value'),
        saved_Doc_Extension: saved_Doc_Extension

    };
    $.ajax({
        type: "POST",
        url: "scan.aspx/Add_Document_DB_Operations",
        data: JSON.stringify({ DocumentDetails: datajsonparam }),
        //'{"hidLibrary" : "' + $("#hidFTPServer").prop('value') + '" ,"lang" : "' + lang + "hidLibrary" : "' + $("#hidFTPServer").prop('value')   }',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            $("#hidDocID").val(result.d);
            alert("تمت عملية الأرشفة بنجاح - Archive Operation Succeeded");
            window.opener.location.reload();
            window.close();
        },
        error: function (x, e) { alert(x.responseText); }
    });
}
--------------------------------------------------------------------
   [WebMethod]
        public static string  Add_Document_DB_Operations(DocumentDetail DocumentDetails)
        {
                if (DocumentDetails.hidIsVersion.Length > 0)
                {
                    //insert document in table doc_Versions
                    string doc_version_ID = archive_Class.Doc_Versions_INSERT(DocumentDetails.hidDocID, DocumentDetails.hidDocName, DocumentDetails.hidDocComment, DocumentDetails.hidFTPGuidName + DocumentDetails.saved_Doc_Extension, DocumentDetails.hidUserID);
                    //update main version to the new version inserted
                    archive_Class.Doc_Versions_Set_AS_Main_Doc(int.Parse(DocumentDetails.hidDocID), int.Parse(doc_version_ID));
                    //update main document in table profile_docs to the name of the new version file
                    archive_Class.Profile_Docs_UPDATE_Phisical_File_Name(int.Parse(DocumentDetails.hidDocID), DocumentDetails.hidFTPGuidName + DocumentDetails.saved_Doc_Extension, DocumentDetails.hidUserID);

                    try
                    {
                        archive_Class.Archive_History_Insert(int.Parse(DocumentDetails.hidLibrary), int.Parse(DocumentDetails.hidFormID), int.Parse(DocumentDetails.hidProfileID), int.Parse(DocumentDetails.hidDocID), int.Parse(doc_version_ID),
                        Convert.ToInt32(ArchiveOperations.DocVersion_Add),
                        Convert.ToInt32(DocumentDetails.hidUserID), "  تم عمل مسح ضوئي لوثيقة الإصدار رقم  :  " + doc_version_ID.ToString() + "  عنوان الوثيقة   : " + DocumentDetails.hidDocName);

                    }
                    catch (Exception ex)
                    { Elmah.ErrorSignal.FromCurrentContext().Raise(ex); }

                }
                else
                {
                    if (DocumentDetails.HidMRTYPE.Length > 0)

                        DocumentDetails.hidDocID = archive_Class.Profile_Docs_INSERT_MR(DocumentDetails.hidProfileID, DocumentDetails.hidDocName, DocumentDetails.hidDocComment, DocumentDetails.hidFTPGuidName + DocumentDetails.saved_Doc_Extension, DocumentDetails.hidUserID, DocumentDetails.HidMRTYPE, DocumentDetails.hidDOC_DATE_EN, DocumentDetails.hidDOC_DATE_AR);
                    else
                        //insert documents in db in table profile_Docs
                        DocumentDetails.hidDocID = archive_Class.Profile_Docs_INSERT(DocumentDetails.hidProfileID, DocumentDetails.hidDocName, DocumentDetails.hidDocComment, DocumentDetails.hidFTPGuidName + DocumentDetails.saved_Doc_Extension, DocumentDetails.hidUserID);

                    //insert new version for the document and make it as main doc version
                    archive_Class.Doc_Versions_INSERT(DocumentDetails.hidDocID, DocumentDetails.hidDocName, DocumentDetails.hidDocComment, DocumentDetails.hidFTPGuidName + DocumentDetails.saved_Doc_Extension, DocumentDetails.hidUserID);

                    //try
                    //{
                        archive_Class.Archive_History_Insert(int.Parse(DocumentDetails.hidLibrary), int.Parse(DocumentDetails.hidFormID), int.Parse(DocumentDetails.hidProfileID), int.Parse(DocumentDetails.hidDocID), 0,
                        Convert.ToInt32(ArchiveOperations.Document_Add),
                        Convert.ToInt32(DocumentDetails.hidUserID), "  تم عمل مسح ضوئي للوثيقة رقم  :  " + DocumentDetails.hidDocID + "  عنوان الوثيقة   : " + DocumentDetails.hidDocName);

                    //}
                    //catch (Exception ex)
                    //{ Elmah.ErrorSignal.FromCurrentContext().Raise(ex); }

                }
                // _ArchiveClass.CreateArchiveFolders(DocumentDetails.hidLibrary, DocumentDetails.hidFormID, DocumentDetails.hidProfileID, DocumentDetails.hidDocID);
                //ftp "/" + hidLibraryID.Value + "/" + hidFormID.Value + "/" + hidProfileID.Value + "/" + row.Cells[0].Text + "/" + phisicalname);

                Doc_Server doc_srv = new Doc_Server(int.Parse(DocumentDetails.hidDocID.ToString()));
            string controlSavedPath="/" + DocumentDetails.hidLibrary + "/" + DocumentDetails.hidFormID + "/" + DocumentDetails.hidProfileID + "/" + DocumentDetails.hidFTPGuidName + ".pdf";
                doc_srv.ftpRelativeFilePath = "/" + DocumentDetails.hidLibrary + "/" + DocumentDetails.hidFormID + "/" + DocumentDetails.hidProfileID + "/" + DocumentDetails.hidDocID + "/";
                FTP_Handler.CreateFTP_Directories(doc_srv);
                FTP_Handler.ftp_Move_File_afterSave(doc_srv, controlSavedPath, doc_srv.ftpRelativeFilePath, DocumentDetails.hidFTPGuidName + ".pdf");

            //}
            //catch (Exception ex)
            //{
            //    Elmah.ErrorSignal.FromCurrentContext().Raise(ex);
            //    var message = new JavaScriptSerializer().Serialize(ex.Message.ToString());
            //    string script = "alert('" + ex.Message + "');";
            //    Page.ClientScript.RegisterStartupScript(this.GetType(), "sctipKey", script, true);
             
            //}

            return DocumentDetails.hidDocID;
         
        }
     
    }
---------------------------------------------------------------------------
Class

    public class DocumentDetail
    {
        public string hidLibrary { get; set; }
        public string hidFormID { get; set; }
        public string hidProfileID { get; set; }
        public string hidDocID { get; set; }
        public string hidDocName { get; set; }
        public string hidIsVersion { get; set; }
        public string hidDocComment { get; set; }
        public string hidFTPGuidName { get; set; }
        public string hidUserID { get; set; }
        public string HidMRTYPE { get; set; }
        public string hidDOC_DATE_EN { get; set; }
        public string hidDOC_DATE_AR { get; set; }
        public string hidVersionID { get; set; }
        public string saved_Doc_Extension { get; set; }
    }
}

money conversion class using vb.net

Imports Microsoft.VisualBasic
Imports System

Public Class Conversion

    Dim Ma, Mi, N, B, R As String
    Dim result As String

    Public Function BADRMEDIA(ByVal X As Double) As String

        Ma = " ريا ل"
        Mi = " هلله"
        N = Int(X)
        B = Val(Right(Format(X, "000000000000.00"), 2))
        R = SFormatNumber(N)

        If R <> "" And B > 0 Then result = R & Ma & " و " & B & Mi
        If R <> "" And B = 0 Then result = R & Ma
        If R = "" And B <> 0 Then result = B & Mi
        BADRMEDIA = result
    End Function

    Public Function SFormatNumber(ByVal X As Double) As String
        Dim letter1 As String = String.Empty
        Dim letter2 As String = String.Empty
        Dim letter3 As String = String.Empty
        Dim letter4 As String = String.Empty
        Dim letter5 As String = String.Empty
        Dim letter6 As String = String.Empty


        Dim c As String = Format(Math.Floor(X), "000000000000")
        Dim C1 As Double = Val(Mid(c, 12, 1))
        Select Case C1
            Case Is = 1 : letter1 = "واحد"
            Case Is = 2 : letter1 = "اثنان"
            Case Is = 3 : letter1 = "ثلاثة"
            Case Is = 4 : letter1 = "اربعة"
            Case Is = 5 : letter1 = "خمسة"
            Case Is = 6 : letter1 = "ستة"
            Case Is = 7 : letter1 = "سبعة"
            Case Is = 8 : letter1 = "ثمانية"
            Case Is = 9 : letter1 = "تسعة"
        End Select


        Dim C2 As Double = Val(Mid(c, 11, 1))
        Select Case C2
            Case Is = 1 : letter2 = "عشر"
            Case Is = 2 : letter2 = "عشرون"
            Case Is = 3 : letter2 = "ثلاثون"
            Case Is = 4 : letter2 = "اربعون"
            Case Is = 5 : letter2 = "خمسون"
            Case Is = 6 : letter2 = "ستون"
            Case Is = 7 : letter2 = "سبعون"
            Case Is = 8 : letter2 = "ثمانون"
            Case Is = 9 : letter2 = "تسعون"
        End Select


        If letter1 <> "" And C2 > 1 Then letter2 = letter1 + " و" + letter2
        If letter2 = "" Or letter2 Is Nothing Then
            letter2 = letter1
        End If
        If C1 = 0 And C2 = 1 Then letter2 = letter2 + "ة"
        If C1 = 1 And C2 = 1 Then letter2 = "احدى عشر"
        If C1 = 2 And C2 = 1 Then letter2 = "اثنى عشر"
        If C1 > 2 And C2 = 1 Then letter2 = letter1 + " " + letter2
        Dim C3 As Double = Val(Mid(c, 10, 1))
        Select Case C3
            Case Is = 1 : letter3 = "مائة"
            Case Is = 2 : letter3 = "مئتان"
            Case Is > 2 : letter3 = Left(SFormatNumber(C3), Len(SFormatNumber(C3)) - 1) + "مائة"
        End Select
        If letter3 <> "" And letter2 <> "" Then letter3 = letter3 + " و" + letter2
        If letter3 = "" Then letter3 = letter2


        Dim C4 As Double = Val(Mid(c, 7, 3))
        Select Case C4
            Case Is = 1 : letter4 = "الف"
            Case Is = 2 : letter4 = "الفان"
            Case 3 To 10 : letter4 = SFormatNumber(C4) + " آلاف"
            Case Is > 10 : letter4 = SFormatNumber(C4) + " الف"
        End Select
        If letter4 <> "" And letter3 <> "" Then letter4 = letter4 + " و" + letter3
        If letter4 = "" Then letter4 = letter3
        Dim C5 As Double = Val(Mid(c, 4, 3))
        Select Case C5
            Case Is = 1 : letter5 = "مليون"
            Case Is = 2 : letter5 = "مليونان"
            Case 3 To 10 : letter5 = SFormatNumber(C5) + " ملايين"
            Case Is > 10 : letter5 = SFormatNumber(C5) + " مليون"
        End Select
        If letter5 <> "" And letter4 <> "" Then letter5 = letter5 + " و" + letter4
        If letter5 = "" Then letter5 = letter4


        Dim C6 As Double = Val(Mid(c, 1, 3))
        Select Case C6
            Case Is = 1 : letter6 = "مليار"
            Case Is = 2 : letter6 = "ملياران"
            Case Is > 2 : letter6 = SFormatNumber(C6) + " مليار"
        End Select
        If letter6 <> "" And letter5 <> "" Then letter6 = letter6 + " و" + letter5
        If letter6 = "" Then letter6 = letter5
        SFormatNumber = letter6


    End Function
End Class

Saturday, October 3, 2015

sql server recursion recursive function example to get complex name string from parent and child in table hikel

USE [AccountingSqlTest]
GO
/****** Object:  UserDefinedFunction [dbo].[HIKEL_GET_NAME_BY_HIKEL_ID]    Script Date: 10/03/2015 22:37:38 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE  FUNCTION [dbo].[HIKEL_GET_NAME_BY_HIKEL_ID]
(
@HIKEL_ID INT
)
RETURNS NVARCHAR(1000)
AS
BEGIN
   DECLARE @HIKEL_NAME AS NVARCHAR(1000);
   --GET ALL ROWS RELATED PASSED HIKEL ID ACCORDING TO @HIKEL_ID PASSED
  WITH NAME_TREE AS (
   SELECT HIKEL_ID ,PARENT_ID, HIKEL_NAME,HIKEL_NAME_EN
   FROM HIKEL
   WHERE HIKEL_ID = @HIKEL_ID -- THIS IS THE STARTING POINT YOU WANT IN YOUR RECURSION SMALL HIKEL
   UNION ALL
   SELECT  C.HIKEL_ID ,C.PARENT_ID,C.HIKEL_NAME ,C.HIKEL_NAME_EN
   FROM HIKEL C
     JOIN NAME_TREE P ON P.PARENT_ID = C.HIKEL_ID  -- THIS IS THE RECURSION
)
SELECT DISTINCT @HIKEL_NAME= SUBSTRING(
(SELECT ','+ T.HIKEL_NAME AS [text()]

FROM NAME_TREE T
WHERE T.PARENT_ID IS NOT NULL
ORDER BY T.PARENT_ID ASC

FOR XML PATH ('')
), 2, 1000)


RETURN @HIKEL_NAME
END
----------------------------------------------------------------------------------------------------------------

create  FUNCTION [dbo].[HikelGetChilds]
(
@id INT
)
RETURNS table
AS
return (
WITH ret AS(
        SELECT  id ,ParentId, name ,MnagerId
        FROM    DEPARTMENTS
        WHERE   ID = @id
        UNION ALL
        SELECT  d.id ,d.ParentId,d. name ,d.MnagerId
        FROM    DEPARTMENTS d  INNER JOIN
                ret r ON d.ParentId = r.ID
)

SELECT  *
FROM    ret
)

-------------------------------------------------------------------------------------------------------------------------
ALTER  FUNCTION [dbo].[HikelGetParents]
(
@id INT
)
RETURNS table
AS
return (
   --GET ALL ROWS RELATED PASSED HIKEL ID ACCORDING TO @id PASSED
  WITH NAME_TREE AS (
   SELECT id ,ParentId, name ,MnagerId
   FROM DEPARTMENTS
   WHERE id = @id -- THIS IS THE STARTING POINT YOU WANT IN YOUR RECURSION SMALL HIKEL

   UNION ALL
   SELECT  C.id ,C.parentid,C.name  ,c.MnagerId
   FROM DEPARTMENTS C
     JOIN NAME_TREE P ON P.parentid = C.id  -- THIS IS THE RECURSION
)
SELECT DISTINCT *
 
   FROM NAME_TREE T
   WHERE T.parentid IS NOT NULL
   )
   
---------------------------------------------------------------------------------------------------------------------