Friday, 8 June 2012

How to import data from data grid view to excel file.

How to import data from data grid view to excel file.
:- User the following code...




---------------------------------

    public static void Export(string fileName, GridView gv)
    {
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.AddHeader("content-disposition", string.Format("attachment; filename={0}", fileName));
        HttpContext.Current.Response.ContentType = "application/ms-excel";

        using (StringWriter sw = new StringWriter())
        {
            using (HtmlTextWriter htw = new HtmlTextWriter(sw))
            {
                // Create a form to contain the grid
                Table table = new Table();

                // add the header row to the table
                if (gv.HeaderRow != null)
                {
                    PrepareControlForExport(gv.HeaderRow);
                    table.Rows.Add(gv.HeaderRow);
                }

                // add each of the data rows to the table
                foreach (GridViewRow row in gv.Rows)
                {
                    PrepareControlForExport(row);
                    table.Rows.Add(row);
                }

                // add the footer row to the table
                if (gv.FooterRow != null)
                {
                    PrepareControlForExport(gv.FooterRow);
                    table.Rows.Add(gv.FooterRow);
                }

                // render the table into the htmlwriter
                table.RenderControl(htw);
                //HttpContext.Current.Response.Write(style);
                HttpContext.Current.Response.Write(sw.ToString());
                HttpContext.Current.Response.End();
            }
        }
    }

---------------------------------
    private static void PrepareControlForExport(Control control)
    {
        for (int i = 0; i < control.Controls.Count; i++)
        {
            Control current = control.Controls[i];
            if (current is Label)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as Label).Text));
            }
            if (current is LinkButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as LinkButton).Text));
            }
            else if (current is ImageButton)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as ImageButton).AlternateText));
            }
            else if (current is HyperLink)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as HyperLink).Text));
            }
            else if (current is DropDownList)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as DropDownList).SelectedItem.Text));
            }
            else if (current is CheckBox)
            {
                control.Controls.Remove(current);
                control.Controls.AddAt(i, new LiteralControl((current as CheckBox).Checked ? "True" : "False"));
            }

            if (current.HasControls())
            {
                PrepareControlForExport(current);
            }
        }
    }

-----------------------------


On button click call following  method....



 protected void Btn_Deatil_Excel_Click(object sender, EventArgs e)
    {

            Export("Employee_Grade.xls", this.GridView2);
    }





Message box in Asp.Net application.


hi friends,
here is final code to show msg box in asp.net app.......


my mail id ..

shivram.popalghat@gmail.com

--------------------------------------------------------------------------------------------------------
Step 1).
create a code file, contains following code...
Name it as :- <Modal.cs>
Keep this file in App_Code folder.

using System.Text;
using System.Web.UI;

/// <summary>
/// Summary description for Modal
/// </summary>
public class Modal
{

    public static void Close(Page page)
    {
        Close(page, null);
    }

    public static void Close(Page page, object result)
    {
        page.Response.ClearContent();

        StringBuilder sb = new StringBuilder();
        sb.Append("<html>");
        sb.Append("<head>");
        sb.Append("<script type='text/javascript'>");
        sb.Append("if (parent && parent.DayPilot && parent.DayPilot.ModalStatic) {");
        sb.Append("parent.DayPilot.ModalStatic.result = " +
                  DayPilot.Web.Ui.Json.SimpleJsonSerializer.Serialize(result) + ";");
        sb.Append("if (parent.DayPilot.ModalStatic.hide) parent.DayPilot.ModalStatic.hide();");
        sb.Append("}");
        sb.Append("</script>");
        sb.Append("</head>");
        sb.Append("</html>");

        string output = sb.ToString();

        byte[] s = Encoding.UTF8.GetBytes(output);
        page.Response.AddHeader("Content-Length", s.Length.ToString());

        page.Response.Write(output);

        page.Response.Flush();
        page.Response.Close();

    }

}


-------------------------------------------------------------------------------------------------------

Step 2) Create a  script  file, contains following code...
Name it as :- <modal.js>
Keep this file in script folder
--------

if (typeof(DayPilot) === 'undefined') {
    DayPilot = {};
}

DayPilot.ModalStatic = {};

