zonaro
zonaro
Reputation Top 1%
Gabriel Forti Zonaro
.NET Developer
30 Snippets  (14th place)
Published
1 Channel
Created
6 Channels
Following
796 points  (49th place)
Reputation
Top 5% Publisher
Junior Code Generator
Serious Code Generator
Senior Code Generator
Junior Publisher
Serious Publisher
Senior Publisher
Junior Topic Hub
Junior Trend Maker
Serious Trend Maker
Senior Trend Maker
Junior Commenter
Junior Judge
Serious Judge
Junior Popular Coder
Master Popular Coder
Junior Autobiographer
Senior Autobiographer
Master Autobiographer
Junior Famous Coder
Master Famous Coder
Junior Wise Coder

Recent Snippets See all snippets by zonaro

public by zonaro  4833  19  6  -1

DateDiff - Return the Diferrence Between Two Dates

Returns a string in "X years, Y months and Z days" format
public string DateDiff(DateTime FirstDay, DateTime LastDay)
        {
            TimeSpan diff = LastDay.Subtract(FirstDay);
            double years = Math.Floor(diff.Days / 365.25);
            double months = Math.Floor((diff.Days - (years * 365.25)) / 30);
            double days = Math.Floor(((diff.Days - (years * 365.25)) - (months * 30)));
             
            string year = (years > 0 ? (years == 1 ? years + " year," : years + " years,") : "");
            string month = (months > 0 ? (months == 1 ? months + " month and" : months + " months and") : "");
            string day = (days > 0 ? (days == 1 ? days + " day" : days + " days") : "");
            return year + " " + month + " " + day;
        }            
;

public by zonaro  3684  4  6  1

ToDataURL() - Convert a Image HttpPostedFile to DataURI (Base64)

Extension method to convert any image type in a HttpPostedFile to base64 in Data URL format
public static string ToDataURL(this HttpPostedFile PostedFile)
       {
           try
           {
               if (PostedFile.ContentType.ToLower().Contains("image/"))
               {
                   Stream input = PostedFile.InputStream;
                   int streamLength = Convert.ToInt32(input.Length);
                   byte[] fileData = new byte[streamLength];
                   input.Read(fileData, 0, streamLength);
                   return "data:" + PostedFile.ContentType.ToLower() + ";base64," + Convert.ToBase64String(fileData.ToArray());
               }
               else
               {
                   throw new FormatException(PostedFile.FileName + " is not image. (" + PostedFile.ContentType + ")");
               }
           }
           catch (Exception ex)
           {
               throw ex;
           }
       }

;

public by zonaro  4370  34  7  1

Console Hacking - Get all Images of webpage using Jquery

Just paste it on your webbrowser console and every image will be opened in a new tab (turn off your pop up blocker)
var imgs = $("img").map(function () { return this.src; });
for (var i = 0; i < imgs.length; i++) { 
   window.open( imgs[i]); 
}
;

public by zonaro  2962  3  6  0

Export any DbDataReader to Different Types

