2012/03/23

Save XML by stored procedure In SQL Server, also see the use of transaction

ALTER PROCEDURE [dbo].[AdminSectionFactLinkSave]
        @RegXML AS XML
AS
BEGIN    

    BEGIN TRY
        
    BEGIN TRANSACTION    

    DECLARE @SectionFact AS INT
        

            DELETE FROM SectionFactKnowFactLink WHERE SFKF_SFID IN (
            SELECT SectionFactLink.ITEM.query('./SFKF_SFID').value('.','INT')
            FROM @RegXML.nodes('/DocumentElement/SectionFactLink') AS SectionFactLink(ITEM))


            INSERT INTO SectionFactKnowFactLink(SFKF_SFID,SFKF_KFMasterID)
            SELECT SectionFactLink.ITEM.query('./SFKF_SFID').value('.','INT'),
            SectionFactLink.ITEM.query('./SFKF_KFMasterID').value('.','INT')
            FROM @RegXML.nodes('/DocumentElement/SectionFactLink') AS SectionFactLink(ITEM)            
            WHERE SectionFactLink.ITEM.query('./SFKF_KFMasterID').value('.','INT') > 0
    
        COMMIT
        RETURN 0

    END TRY

    BEGIN CATCH
        EXEC CatchError
        IF @@TRANCOUNT > 0 ROLLBACK 
        RETURN -1
    END CATCH

END


****************************************************************************
Catch Error:


CREATE PROCEDURE [dbo].[CatchError]    
AS

BEGIN
    
    --To keep the error message
    Declare @ErrMsg as varchar(1000)
    
    BEGIN TRY  
        
        INSERT INTO dbo.Error_Log(Error_Date,Error_Msg,Error_Number,Error_Source )
        SELECT DATEADD(HOUR, 20, GETDATE()), ERROR_MESSAGE() ,ERROR_NUMBER(), ERROR_PROCEDURE()


    END TRY  
  
    BEGIN CATCH  
     
    END CATCH
    
    set @ErrMsg = ERROR_MESSAGE()
    
    Raiserror(@ErrMsg -- Error Message
        , 16 --Severity Level
        , 1 --State
        )

END

Export a Panel or Div to MS Word Docs

