Tuesday, 31 January 2017

LWEBCODE on Facebook, Get Like and Share buttons on your website or custom URL

Hi all, LWEBCODE is now on facebook





To get Facebook like or share button to a custom page, refer to this address: https://developers.facebook.com/docs/plugins/like-button
Click to Open

Monday, 30 January 2017

c# fast traslation of websites, Extract *.resx To *.xls

Hi All, Here a very quick Class which get all Entries to translate from resource *.resx file and make a Excel *.xls file with a list of all Entries.
Program writes entries in “A” column, 

In “B” (“C”,”D”…if you have more languages) Final user have to puts his language translation,
when done, after xls is saved and came back:
make a copy of *.resx file
With advanced text editor such as notepad++ or ultraedit after have make a copy of *.resx file, with “Find and Replace” tool I can easly traslate all the entries.
here the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Xml;
using Excel; // Add refer to Excel (in COM object Microsoft Excel Library )
using System.IO;


//#LWEBCODE
//Website Building & free script at http://lwebcode.blogspot.com/


namespace LWEBCODE
{
    public partial class ResxToXls
    {
        public static void MyStart()
        {
        string sPath =@"\\server\folder\" // where resx files are
        string DestFile = "", Title = "", ResxFile = "";
        string[] SourceFiles = Directory.GetFiles(sPath);
        if (SourceFiles.Length > 0)
        {
            for (int i = 0; i < SourceFiles.Length; i++)
            {
                if (SourceFiles[i].EndsWith(".resx"))
                {
                ResxFile = SourceFiles[i];
                Title = PageNameASPX(ResxFile);
                DestFile = ResxFile.Substring(0, ResxFile.LastIndexOf(@"\") + 1) + Title + ".xls";

                ReadResx(ResxFile, DestFile);
                }
            }
        }
        }

        public static void ReadResx(string ResxPath, string XlsPath)
        {
        Excel.ApplicationClass excel = new ApplicationClass();
        XmlTextReader xTr = new XmlTextReader(ResxPath);
        string ris = "";
        bool bwrite = true;
            while (xTr.Read())
            {
                // Do some work here on the data.
                //Console.WriteLine(xTr.Name);
                switch (xTr.NodeType)
                {
                case XmlNodeType.Element: // The node is an element.
                Console.Write("");

                break;
                case XmlNodeType.Text: //Display the text in each element.
                Console.WriteLine(xTr.Value);

                if (bwrite)
                ris += xTr.Value + ";";
                break;
                case XmlNodeType.EndElement: //Display the end of the element.
                Console.Write("");

                if ((xTr.Name.ToLower() == "comment") || (xTr.Name.ToLower() == "resheader"))
                bwrite = true;
                break;
                }
            }
        Console.Write(ris);
        Excel.Workbook workbook = excel.Workbooks.Add(XlWBATemplate.xlWBATWorksheet);
        Excel.Sheets sheets = workbook.Worksheets;

        excel.Visible = true;
        Excel.Worksheet mySheet = (Excel.Worksheet)sheets.get_Item(1);

        string[] splRis = ris.Split(Convert.ToChar(";"));
        int ExRow = 0;
        for (int j = 1; j < splRis.Length; j++)
        {
            ExRow = j + 4;
            Excel.Range myCell = (Excel.Range)mySheet.get_Range("A" + ExRow.ToString(), "A" + ExRow.ToString());
            myCell.Value = splRis[j - 1];
        }
        workbook.SaveAs(XlsPath, Excel.XlFileFormat.xlExcel9795, null, null, false, false, XlSaveAsAccessMode.xlNoChange, null, null, null, null);
        excel.Quit();
        }

        public static string PageNameASPX(string ResxPath)
        {
        return ResxPath.Substring(ResxPath.LastIndexOf(@"\") + 1).Replace(".resx","");
        }
    }
}

Friday, 27 January 2017

C# GridView Runtime Change Header Text, AutoGenerateColumns=”true” AND AllowSorting =”True”

Hi All, here’s function to runtime Change Header Text with AutoGenerateColumns=”true” AND AllowSorting =”True” in ASPX GridView:

//#LWEBCODE
//Website Building & free script at http://lwebcode.blogspot.com/
 

protected void GVTestate_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.Header)
    {

        LinkButton lnk = new LinkButton();
        // I've hide My first column in my GridView
        // You should have to Start from index 0
        // lnk = (LinkButton)e.Row.Cells[0].Controls[0];

        lnk = (LinkButton)e.Row.Cells[1].Controls[0];
        lnk.Text = "HeaderText1"
        e.Row.Controls[1].Controls.Clear();
        e.Row.Controls[1].Controls.Add(lnk);

        lnk = (LinkButton)e.Row.Cells[2].Controls[0];
        lnk.Text = "HeaderText2"
        e.Row.Controls[2].Controls.Clear();
        e.Row.Controls[2].Controls.Add(lnk);

        lnk = (LinkButton)e.Row.Cells[3].Controls[0];
        lnk.Text = "HeaderText3"
        e.Row.Controls[3].Controls.Clear();
        e.Row.Controls[3].Controls.Add(lnk);

        lnk = (LinkButton)e.Row.Cells[4].Controls[0];
        lnk.Text = "HeaderText4"
        e.Row.Controls[4].Controls.Clear();
        e.Row.Controls[4].Controls.Add(lnk);

        lnk = (LinkButton)e.Row.Cells[5].Controls[0];
        lnk.Text = "HeaderText5"
        e.Row.Controls[5].Controls.Clear();
        e.Row.Controls[5].Controls.Add(lnk);

        lnk = (LinkButton)e.Row.Cells[6].Controls[0];
        lnk.Text = "HeaderText6"
        e.Row.Controls[6].Controls.Clear();
        e.Row.Controls[6].Controls.Add(lnk);
    }
}

Thursday, 26 January 2017

C# RSS 2.0 Atom FEED Writer

Hi All, here a lwebode's C# class to generate a Feed rss 2.0 ,
it can generate by code or by SQL with dataset create a new class naming it “RSS_Writer” and just copy and paste code below.

//LWEBCODE C# RSS 2.0 Atom FEED Writer Usage example:

//To generate a feed by code (in this case rss will placed into root directory of websites):
string RssPath = Server.MapPath("../rss.xml");
RSS_Writer.NewRss(RssPath, "FEED Title", "www.exmple.com", "Header Description of feed", "en-us");

//to add a items programmatically:
RSS_Writer.AddItems(RssPath, "your title", "link to this article", "Description", "");

//to generate rss from SQL after have filled a Dataset (ds) :
RSS_Writer.CreateRssFromDataSet(RssPath, ds, "titleFiled", "LnkField", "DescrField", "GuidField");



//Here C# LWEBCODE RSS 2.0 Atom FEED Writer Class

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml;
using System.IO;


/// RSS_Writer
/// generated by LWEBCODE
/// http://lwebcode.blogspot.com/
///
///

public class RSS_Writer
{
public RSS_Writer()
{

//string path = HttpContext.Current.Server.MapPath("rss.xml");
public static int NewRss(string path, string sTitle, string sLink, string sDescription, string sLanguage)
{
    //XmlTextWriter writer = OpenXMLWriter(path);
    /*writer.WriteStartDocument();
    writer.WriteProcessingInstruction("xml-stylesheet", "type='text/xsl' href='lead.xsl'");
    writer.WriteStartElement("rss");
    writer.WriteAttributeString("version", "2.0");
    writer.WriteStartElement("channel");
    writer.WriteElementString("title", sTitle);
    writer.WriteElementString("link", sLink);
    writer.WriteElementString("description", sDescription);
    writer.WriteElementString("language", sLanguage);
    string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");
    writer.WriteElementString("lastBuildDate", GMTDate);
    */
    XmlDocument xd = new XmlDocument();
    //xd.Load(path);
    XmlNode xn;
    xn = xd.CreateNode(XmlNodeType.Element, "rss", null);
    XmlAttribute xa;
    xa = xd.CreateAttribute("version");
    xa.Value = "2.0";
    xn.Attributes.Append(xa);
    xd.AppendChild(xn);
    xn = xd.CreateNode(XmlNodeType.Element, "channel", null);
    xd.SelectSingleNode("//rss").AppendChild(xn);
    xn = xd.CreateNode(XmlNodeType.Element, "title", null);
    xn.InnerText = sTitle;
    xd.SelectSingleNode("//channel").AppendChild(xn);
    xn = xd.CreateNode(XmlNodeType.Element, "link", null);
    xn.InnerText = sLink;
    xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//title"));
    xn = xd.CreateNode(XmlNodeType.Element, "description", null);
    xn.InnerText = sDescription;
    xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//link"));
    xn = xd.CreateNode(XmlNodeType.Element, "language", null);
    xn.InnerText = sLanguage;
    xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//description"));
    string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");
    xn = xd.CreateNode(XmlNodeType.Element, "lastBuildDate", null);
    xn.InnerText = GMTDate;
    xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//language"));
    XmlTextWriter writer = OpenXMLWriter(path, false);
    xd.Save(writer);
    CloseXMLWriter(writer);
    return 0;
}
public static int AddItems(string path, string sTitle, string sLink, string sDescription, string sGuid)
{
    UpdateRssHEAD(path);
    XmlDocument xd = new XmlDocument();
    xd.Load(path);
    XmlNode xn = xd.CreateNode(XmlNodeType.Element, "item", null);
    XmlNode xn2;
    xn2 = xd.CreateNode(XmlNodeType.Element, "title", null);
    xn2.InnerText = sTitle;
    xn.AppendChild(xn2);
    xn2 = xd.CreateNode(XmlNodeType.Element, "link", null);
    xn2.InnerText = sLink;
    xn.AppendChild(xn2);
    xn2 = xd.CreateNode(XmlNodeType.Element, "description", null);
    xn2.InnerText = sDescription;
    xn.AppendChild(xn2);
    string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");
    xn2 = xd.CreateNode(XmlNodeType.Element, "pubDate", null);
    xn2.InnerText = GMTDate;
    xn.AppendChild(xn2);
    xn2 = xd.CreateNode(XmlNodeType.Element, "guid", null);
    xn2.InnerText = sGuid;
    xn.AppendChild(xn2);
    xd.SelectSingleNode("//channel").InsertAfter(xn, xd.SelectSingleNode("//lastBuildDate"));
    XmlTextWriter writer = OpenXMLWriter(path, false);
    xd.Save(writer);
    CloseXMLWriter(writer);
    return 0;
}

public static int CreateRssFromDataSet(string path, DataSet ds, string sTitleField, string sLinkField, string sDescriptionField, string sGuidField)
{
    for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
    {
    AddItems(path, ds.Tables[0].Rows[i][sTitleField].ToString(), ds.Tables[0].Rows[i][sLinkField].ToString(), ds.Tables[0].Rows[i][sDescriptionField].ToString(), ds.Tables[0].Rows[i][sGuidField].ToString());
    }
    return 0;
}

private static int UpdateRssHEAD(string path)
{
    XmlDocument xd = new XmlDocument();
    xd.Load(path);
    XmlNode nodeCh = xd.DocumentElement.SelectSingleNode("//channel");
    XmlNode xnd = xd.DocumentElement.SelectSingleNode("//lastBuildDate");
    string GMTDate = System.TimeZone.CurrentTimeZone.ToUniversalTime(DateTime.Now).ToString("r");
    xnd.InnerText = GMTDate;
    nodeCh.ReplaceChild(xnd, xnd);
    XmlTextWriter writer = OpenXMLWriter(path, false);
    xd.Save(writer);
    CloseXMLWriter(writer);
    return 0;
}
private static XmlTextWriter OpenXMLWriter(string path)
{
    XmlTextWriter writer;
    if (!File.Exists(path))
    {
        writer = new XmlTextWriter(path, System.Text.Encoding.UTF8);
    }
    else
    {
        Stream xmlFile = new System.IO.FileStream(path, FileMode.Append);
        writer = new XmlTextWriter(xmlFile, System.Text.Encoding.UTF8);
    }
    return writer;
}
    private static XmlTextWriter OpenXMLWriter(string path, bool Append)
    {
        XmlTextWriter writer;
        if ((!File.Exists(path)) || (!Append))
        {
        writer = new XmlTextWriter(path, System.Text.Encoding.UTF8);
        }
        else
        {
        Stream xmlFile = new System.IO.FileStream(path, FileMode.Append);
        writer = new XmlTextWriter(xmlFile, System.Text.Encoding.UTF8);
        }
    return writer;
    }
   
    private static void CloseXMLWriter(XmlTextWriter writer)
    {
    writer.WriteEndDocument();
    writer.Flush();
    writer.Close();
    }
}


/*

IMPORTANT NOTES:
Then to configure IIS, follow these steps:
Open IIS and navigate to the appropriate application/website
Right click and choose ‘Properties’ from the menu
Select the ‘HTTP Headers’ tab
There’s a section at the bottom entitled ‘MIME Map’, and from that click on ‘File Types’
Click ‘New Type’
For the ‘Associated extension’ enter .rss
And for ‘Content type (MIME)’ enter application/rss+xml
Click ‘OK’ and then ‘Apply’
Feel free to restart the IIS server, although you shouldn’t have to.
Ensure that the HTML page link to the RSS file includes the RSS extension, e.g.
*/
}

Wednesday, 25 January 2017

C# CSS Stylesheet Compression, fast website loading

Hi All, Here's a static class developed in c# to compress Css stylesheets,
It removes all “\r\n” (newline carriage return) , all “\t” (tab) all spaces before and after “:” “;” , it also remove all comment from your css.
Thanks to this function Stylesheet should be more light and fast for web and you can remove additionals spaces manually for complete optimization .
You can copy your css and pass it to class with:
textBox2.Text = LWEBCODE.Get_CSS_RTM(textBox1.Text);
I suggest to make a form o webform with 2 textboxes and paste in textbox1 your css stylesheet ,result can be show in textbox2 , after paste into css_runtime_sheet.css, 
REMEMBER on pagae_load event or in <head> tag to correctly set the right stylesheet: version for development and debug, or runtime version
Please for any optimization or suggestion contact us.
Here the code:


using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;

/*
Css Runtime Generator, compressor
developed by: lwebcode;
for other script and resource :
http://lwebcode.blogspot.com/
*/
class LWEBCODE
{
    public static string Get_CSS_RTM(string sInpunt)
    {
    string sOut = "";
    char[] c = System.Environment.NewLine.ToCharArray();
    string[] sRows = new string[] { };
    string[] sRow = new string[] { };
    bool Esc1 = false;
    bool Esc2 = false;
    bool Esc3 = false;

    sRows = Regex.Split(sInpunt, "\t");
    for (int i = 0; i 0)
    {
        if (sRows[i].IndexOf(Convert.ToChar("-")) < 0)
        {
        sRows[i] = sRows[i].Trim();
        }
    }
    sRow = SplitChar(sRows[i]);
    for (int j = 0; j < sRow.Length; j++)
    {
        if (sRow[j] == "/")
        {
            if (j < sRow.Length - 1)
            {
            if (sRow[j + 1] == "*")
            {
            Esc1 = true;
            }
        }
    }
    if (sRow[j] == "*")
    {
        if (j < sRow.Length - 1)
        {
            if (sRow[j + 1] == "/")
            {
            Esc1 = false;
            j = j + 2;
            }
        }
    }

    if (j < sRow.Length)
    {
    char[] c2 = sRow[j].ToCharArray();
        if ((c[0] == c2[0]) || (c[1] == c2[0]))
        Esc2 = true;
        else
        Esc2 = false;
    }
    if (j < sRow.Length)
    {
    char[] c2 = sRow[j].ToCharArray();
    if (c2[0] == Convert.ToChar(" "))
    {
        if ((sRow[j - 1] == ":") || (sRow[j - 1] == ";"))
        Esc3 = true;
        else
        Esc3 = false;
        if ((j + 1 < sRow.Length) && (!Esc3))
        {
            if ((sRow[j + 1] == ":") || (sRow[j + 1] == ";"))
            Esc3 = true;
            else
            Esc3 = false;
        }
    }
    else
    {
    Esc3 = false;
    }
    }

    if ((!Esc1) && (!Esc2) && (!Esc3))
    {
        if (j <= sRow.Length - 1)
        {
        sOut += sRow[j];
        }
    }
    }
    return sOut;
    }

    private static string[] SplitChar(string sIn)
    {
        string[] sInSpl;
        sInSpl = new string[sIn.Length];
        for (int i = 0; i < sIn.Length; i++)
        {
        sInSpl[i] = sIn.Substring(i, 1);
        }
        return sInSpl;
    }

}


Tuesday, 24 January 2017

SQL Dynamic table’s name in SELECT COUNT(*),TOP 1, MAX etc..

Hi All, here a easy lwebcode's sql script, to make a SELECT COUNT(*), or other sql statements wich return only 1 result (SELECT TOP 1, SELECT MAX, etc..) with dynamic tables name and put in a parameter o variable, 
It works well in All Sql Server versions, from MS SQL Server 2000 to last SQL Server 2016 

--LWEBCODE SQL SCRIPT Dynamic table's name
Declare @tableName varchar(100),
@tableCount int,
@sql nvarchar(100)
Set @tableName = 'Products'
Set @sql = N'Select @tableCount = Count(*) from ' + @tableName
exec sp_executesql @sql, N'@tableCount int output', @tableCount output
Select @tableCount

Monday, 23 January 2017

LWEBCODE Twitter Account

Follow LWEBCODE ON Twitter: