How to show/hide progress bar or loader or spinner while download excel file on button click in asp.net

Hello Everyone, while working in I got a requirement where I have to show the loader on button click while downloading the excel file. Once file downloaded Loader also should stop. So I am sharing that way how we can show achieve the same.

Step-1 Open the visual studio go to File> New>Project and click on project

Step-2 Select Asp.Net Web application project like below


Step-3 Add the 2 form in your project one is Download.aspx and other you can say test2.aspx

 Step-4 Copy and paste below code on test2.aspx page.

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
 <title>Loader</title>
 <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js"></script>

    <style type="text/css">
        .modal {
            position: fixed;
            top: 0;
            left: 0;
            background-color: black;
            z-index: 99;
            opacity: 0.8;
            filter: alpha(opacity=80);
            -moz-opacity: 0.8;
            min-height: 100%;
            width: 100%;
        }

        .loading {
            font-family: Arial;
            font-size: 10pt;
            border: 5px solid #67CFF5;
            width: 200px;
            height: 100px;
            display: none;
            position: fixed;
            background-color: White;
            z-index: 999;
        }
               
    </style>
    <script type="text/javascript">

        function ShowProgress() {
            setTimeout(function () {
                var modal = $('<div />');
                modal.addClass("modal");
                $('body').append(modal);
                var loading = $(".loading");
                loading.show();
                var top = Math.max($(window).height() / 2 - loading[0].offsetHeight / 2, 0);
                var left = Math.max($(window).width() / 2 - loading[0].offsetWidth / 2, 0);
                loading.css({ top: top, left: left });
            }, 200);
        }
    </script>

</head>
<body>    
    <form id="form1" runat="server">
        <table>
            <tr>
                <td> <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" OnClientClick="ShowProgress();" Width="82px" />  </td>
               
            </tr>
            <tr>
                 <td>
                     <iframe id="iframe1" src="" runat="server" style="visibility:hidden" ></iframe>
                </td>
            </tr>
        </table>
         <div class="loading" align="center">
            Loading. Please wait.<br />
            <br />
            <img src="loader.gif" alt="" />
             </div>
    </form>
    
</body>
</html>











Step-5 Right click on test2.aspx page and click on view code and paste below code 

using System;

namespace TestSpinner
{
    public partial class test2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            System.Threading.Thread.Sleep(5000);
            iframe1.Attributes.Add("src", "Download.aspx");
        }
       
        
    }

}

Step-6 Now open Download.aspx page. copy  and paste below code.

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Download.aspx.cs" Inherits="TestSpinner.Download" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <script type="text/javascript">
        $(function () {
            var loading = $(".loading");
            loading.hide();
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>

Step-7 Right click on Download.aspx page and click on view code and paste below code 
using System;
using System.Data;

namespace TestSpinner
{
    public partial class Download : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            GenerateExcelFile();
        }
        private void GenerateExcelFile()
        {
            Response.ClearContent();
            Response.Buffer = true;
            Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", "Customers.xls"));
            Response.ContentType = "application/ms-excel";
            DataTable dt = BindDatatable();
            string str = string.Empty;
            foreach (DataColumn dtcol in dt.Columns)
            {
                Response.Write(str + dtcol.ColumnName);
                str = "\t";
            }
            Response.Write("\n");
            foreach (DataRow dr in dt.Rows)
            {
                str = "";
                for (int j = 0; j < dt.Columns.Count; j++)
                {
                    Response.Write(str + Convert.ToString(dr[j]));
                    str = "\t";
                }
                Response.Write("\n");
            }
            Response.End();

        }
        protected DataTable BindDatatable()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("UserId", typeof(Int32));
            dt.Columns.Add("UserName", typeof(string));
            dt.Columns.Add("Education", typeof(string));
            dt.Columns.Add("Location", typeof(string));
            dt.Rows.Add(1, "Rahul", "M.Tech", "Delhi");
            dt.Rows.Add(2, "Haresh", "BBA", "USA");
            dt.Rows.Add(3, "Mark", "LLB", "USA");
            dt.Rows.Add(4, "John", "B.Sc", "USA");
            dt.Rows.Add(5, "Kim", "Graduate", "USA");
            dt.Rows.Add(6, "DC", "MCA", "Moradabad");
            return dt;
        }
    }
}

Add below image in project or any other image you can use just you have to change the name in test2.aspx page there we have below code
  <img src="loader.gif" alt="" />







Build the project and run it .




How many type of function in javascript