/// <summary>
    /// 
    /// </summary>
    /// <returns></returns>
    public string GetHtmlForPage()
    {
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        //Panel Panel1 = (Panel)this.Page.FindControl("Panel1");
        Panel1.RenderControl(htw);
        return sw.ToString();
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="webPage"></param>
    /// <param name="download"></param>
    /// <param name="FileName"></param>
    public void ServeAsDoc(System.Web.UI.Page webPage, Boolean download, String FileName)
    {
        try
        {
            string htmlString = GetHtmlForPage();

            webPage.Response.Buffer = true;
            webPage.Response.Clear();
            webPage.Response.ContentType = "application/vnd.ms-word"; //application/octet-stream
            webPage.Response.ContentEncoding = System.Text.UnicodeEncoding.UTF8;
            webPage.Response.Charset = "UTF-8";
            webPage.Response.AddHeader("Content-Disposition:", "attachment; filename=" + FileName + "");
            webPage.Response.AddHeader("cache-control", "must-revalidate");


            webPage.Response.Write("<html xmlns:v='urn:schemas-microsoft-com:vml' xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:word'  xmlns:m='http://schemas.microsoft.com/office/2004/12/omml' xmlns='http://www.w3.org/TR/REC-html40'>");
            webPage.Response.Write("<head>");
            webPage.Response.Write("<title>PF FUND</title>");
            webPage.Response.Write("<META HTTP-EQUIV='Content-Type' CONTENT='text/html; charset=UTF-8'>");
            webPage.Response.Write("<meta name=ProgId content=Word.Document>");
            webPage.Response.Write("<meta name=Generator content='Microsoft Word 9'>");
            webPage.Response.Write("<meta name=Originator content='Microsoft Word 9'>");
            webPage.Response.Write("<!--[if gte mso 9]> <xml> <w:WordDocument> <w:View>Print</w:View> <w:Zoom>100</w:Zoom> <w:DoNotOptimizeForBrowser/> </w:WordDocument> </xml> <![endif]-->");
            webPage.Response.Write("<style>");
            webPage.Response.Write("@page { size: 8.27in 11.69in; mso-page-orientation: Portrait Orientation; }");
            webPage.Response.Write("@page Section1 {margin:0.5in 0.5in 0.5in 0.5in;mso-paper-source:0;}");
            webPage.Response.Write("div.Section1 {page:Section1;}");
            webPage.Response.Write("@page Section2 {size:841.7pt 595.45pt;mso-page-orientation:landscape;margin:1.0in 1.0in 1.0in 1.0in;mso-header-margin:.5in;mso-footer-margin:.5in;mso-paper-source:0;}");
            webPage.Response.Write("div.Section2 {page:Section2;}");
            webPage.Response.Write("</style>");
            webPage.Response.Write("</head>");
            webPage.Response.Write("<body>");
            webPage.Response.Write("<div class=Section1>");
            webPage.Response.Write(htmlString);
            webPage.Response.Write("</div>");
            webPage.Response.Write("</body>");
            webPage.Response.Write("</html>");
            webPage.Response.Flush();
            webPage.Response.End();
        }
        catch
        {
            throw;
        }
    } 

Progress Bar in Web Application


Create a image folder and relace some loding icon into it downloaded from Google :

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>

    <script type="text/javascript">
        var url = 'http://logicalca.com/NZ/'; //the details page you want to display... 
    </script>

    <style type="text/css">
        </style>
</head>
<body onload="location.href = url;" style="overflow: hidden; overflow-y: hidden">
    <div style="width: 980px; height: 800px; text-align: center">
        <img id="Img1" src="~/Images/loading.gif" runat="server" alt="loading" />
    </div>
</body>

<script type="text/javascript" language="javascript">
    if (document.layers) {
        document.write('<Layer src="' + url + '" visibility="hidden"></Layer>');
    }
    else if (document.all || document.getElementById) {
        document.write('<iframe src="' + url + '" style="visibility:hidden;"></iframe>');
    }
    else {
        location.href = url;
    }   
</script>
</html>

Get Root Path of the Application


#region Get Root Path

    /// <summary>
    /// // To get Root path of the Application.
    /// </summary>
    /// <CreatedBy>Harish Kumar Chandna</CreatedBy>
    /// <CreatedDate>02/AUGUST/2011</CreatedDate>
    /// <returns></returns>
    public static string GetRootPath()
    {

        string strRootPath = string.Empty;
        if (HttpContext.Current.Request.ApplicationPath != "/")
            strRootPath = HttpContext.Current.Request.ApplicationPath + "/";
        else
            strRootPath = HttpContext.Current.Request.ApplicationPath;
        return strRootPath;
    }
    #endregion

Create Crystal Report (with sub report) In ASP.NET

Step1- Register the Crstal Report DLL (Not Need to include the DLL In Bin Folder) by using the following         lines
<%@ Register Assembly="CrystalDecisions.Web, Version=10.5.3700.0, Culture=neutral, PublicKeyToken=692fbea5521e1304"
    Namespace="CrystalDecisions.Web" TagPrefix="CR" %> 
Step2- Take the Crystal Report Viewer control onto the page Like this
<CR:CrystalReportViewer ID="Crv" runat="server" AutoDataBind="true" />
Step-3- Create Report: first of Create an XSD then bind the report with the XSD (here not need to create a new connection)
Step-4 Use these function to bind the report
private DataSet GetReportData()
    {
        try
        {
            DataSet dsReport = new DataSet();
            DataTable dtParent = new DataTable();
            dtParent.Columns.Add("ParentID");
            dtParent.Columns.Add("ParentName");
            for (int i = 1; i <= 3; i++)
            {
                dtParent.Rows.Add(i, "Parent " + i.ToString());
            }
            dsReport.Tables.Add(dtParent);

            DataTable dtChild = new DataTable();
            DataTable dtSubReport = new DataTable();
            dtChild.Columns.Add("ParentID");
            dtChild.Columns.Add("ClientID");
            dtChild.Columns.Add("ClientName");
            dtChild.Columns.Add("ClientAddress");
            dtChild.Columns.Add("DOB");
            dtSubReport.Columns.Add("ClientID");
            dtSubReport.Columns.Add("ProductDetails");
            for (int j = 1; j <= 3; j++)
            {
                for (int i = 21; i <= 23; i++)
                {
                    dtChild.Rows.Add(j, j.ToString() + i.ToString(), "ClientName " + i.ToString(), "ClientAddress " + i.ToString(), "DOB " + i.ToString());
                    dtSubReport.Rows.Add(j.ToString() + i.ToString(), "ProductDetails " + i.ToString());
                }
            }
            dsReport.Tables.Add(dtChild);
            dsReport.Tables.Add(dtSubReport);

            return dsReport;
        }
        catch 
        {
            
            throw;
        }
    }

    private void ShowReport()
    {
        try
        {
            string ReportName = string.Empty;
            this.Title = "MMNDY:RMSC Medicines List";
            DataSet dsReport = new DataSet();
            dsReport = GetReportData();

            ReportDocument rpt = new ReportDocument();
            string path = Server.MapPath("") + "\\Reports\\GroupBylReport.rpt";
            rpt.Load(path);
            rpt.Database.Tables[0].SetDataSource(dsReport);
            rpt.SetParameterValue("Title", "My Test Report");
            rpt.SetParameterValue("DesignedBy", "Harish & Asvini");
            Crv.ReportSource = rpt;
            Crv.DataBind();
        }
        catch 
        {
            
            throw;
        }
    }

This will show the report on the page.

 What is AI? Artificial Intelligence (AI) is the ability of machines or computer systems to perform tasks that normally require human intell...