Wednesday, 18 January 2017

VBA (Excel) – Add Single Quote, Excel text cells, Zeros 0s Fix

Hill All, happens to copy and paste from SQL tables or web Page to Excel files, if some fields begin with Zero 
example:“0000545LWEBCODE” , 
when paste to Excel Zeros 0s before other numbers were removed 
example:“545LWEBCODE”
To fix Zeros 0 before number and other things , Here is VBA funcion/Script which add a single quote ‘ before each cells: you can copy from any external platform like Internet, SQL Tables, SQL Views etc.., and paste it in your Microsoft Excel sheet,
Just Copy and Paste this code in a Module a run it.


‘** VBA Script Excel Zeros 0s Fix Script by lwebcode.blogspot.com
Public Sub AddQuote()
Dim Start_, End_, Col, i As Integer
Dim SName As String

Start_ = InputBox(“Row Start:”, “Start”, “1”)
End_ = InputBox(“Row End:”, “End”, “10000”)

Col = CInt(InputBox(“Column (in Number) to Add Quote”, “Column”, “1”))

SName = InputBox(“Sheets’s Name:”, “Sheets”, Sheets(1).Name)

For i = Start_ To End_
    If Sheets(SName).Cells(i, Col).Value “” Then
        Sheets(SName).Cells(i, Col).Value = “‘” & Sheets(SName).Cells(i, Col).Value
    End If
Next

MsgBox “Executed”, vbInformation, “Executed”

End Sub


Saturday, 14 January 2017

SQL - Double Rows

Hi All, here a fast script to find double rows!
Happens to have some table/s with 2 or more rows which have the same value on field, to find this rows here there's a script which select all double rows in table, 
After you can choose to delete or analize why there are double.

--lwebcode Select all double rows in a table
SELECT     City, Address, Zip, Year,  COUNT(*) AS Expr1
FROM        MyTable
GROUP BY  City, Address, Zip, Year
HAVING      (COUNT(*) > 1)


--lwebcode For a single  column double rows:
SELECT     City  COUNT(*) AS Expr1
FROM        MyTable
GROUP BY  City

HAVING      (COUNT(*) > 1)

Friday, 13 January 2017

SQL – Servers, Databases, Tables & Fields List

Hi All, here a very easy SQL script to get  Servers, Databases, Tables & Fields of  Table
 
To see other property of fields put ” ,*”  after COLUMN_NAME

1) Servers List:

SELECT * FROM master.dbo.sysservers

2)Databases List:

SELECT * FROM master.dbo.sysdatabases

3)Tables/Views List:

SELECT     TABLE_NAME
FROM         INFORMATION_SCHEMA.TABLES
WHERE     (TABLE_TYPE = ‘BASE TABLE’)


Change ‘BASE TABLE’ to ‘VIEWS’ to get Views list

4)Table's Field/Columns List:

SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME =’MyTalbe_Name’

Thursday, 12 January 2017

C# HTML to PDF Converter, iTextSharp

Hi All, we 've made lot of research to make this super useful function in c# to convert HTML to PDF,
with help of fantastic free library iTextSharp:

https://sourceforge.net/projects/itextsharp/
With only 6 rows it's done!
Warning!: HTML code must be very clear, simple and clean(CSS was not fully supported, is better use attributes to element if exists)
Example:

<!--lwebcode:inside yout html code-->
<div style='font-size:15px;text-align:left;'>Title Of Page<br/></div> ==> Works
<td align='center' style='width:15%' >cell content</td> ==> DOESN'T Works
<td align='center' width='15%' >cell content</td> ==> Works

//lwebcode:inside *.cs file
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
using iTextSharp.tool.xml;

//
lwebcode:inside your class lwebcode
private void ConvertHtmlToPdf(string sHTML)
    {
        Document document = new Document();
        PdfWriter.GetInstance(document, new FileStream(Server.MapPath("~/OutPath/TargetName.pdf"), FileMode.Create));
        document.Open();
        iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
        hw.Parse(new StringReader(sHTML));
        document.Close();
    }

SQL - Fast Table Copy

Hi All, this script is very easy,a simple and fast solution to copy (structure and data) a table :

SELECT * INTO Table_Destination_Name FROM Table_Source_Name

Warning:Using this script Index aren't copied in new table (Table_Destination_Name) for adds it you have to add it manually.

Wednesday, 11 January 2017

C# AJAX TextBox Cursor,Caret Focus with AutoPostBack="True" OnTextChanged="TextBox1_TextChanged"

Hi All, here explain how to mantain get and set cursor/caret position after a c# with AJAX Textbox TextChanged with AutoPostBack="True", this solution is cross browser compatible.
Put this Javascript code in your *.aspx Page:

<script language="javascript" type="text/javascript" >
//lwebcode.bloggger.com
//http://blog.vishalon.net/javascript-getting-and-setting-caret-position-in-textarea
var selstart=0;
var selend=0;

 var pos = 0;
 function GetPos(el)
 {
    if("selectionStart" in el) {
       pos = el.selectionStart;
    } else if("selection" in document) {
       el.focus();
       var Sel = document.selection.createRange();
       var SelLength = document.selection.createRange().text.length;
       Sel.moveStart("character", -el.value.length);
       pos = Sel.text.length - SelLength;
    }
    return pos;
 }

 function SetPos(ctrl, mypos)
 {
        if(ctrl.setSelectionRange)
        {
            ctrl.focus();
            ctrl.setSelectionRange(mypos, mypos);
        }
        else if (ctrl.createTextRange)
        {
            var range = ctrl.createTextRange();
            range.collapse(true);
            range.moveEnd('character', mypos);
            range.moveStart('character', mypos);
            range.select();
        }
  }
</script>


Always in your *.aspx page:

<asp:TextBox ID="TextBox1" runat="server" AutoPostBack="True" OnTextChanged="TextBox1_TextChanged" > 
<asp:TextBox ID="TextBox2" runat="server" AutoPostBack="True" OnTextChanged="TextBox2_TextChanged" >

On Page Code behind *.apsx.cs

private const string SCRIPT_DOFOCUS =
        @"window.setTimeout('DoFocus()', 1);
    function DoFocus()
    {
        try {
            document.getElementById('REQUEST_LASTFOCUS').focus();
            SetPos(document.getElementById('REQUEST_LASTFOCUS'), pos);
        } catch (ex) {}
    }";

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            HookOnFocus(this.Page as Control);
        }
        ScriptManager.RegisterStartupScript(this, typeof(Page), "ScriptDoFocus", SCRIPT_DOFOCUS.Replace("REQUEST_LASTFOCUS", Request["__LASTFOCUS"]), true);
    }
   
    private void HookOnFocus(Control CurrentControl)
    {
        //checks if control is one of TextBox, DropDownList, ListBox or Button
        if ((CurrentControl is TextBox)
            /*|| 
            (CurrentControl is DropDownList) ||
            (CurrentControl is ListBox) ||
            (CurrentControl is Button)*/
           
            )
            //adds a script which saves active control on receiving focus
            //in the hidden field __LASTFOCUS.
            (CurrentControl as WebControl).Attributes.Add(
               "onfocus",
               "try{document.getElementById('__LASTFOCUS').value=this.id; GetPos(this);} catch(e) {}");
        //checks if the control has children
        if (CurrentControl.HasControls())
            //if yes do them all recursively
            foreach (Control CurrentChildControl in CurrentControl.Controls)
                HookOnFocus(CurrentChildControl);
    }


protected void TextBox1_TextChanged(object sender, EventArgs e) 
{ //your code... }
protected void TextBox2_TextChanged(object sender, EventArgs e) 
{ //your code... }