With this static class, you can export a DbDataReader (SQL Server, Access, MySQL, Oracle and Excel compatilbe) to XML, CSV, Text, HTML Table and JSON Formats. Also includes the IsNull validator to prepare your strings to queries and the GetColumns function to get The DbDataReader columns into a List of String
public static class ExtendDbDataReader
    {
        public static List<string> GetColumns(this DbDataReader Reader)
        {
            List<string> columns = new List<string>();
            for (int i = 0; i <= Reader.FieldCount - 1; i++)
            {
                columns.Add(Reader.GetName(i));
            }
            return columns;
        }
        public static string IsNull(this object Text, string ValorSeNulo = "NULL", bool Aspas = true)
        {
            if (Aspas)
            {
                return string.IsNullOrWhiteSpace("" + Text) ? ValorSeNulo : "'" + ("" + Text).Replace("'", "''") + "'";
            }
            else
            {
                return string.IsNullOrWhiteSpace("" + Text) ? ValorSeNulo : "" + Text;
            }

        }
        public static string ToHTMLTable(this DbDataReader Reader, string Classes = "")
        {
            string Returned = "";
            do
            {
                if (Reader.HasRows)
                {
                    Returned = "<table class=\"" + Classes + "\">";

                    Returned += " <thead>";
                    Returned += "     <tr>";
                    foreach (var item in Reader.GetColumns())
                    {
                        Returned += "         <th>" + item + "</th>";
                    }
                    Returned += "     </tr>";
                    Returned += " </thead>";
                    Returned += " <tbody>";
                    while (Reader.Read())
                    {
                        Returned += "     <tr>";
                        foreach (var item in Reader.GetColumns())
                        {
                            Returned += " <td>" + Reader[item] + "</td>";
                        }
                        Returned += "     </tr>";
                    }
                    Returned += " </tbody>";
                    Returned += " </table>";
                }
            }
            while (Reader.NextResult());
            return Returned;
        }
        public static string ToJSON(this DbDataReader Reader)
        {
            int contador = 0;
            string Returned = "{";
            do
            {
                contador = contador++;
                Returned += ("\"Table" + contador + "\":[");
                while (Reader.Read())
                {
                    Returned += ("{");
                    foreach (string coluna in Reader.GetColumns())
                    {
                        Returned += ("\"" + coluna.RemoverAcentos2() + "\":" + "\"" + HttpUtility.HtmlEncode(Reader[coluna].ToString()) + "\",");
                    }
                    Returned = Returned.Substring(0, Returned.Length - 1);
                    Returned += ("},");
                }
                Returned = Returned.Substring(0, Returned.Length - 1);
                Returned += ("],");
            } while (Reader.NextResult());
            Returned = Returned.Substring(0, Returned.Length - 1);
            return Returned + "}";
        }
        public static string ToDelimitedString(this DbDataReader rsEditar, string DelimitadorColuna = "|", string DelimitadorLinha = "§", string DelimitadorTabela = "♠")
        {
            string Delimitado = "";
            if (rsEditar == null) { return new Exception("Erro na consulta!").Message; }
            do
            {
                List<string> colunas = Enumerable.Range(0, rsEditar.FieldCount).Select(rsEditar.GetName).ToList();
                while (rsEditar.Read())
                {
                    foreach (var coluna in colunas)
                    {
                        Delimitado += (rsEditar[coluna] + DelimitadorColuna);
                    }
                    Delimitado += DelimitadorLinha;
                }
                Delimitado += DelimitadorTabela;
            } while (rsEditar.NextResult());

            return Delimitado;
        }
        public static string ToXML(this DbDataReader Reader)
        {
            string Returned = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?><response>";
            int count = -1;
            do
            {
                if (Reader.HasRows)
                {
                    count = count + 1;
                    Returned += "<table>";
                    int count2 = 0;
                    while (Reader.Read())
                    {
                        Returned += "<row>";
                        foreach (string item in Reader.GetColumns())
                        {
                            Returned += ("<" + item.Replace(" ", "_").RemoverAcentos2() + ">" + HttpUtility.HtmlEncode(Reader[item].ToString()) + "</" + item.Replace(" ", "_").RemoverAcentos2() + ">");
                        }
                        Returned += "</row>";
                        count2 = count2 + 1;
                    }
                    Returned += "</table>";
                }
            }
            while (Reader.NextResult());
            return Returned + "</response>";

        }
        public static string ToCSV(this DbDataReader Reader, string separator = ",")
        {
            string returned = "sep=" + separator + Environment.NewLine;
            if (Reader.HasRows)
            {
                while (Reader.Read())
                {
                    foreach (string item in Reader.GetColumns())
                    {
                        returned += "\"" + HttpUtility.HtmlEncode(Reader[item].ToString()) + "\"" + separator;
                    }
                    returned += Environment.NewLine;
                }
            }
            return returned;
        }

    }
;

public by zonaro  3047  0  7  0

Quickresponse - Response.Write and Response.End

Allow you to Response.Write and immediately stops the current Response.
public static void WriteEnd(this HttpResponse Response, String Text)
       {
           Response.Write(Text);
           Response.End();
       }
;