Today, Will discuss How many type of functions is available in javascript.
So There are 3 type of functions is available in javascript
  • Named Function
  • Anonymous Function
  • Immediately Invoked function expression.


A function which has a name at the time of definition called Named function 
Syntax:

                  function function_Name(param1, param2){

                    function body

                 }

 function_Name();


Such function which don't have name are called Anonymous Function These functions are declared dynamically at runtime using the function operator instead of the function declaration. The function operator is more flexible than a function declaration and to invoked these function we assigned it to a variable.

Syntax:

            var fun = function(param) {

            do something here (Body of the function)

            }

    fun(param);

Let's understand with an example.

<script type ="text/javascript">

        var anonymousFun = function(param1, param2) {

           alert(param1 + " Good morning " + param2);

            }

        anonymousFun("Hello", "anonymous function");

    </script>

Add the above code to a web page an run the application then output would be :

Hello Good morning anonymous function






Invoked function expression are those function which runs as soon as the browser encounters it. The main benefit of function is that it runs immediately where it’s located in the code and produces a direct output. That means it is unaffected by code which appears further down in the script which can be useful.

<script type ="text/javascript">

        var msg = (function(){

            var name= "Dal Chand";

            return name;

        })();

    </script>

Outpou: Dal Chand









Explain the named function in javascript with example

 We are going to learn here about named function in javascript. Let's understand first what is named function.

Named Function: A function which is define with a name at the time of definition is called named function

Syntax: 

            function function_Name(param1, param2){

                    function body

               }

        function_Name();

Example: function Hi(name){

        alert("Hello "+name);

}

Hi("Dev");

Output: Hello Dev



How to install Sql server

We'll learn here  how to install sql server in system. Please follow below step to install sql server 2008 in your system.



























Let me know if anyone is facing any issue while installing sql server.

Arithmetic overflow error converting numeric to data type numeric

 Arithmetic OverFlow:  It occur when  we try to insert a value in a column or assign a value to a variable  which is more than the limit of data type.

Let' say we have a variable of numeric type with size

            Declare @IsArithmeticOverFlow numeric(5,2)

            SET @IsArithmeticOverFlow  = 999.999

            SELECT @IsArithmeticOverFlow 

will get the airthmetic error  error here while executing the above statement because we defined the size of variable (5,2) and IsArithmeticOverFlow  vaiable have the capacity to hold the value upto only 999, not more than it. but the value 999.999 is rounding to nearest vale. so, after rounding, the nearest vale is 1000. and the variable don't have the capacity now to hold 1000. so in this situation, will get arithmetic overflow error


Now let's fix this issue. I am just increasing the size of variable from (5,2) to (6,2)

Declare @IsArithmeticOverFlow numeric(6,2)

            SET @IsArithmeticOverFlow  = 999.999

            SELECT @IsArithmeticOverFlow 

Let's run it and see the output.
















Now you can see the above screenshot, no error after increasing the size of variable.

Let say if someone is using more than one nested procedure inside a procedure and getting arithmetic overflow issue then use the try catch to identify the eject place where it is occurring.
We'll discuss in details about try catch in our next post.



What is Javascript

 JavaScript: JavaScript is an object-oriented programming language It was introduced in 1995 for adding programs to the web pages in the Netscape Navigator browser. Since then, it has been adopted by all other graphical web browsers. it is lightweight,cross-platform ,interpreted and full-fledged programming language. it is used by lot's of websites for scripting the web pages.

Using JavaScript, developers can develop modern web applications to interact directly without reloading the page every time. it also improve the performance of the application because mostly validation part done only at client side.

Benefit of JavaScript: There are lot's of benefit of  javascript

1-JavaScript is lightweight,cross-platform and interpreted  programming language

2-Validation can be done on client side due to that it's avoid the unnecessary round trip between client side and server side.

3-it also provide the facility to update the  partially page rendering without reloading the entire web page.

How to truncate data from table in sql server

We'll learn here How to truncate data from database table.
So I have already created a employee table in test database  and inserted 5 records in Employee table
Let's select data from table with the help of below query.



























So we have the 5 records in table. To truncate the data from the table we have the TRUNCATE command  let's truncate this data from the table with the help of below query.

Syntax: TRUNCATE TABLE <TABLENAME>

Execute below command to truncate the data
TRUNCATE TABLE  Employee

output:
















Let's  check whether data is truncated or not from the table with the help of Select statement.


















So using the TRUNCATE command we can delete the entire data from the table. it does not allow   WHERE Clause.