DayPilot.Modal = function() {

    // default values
    this.width = 300;
    this.height = 460;
    this.top = 20;
    this.opacity = 30;
    this.border = "1px solid black";

    // internal
    var This = this;

    this.id = '_' + new Date().getTime() + 'n' + (Math.random()*10); 
    this.closed = null;

    this.showHtml = function(html) {

   if (!this.div) {
   this.create();
   }
   else {
   this.div.style.display = '';
   }

   var delayed = function(p, innerHTML) {
       return function() {
           p.setInnerHTML(p.id + "iframe", innerHTML);
            }
        };
            
   window.setTimeout(delayed(this, html), 0);

    };

    this.showUrl = function(url) {

   if (!this.div) {
   this.create();
   }
   else {
   this.div.style.display = '';
   this.hideDiv.style.display = '';
   }
   DayPilot.ModalStatic = this;
   
   this.iframe.src = url;

    };

    this.create = function() {
        var scrollY = window.pageYOffset ? window.pageYOffset : ((document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop);
        var scrollX = window.pageXOffset ? window.pageXOffset : ((document.documentElement && document.documentElement.scrollLeft) ? document.documentElement.scrollLeft : document.body.scrollLeft);
    
        var height = function () {
            var D = document;
            return Math.max(
                Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
                Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
                Math.max(D.body.clientHeight, D.documentElement.clientHeight)
            );
        };
    
        var width = function () {
            var D = document;
            return Math.max(
                Math.max(D.body.scrollWidth, D.documentElement.scrollWidth),
                Math.max(D.body.offsetWidth, D.documentElement.offsetWidth),
                Math.max(D.body.clientWidth, D.documentElement.clientWidth)
            );
        };
    
   var hide = document.createElement("div");
   hide.id = this.id + "hide";
   hide.style.position = 'absolute';
   hide.style.left = "0px";
   hide.style.top = "0px";
   hide.style.width = "100%";
   hide.style.height = height() + "px";
   hide.style.filter = "alpha(opacity=" + this.opacity + ")";
   hide.style.opacity = "0." + this.opacity;
   hide.style.backgroundColor = "black";
   hide.onclick = function() { This.hide(); };
   hide.oncontextmenu = function() { return false; };

   document.body.style.height = '100%';

   document.body.appendChild(hide);

   var iframe = document.createElement("iframe");
   iframe.id = this.id + "iframe";
   iframe.name = this.id + "iframe";
   iframe.frameBorder = '0';
   iframe.style.width = '100%';
   iframe.style.height = (this.height) + 'px';

   var div = document.createElement("div");
   div.id = this.id + 'popup';
   div.style.border = this.border;
   div.style.position = 'absolute';
   div.style.left = '50%';
   div.style.marginLeft = '-' + Math.floor(this.width/2) + "px";  // '-45%'
   div.style.top = (scrollY + this.top) + 'px';
   div.style.width = this.width + 'px';  // '90%'
   div.style.height = this.height + 'px';
   div.style.backgroundColor = 'white';

   div.appendChild(iframe);

   document.body.appendChild(div);

   this.div = div;
   this.iframe = iframe;
   this.hideDiv = hide;
    };

    this.setInnerHTML = function(id, innerHTML) {
   var frame = window.frames[id];

   var doc = frame.contentWindow || frame.document || frame.contentDocument;
   //alert(id + ' ' + frame);
   if (doc.document) {
   doc = doc.document;
   }

   doc.body.innerHTML = innerHTML;
    };

    this.hide = function() {
   if (this.div) {
   this.div.style.display = 'none';
   this.hideDiv.style.display = 'none';
   }
   if (this.closed) {
       this.closed();
   }
    };

};
-----------------------------------------------------------------------------------------------------------
Step 3) Call msg box in your application as follow....

       Modal.Close(this, "OK");









Saturday, 28 April 2012

Microsoft Dynammics NAV Code

Get all code presend in standered Microsoft Dynammics NAV...!
Waiting for comments...!



Dynamic Businesses NAV

Dynamic Business: From Aspiration to Reality
Dynamic Businesses are connected, forward-looking organizations that thrive by empowering their people to reach their full potential.
Microsoft is redefining how business solutions empower people for greater success, predict potential issues and opportunities, and enable organizations to expand the possibilities for competitive advantage.


Get more details from Dynamic Businesses ...!



ABAP/4®

Learn ABAP/4® in 21 Days yourself .


You can get more in detail from here...!

Plsease comment your thought...
Or mail on shivram.popalghat@gmail.com...
Welcome suggetions...

Friday, 4 November 2011

Message Box ASP.Net Application

In ASP.Net if we want to show messagebox , then it is not possible direct using any tool.
We can get this by creating an user control .
In user control make a method , which take an argument and show that in any lebel or whatever you want.
On .aspx page use Modelpopup Extender and keep you user control in a panel.
                                                          
 

If it is really healpful  to you then , please mail me on shivram.popalghat@gmail.com.