Skip to main content

create html format of data extracted from database in asp.net

Suppose we have a need to print 500 pages a time without using report and every page is on individual page then what we will do.Below is the solution.

This thing is very needed now a days as to avoid using reports in asp.net.Reports are used to display data in the output page with proper format and alignment but they are heavy and some extra features are to be included.We cal also generate output in proper format by just using HTML tags.For this we use Jquery (very simple and easy to use).

Below is the code :

First import namespace using System.Web.Services;

Then Create Web method on cs page (code behind model) to get data from database and format it in List.

[WebMethod]
    public static List<getdata> getlccodedetail(string prefixText, string studymode, string state)
    {
        try
        {
            List<getdata> li = new List<getdata>();

            if (con.State == ConnectionState.Closed)
                con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "pODLenvelopeformat";
            cmd.Connection = con;
            cmd.CommandType = CommandType.StoredProcedure;
            if (prefixText != "")
            {
                string check = prefixText;
                if (check[check.Length - 1]==',')
                {
                    check = check.Substring(0, check.Length - 1);
                }
                check = prefixText.Replace(",", "','");
                cmd.Parameters.AddWithValue("@lccode", check);
            }
            if (studymode != "0")
            {
                cmd.Parameters.AddWithValue("@studymode", studymode);
            }
            if (state != "0")
            {
                cmd.Parameters.AddWithValue("@state", state);
            }
            SqlDataAdapter adp = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adp.Fill(ds);
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                getdata bkl = new getdata();
                bkl.lccode = dr["lccode"].ToString();
                if (dr["headname"].ToString() == "")
                {
                    bkl.LCHeadName = "---";
                }
                else
                {
                    bkl.LCHeadName = dr["headname"].ToString();
                }
                bkl.adress = dr["adress"].ToString();
                bkl.state = dr["state"].ToString();
                bkl.contact = dr["contact"].ToString();
                li.Add(bkl);
            }
            return li;
        }
        catch (Exception ex)
        {
            return null;
        }
    }




 public class getdata
    {
        private string _lccode;
        private string _LCHeadName;
        private string _Address;
        private string _State;
        private string _Contact;

        public string lccode
        {
            get
            {
                return _lccode;
            }
            set
            {
                _lccode = value;
            }
        }
        public string LCHeadName
        {
            get
            {
                return _LCHeadName;
            }
            set
            {
                _LCHeadName = value;
            }
        }
        public string adress
        {
            get
            {
                return _Address;
            }
            set
            {
                _Address = value;
            }

        }
        public string state
        {
            get
            {
                return _State;
            }
            set
            {
                _State = value;

            }
        }
        public string contact
        {
            get
            {
                return _Contact;
            }
            set
            {
                _Contact = value;
            }
        }
    }




Above Code Will bring data from database in List Form..Now we will format it accordingly in ASPX page to display as much data we need by passing arguments and fetch data

   function GetStudentDetail() {

            var lccode = $("#<%=lccode.ClientID %>").val();
            var studymode = $("#<%=DropDownList3.ClientID %>").val();
            var state = $("#<%=DropDownList6.ClientID %>").val();
            if (lccode == "" && studymode == "0" && state == "0")
            { alert("Please select at least one option to Search !!"); }
            else if ($("#<%=ddlSize.ClientID %>").val() == 'Please Select') {
                alert("Please Select Size To Print !!");
            }
            else if ($("#<%=ddltype.ClientID %>").val() == 'Please Select') {
                alert("Please Select Post Type To Print !!");
            }
            else {
                $("#<%=loadingdiv.ClientID %>").css('display', 'block');
                $("#spanMsg").css('display', 'block');
                spanMsg
                $.ajax({
                    type: "POST",
                    url: "frmenvelopeformat.aspx/getlccodedetail",
                    contentType: "application/json; charset=utf-8",
                    data: "{'prefixText':'" + lccode + "','studymode':'" + studymode + "','state':'" + state + "'}",
                    dataType: "json",
                    success: function (response) {
                        $("#<%=loadingdiv.ClientID %>").css('display', 'none');
                        $("#spanMsg").css('display', 'none');

                        var html = "";
                        var posttype = $("#<%=ddltype.ClientID %>").val();
                        var teams = response;

                        for (var i = 0; i < teams.length; i++) {
                            html += "<div style='height:auto' target='_blank' id='ViewRecord" + i + "'>"
                            html += "<table id='Table1' width='100%' style='height: 100%; font-weight: bold; text-align: left;'>"
                            html += "<tbody>"
                            html += "<tr style='text-align:center'>"
                            html += "<td>Post Type "
                            html += "<span>" + posttype + "</span>"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px;'>TO,"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: top;'>"
                            html += "<span> Service Provider Code: " + teams[i].lccode + "</span>"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: top;'>"
                            html += "<span>" + teams[i].LCHeadName + "</span>"
                            html += "</td>"
                            html += "</tr>"


                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: top;'>"
                            html += "<span>" + teams[i].adress + "</span>"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: top;'>"
                            html += "<span>" + teams[i].state + "</span>"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: top;'>"
                            html += "<span> Phone: " + teams[i].contact + "</span>"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: bottom; height: 2px;'>"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: bottom;'> From: - Directorate Of Distance Education"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: bottom;'>  Lovely Professional University"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: bottom;'>  Jalandhar-Delhi G.T. Road (NH-1),"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: bottom;'>Phagwara, Punjab (INDIA) - 144402"
                            html += "</td>"
                            html += "</tr>"

                            html += "<tr>"
                            html += "<td style='padding-left: 20px; vertical-align: bottom; height: 2px;'>"
                            html += "</td>"
                            html += "</tr>"
                            html += "</tbody>"
                            html += "</table>"

                            html += "</tbody>"
                            html += "</table>"
                            html += "</div>"
                            html += "<span Style='page-break-before: always;'> </span>"
                            $("#Content").html(html);


                        }
                        if (teams.length > 0) {
                            printContent("Content");
                        }
                        else {
                            alert("Sorry !! No Data Found");
                        }
                    }
                });

            }

            return false;
        }



