Thursday 20 February 2020

Datatable date sorting issue type dd/mm/yyyy

/////////// There are multiple types of the features in the jquery -

------- Js Plugin --
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdn.datatables.net/1.10.20/js/jquery.dataTables.min.js"></script>
jQuery.extend(jQuery.fn.dataTableExt.oSort, {
            "date-eu-pre": function (date) {
                date = date.replace(" ", "");

                if (!date) {
                    return 0;
                }

                var year;
                var eu_date = date.split(/[\.\-\/]/);

                /*year (optional)*/
                if (eu_date[2]) {
                    year = eu_date[2];
                }
                else {
                    year = 0;
                }

                /*month*/
                var month = eu_date[1];
                if (month.length == 1) {
                    month = 0 + month;
                }

                /*day*/
                var day = eu_date[0];
                if (day.length == 1) {
                    day = 0 + day;
                }

                return (year + month + day) * 1;
            },

            "date-eu-asc": function (a, b) {
                return ((a < b) ? -1 : ((a > b) ? 1 : 0));
            },

            "date-eu-desc": function (a, b) {
                return ((a < b) ? 1 : ((a > b) ? -1 : 0));
            }

        });

--------- Call Function--

     //// Example
     // $('#example').dataTable( {
     //   columnDefs: [
     //     { type: 'date-eu', targets: 0 }
     //   ]

     //});

Sequence----

1. == >  Extension
2. == >  Datatable.html
3. == >  Datatable.js

Monday 17 February 2020

How to set default button in ASP.Net MVC click on ENTER Button




<script type="text/javascript">
            $(document).bind('keydown', function (e) {
                  // 13 is the enter key 
                  // .clssubmit is Submit button class/ID 
                if (e.which === 13) { // return
                    $('.clssubmit').trigger('click');
                }
            });
        </script>

Tuesday 11 February 2020

How to Serialized/Convert datatable/dataset to json c# javascriptserializer

//////////////////// Convert datatable to json

////// Using Namespace

using System.Web.Script.Serialization;

Steps: Application Root >> Right click >> Add >> Reference 



>> Framework >> System.web.Extensions




public static string DataTable_JSON(DataTable dataTable)
        {
            JavaScriptSerializer serializer = new JavaScriptSerializer();

            List<Dictionary<string, object>> tableRows = new List<Dictionary<string, object>>();
            Dictionary<string, object> row;
            foreach (DataRow dr in dataTable.Rows)
            {
                row = new Dictionary<string, object>();
                foreach (DataColumn col in dataTable.Columns)
                {
                    row.Add(col.ColumnName, dr[col]);
                }
                tableRows.Add(row);
            }
            return serializer.Serialize(tableRows);

        }




///////////////// Dataset to Json



public static string DataSet_JSON(DataSet ds)
        {

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            ///////// If add json without root name of the json
            //  System.Collections.ArrayList root = new System.Collections.ArrayList();

            Dictionary<string, object> _clientDictionary = new Dictionary<string, object>();
            List<Dictionary<string, object>> table;
            Dictionary<string, object> data;
           
            
            foreach (DataTable dt in ds.Tables)
            {
                table = new List<Dictionary<string, object>>();
               
                foreach (DataRow dr in dt.Rows)
                {
                    data = new Dictionary<string, object>();
                    foreach (DataColumn col in dt.Columns)
                    {
                        data.Add(col.ColumnName, dr[col]);
                    }
                    table.Add(data);
                }
                _clientDictionary.Add(dt.TableName, table);
                ///////// If add json without root name of the json
                //root.Add(table);
            }
            return serializer.Serialize(_clientDictionary);

        } 




Monday 3 February 2020

Create excel and zip without save file and images using asp.net

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


public void ExportToExcel(DataTable dt, string FIleName)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("");
            string Fullfilename = "" + FIleName + DateTime.Now.ToString("dd_MM_yyyy_hh_mm_ss") + ".xls";
            String HeaderStyle = "border:.3pt solid windowtext; font-weight:700;   word-wrap: normal; word-break: break-all; ";
            sb.Append("<table width='70%'>");
            sb.Append("<tr style='"+ HeaderStyle + "'>");
            string[] Heads = { "ID", "Name", "Address" };
            for (int j = 0; j < 2; j++)
            {
                sb.Append("<th>"+ Heads [j]+ "</th>");
            }
            sb.Append("</tr>");

            String RowStyle = "border:.1pt solid windowtext; font-weight:100; font-size:9pt;rowspan=2;";
         
            for (int j = 0; j < 5; j++)
            {
                sb.Append("<tr style='" + RowStyle + "'>");
                for (int i = 0; i < 2; i++)
                {
                    sb.Append("<td>" + "Name_"+j+"_"+i + "</td>");
                }              

                sb.Append("</tr>");
            }
          
            sb.Append("</table>");

            MemoryStream stream = new MemoryStream();
            byte[] data = Encoding.ASCII.GetBytes(sb.ToString());
            stream.Write(data, 0, data.Length);
            stream.Seek(0, SeekOrigin.Begin);

            HttpResponse Response = HttpContext.Current.Response;
            Response.ClearHeaders();
            Response.Charset = "UTF-8";

            string filePath = string.Empty;
            using (Ionic.Zip.ZipFile zip = new Ionic.Zip.ZipFile())
            {
                zip.AddEntry(Fullfilename, stream);
                zip.AlternateEncodingUsage = Ionic.Zip.ZipOption.AsNecessary;
                zip.AddDirectoryByName("UserDocs");
                string pth = Server.MapPath("~/FolderName");

                DirectoryInfo dir = new DirectoryInfo(pth);
                for (int i = 0; i < dt.Rows.Count; i++)
                {
                    foreach (var d in dir.GetFiles())
                    {
                        if (d.Name == Convert.ToString(dt.Rows[i]["ImageName"]))
                        {
                            string pt = pth + "\\" + d.Name;
                            zip.AddFile(d.FullName, "UserDocs");
                        }
                    }
                }


                Response.Clear();
                Response.BufferOutput = false;
                string fName =DateTime.Now.ToString("yyyy-MMM-dd-HHmmss");
                Response.ContentType = "application/zip";
                Response.AddHeader("content-disposition", "attachment; filename="fName +".zip";
                zip.Save(Response.OutputStream);
                Response.End();
            }
        }

Excel Sort values in ascending order using function TEXTJOIN

 Excel ::  Text ::  1,3,5,2,9,5,11 Result :: 1,2,3,5,5,9,11 Formula ::     TEXTJOIN ( ",",1,SORT(MID(SUBSTITUTE( A1 ,","...