Monday, 22 June 2020

Calculate Day Difference in 2 dates using jquery

/////////////// html

var days = DateDiff_BetweenTowDates("23/03/2020" "24/05/2020", "dd/mm/yyyy");

              or

var days = DateDiff_BetweenTowDates("2020/03/23" "2020/05/23""yyyy/mm/dd");

              or

var days = DateDiff_BetweenTowDates("03/23/2020" "05/25/2020""mm/dd/yyyy");

////////////// js


function DateDiff_BetweenTowDates(dateFrm, DateTo, CurrentFormat) {
    var start, end;
    if (CurrentFormat.toLowerCase().substring(0, 2) == "dd") {
        var d1 = dateFrm.split('/');
        var d2 = DateTo.split('/');
        start = d1[1] + '/' + d1[0] + '/' + d1[2];
        end = d2[1] + '/' + d2[0] + '/' + d2[2];
    }
    else if (CurrentFormat.toLowerCase().substring(0, 2) == "mm") {
        start = dateFrm;
        end = DateTo;
    }
    else if (CurrentFormat.toLowerCase().substring(0, 2) == "yy") {
        var d1 = dateFrm.split('/');
        var d2 = DateTo.split('/');
        start = d1[1] + '/' + d1[0] + '/' + d1[2];
        end = d2[1] + '/' + d2[0] + '/' + d2[2];
    }
    else {
        return false;
    }
    var dt1 = new Date(start);
    var dt2 = new Date(end);
    return days = (dt2 - dt1) / (1000 * 60 * 60 * 24);
}

Friday, 12 June 2020

The source contains no DataRows OR filter datatable datetime in c#

///////////////////////////


///////// Method 1
            DataTable dt = new DataTable();

            DataTable dt2 = dt.Clone();
            var rows = dt.Select().Where(p => (Convert.ToDateTime(p["DateTime"]).Date >= System.DateTime.Now.AddDays(-50)) && (Convert.ToDateTime(p["DateTime"]) <= System.DateTime.Now.Date));

            foreach (var row in rows)
                dt2.ImportRow(row);



///////// Method 2
            DataTable dtn = new DataTable();
            dtn = dt.Clone();
            dtn = dt.Select().Where(p => (Convert.ToDateTime(p["DateTime"]).Date >= System.DateTime.Now.AddDays(-50)) && (Convert.ToDateTime(p["DateTime"]) <= System.DateTime.Now.Date)).CopyToDataTable();



 ///////// Method 3
            DataTable dt3 = new DataTable();
            dt3 = dt.Clone();
            var reRows = dt.Select().Where(m => ((DateTime)m["DateTime"]).Date >= DateTime.Now.Date);

            if (reRows.Any())
                dt3 = reRows.CopyToDataTable();


Reset all input value inside a control using jquery

///////////////js


function Reset_Controls(CtrlID_Cls, IsCtrlClass) {
    let ctlrid;
    if (IsCtrlClass) {
        ctlrid = $("." + CtrlID_Cls);
    }
    else {
        ctlrid = $("#" + CtrlID_Cls);
    }
    $(ctlrid).find("input[type=text], textarea").val("");
    $(ctlrid).find("input[type=checkbox]").prop(":checked", false);
    $(ctlrid).find('select').val($('select option').eq(0).val());

    /////// ******************** Set Null for all
    // $(ctlrid).find('select').val($('select').val([]);

    /////// ******************** Set value 0 for all
    //$(ctlrid).find("select").val("0");
}

Thursday, 11 June 2020

Maintain Gridview Scroll Position Inside div and Update Panel in asp.net


/////////////// JS


<script type="text/javascript">
            var scrollTop;
            Sys.WebForms.PageRequestManager.getInstance().add_beginRequest(BeginRequest);
            Sys.WebForms.PageRequestManager.getInstance().add_endRequest(EndRequest);

            function BeginRequest(sender, args) {
                var m = document.getElementById('TabledivGrid');
                scrollTop = m.scrollTop;
            }
            function EndRequest(sender, args) {
                var m = document.getElementById('TabledivGrid');
                m.scrollTop = scrollTop;
            }
        </script>




Saturday, 6 June 2020

Not able to install bootstrap 4.0.0 (beta) nuget package to .Net MVC (.Net version 4.6.1)

////////////////////

 Manually add the popper.js package in to your packages config below-


<package id="WebGrease" version="1.6.0" targetFramework="net461" />


Now install package again. Its working fine

Wednesday, 3 June 2020

Export to excel file in MVC using stringbuilder without any extra source

/////////////////.cshtml


<button id="btnExport" onclick="DownloadExcel()" class="btn btn-danger pull-right" title="Download Excel"> Export </button>

//////////////// Js

<script type="text/javascript">

    function DownloadExcel() {
var urls = "@Url.Action("MethodName", "ControllerName")"+"?FileNames=test";
window.location = urls;}
</script>



///////////////////// Controller

        public ActionResult Download_Clinic_Details(string FileNames)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("RollNo");
            dt.Columns.Add("Name");
            dt.Columns.Add("College Name");
            DataRow dr;
            int rn = 1001;
            for (int i = 0; i < 10; i++)
            {
                dr = dt.NewRow();
                dr[0] = (rn).ToString();
                dr[1] = "Raja_" + i.ToString();
                dr[2] = "Name of IT College_" + i.ToString();
                dt.Rows.Add(dr);
                rn++;
            }
            dt.AcceptChanges();
            StringBuilder StrExport = new StringBuilder();
            StrExport.Append(@"<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:w='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'><head><title>Time</title>");
            StrExport.Append(@"<body lang=EN-US style='mso-element:header' id=h1><span style='mso--code:DATE'></span><div class=Section1>");
            StrExport.Append("<div style='font-size:12px;'>");
            // StrExport.Append(dvInfo.InnerHtml);
            StrExport.Append("<div><center style='font-size: 12pt;'><b>This excel generated by Krishna kumar chaturvedi</b></center></div><br />");

            StrExport.Append("<table align='center' border='1' bordercolor='#00aeef' class='reporttable1' cellspacing='0' cellpadding='0' style='font-size: 10pt;'>");
            //StrExport.Append("<tr align='center'>");
            string[] sty = { "70", "300", "90" };
            for (int i = 0; i < dt.Columns.Count; i++)
            {
                StrExport.Append("<th style='width: " + sty[i] + "px;word-break: break-word;'>" + dt.Columns[i].ColumnName + "</th>");
            }
            StrExport.Append("</tr>");
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                StrExport.Append("<tr>");
                for (int c = 0; c < dt.Columns.Count; c++)
                {
                    StrExport.Append("<td>" + Convert.ToString(dt.Rows[i][c]) + "</td>");
                }
                StrExport.Append("</tr>");
            }
            StrExport.Append("</table>");
            StrExport.Append("</div></body></html>");
            FileNames = FileNames+""+System.DateTime.Now.ToString("yyyyMMddhhmmss");
            HttpContext.Response.AddHeader("content-disposition", "attachment; filename=" + FileNames+".xls");
            this.Response.ContentType = "application/vnd.ms-excel";
            byte[] buffer = System.Text.Encoding.UTF8.GetBytes(StrExport.ToString());
            return File(buffer, "application/vnd.ms-excel");
        }      

How to highlight selected text in notepad++

  –> To highlight a block of code in Notepad++, please do the following steps step-1  :- Select the required text. step-2  :- Right click...