PrintContent is the function to print the whole format :

function printContent(id) {
            var height = '<%=HiddenField2.ClientID %>';
            var width = '<%=HiddenField1.ClientID %>';
            var font = '<%=HiddenField3.ClientID %>';


            if ($("#<%=ddlSize.ClientID %>").val() == '9*4') {
                height = '260';
                width = '675';
                font = '12';
            }
            else if ($("#<%=ddlSize.ClientID %>").val() == '10*4.5') {
                height = '293';
                width = '750';
                font = '17';
            }
            else if ($("#<%=ddlSize.ClientID %>").val() == '10*12') {
                height = '715';
                width = '1275';
                font = '29';
            }
            else if ($("#<%=ddlSize.ClientID %>").val() == '10*14') {
                height = '780';
                width = '1275';
                font = '31';
            }
            else if ($("#<%=ddlSize.ClientID %>").val() == '12*16') {
                height = '975';
                width = '1190';
                font = '34';
            }

            str = document.getElementById(id).innerHTML
            newwin = window.open('', 'printwin')
            newwin.document.write('<HTML>\n<HEAD>\n')
            newwin.document.write('<TITLE>Print CA</TITLE>\n')
            newwin.document.write('<style type="text/css">\n')
            newwin.document.write('*{font-size:' + font + 'px;}\n')
            newwin.document.write('html { width: ' + width + '; height: ' + height + '; }\n')
            newwin.document.write('<\/style>\n')
            newwin.document.write('<script>\n')
            newwin.document.write('function chkstate(){\n')
            newwin.document.write('if(document.readyState=="complete"){\n')
            newwin.document.write('window.close()\n')
            newwin.document.write('}\n')
            newwin.document.write('else{\n')
            newwin.document.write('setTimeout("chkstate()",2000)\n')
            newwin.document.write('}\n')
            newwin.document.write('}\n')
            newwin.document.write('function print_win(){\n')
            newwin.document.write('window.print();\n')
            newwin.document.write('chkstate();\n')
            newwin.document.write('}\n')
            newwin.document.write('<\/script>\n')
            newwin.document.write('</HEAD>\n')
            newwin.document.write('<BODY  onload="print_win()">\n')
            newwin.document.write(str)
            newwin.document.write('</BODY>\n')
            newwin.document.write('</HTML>\n')
            newwin.document.close()

        }

Comments

Popular posts from this blog

Snake Xenzia

Hello friends did you remember the GAME you play on nokia 1100 , SNAKE XENZIA .Yeah the same you are thinking ,a snake eating food and go longer and longer till it dies. Why to play it on mobile when it is available on your pc ??? It is made in .net framework 4.0 .Just install the setup and play SNAKE XENZIA . Have Fun !! DOWNLOAD IT   HERE  . Related articles Rattlesnake Dreams "Classsic Snake Game In C"? Resident Evil 6 gamescom demo takes in a giant snake battle Snakes & ladders

Export Doc,Access,Image,CSV,Excel,Pdf,XML,HTML,Text,Print of Gridview in Asp.net

First import itextsharp.dll in the solution and use three namespces (basically for pdf) using iTextSharp.text; using iTextSharp.text.pdf; using iTextSharp.text.html.simpleparser;          Then Use the following code :- on each button  click  protected void Page_Load(object sender, EventArgs e)         {             if (!Page.IsPostBack)             {                 BindGridDetails(GridView1);             }         }                 protected DataTable BindGridDetails(GridView GridView1)         {   ...

10 home remedies for acidity that really work!

All of us have suffered from acidity at some point in our lives. Either it is that extra  gulab jamun  at a family function or that spicy  samosa  during tea break. Whatever the reason may be, acidity can be quite an uncomfortable experience, and sometimes we are left with no option but to reach out for that bottle of antacid. But, did you know that there are alternative, n                        atural remedies that work just as well and don’t leave you with the ill-effects of too much medication? Read about the  new-age technique to relieve the symptoms of acidity .  Here are 10 things to treat acidity and its recurrence at bay:   Bananas : Are high in potassium which is an alkalizing mineral that has a high pH value. The higher the pH value, the lower the acidity, which is  why the banana is a formidable antidote for acidity. They also have a component that makes the stomach...