Showing posts with label Programming. Show all posts
Showing posts with label Programming. Show all posts

Difference between Line Termination or Endings in Windows '/r/n' and UNIX '/n'. Different types of Line systems.

Hi Everyone in todays post, I will explore the Line Endings or End of Line (EOL) in different system and how to identify them -

First thing to note is -
0d stands for Carriage Return - CR
0a stands for Line Feed - LF

In Windows System when we press ENTER KEY a new line is represented with Hex control character of 0d and 0a.

In UNIX system, when we press ENTER KEY a new line is represented with Hex control character of 0a.

OS X also uses a single LF character, and classic Mac operating system used a single CR character for line breaks

Here (in Windows) we will use the below Text to create a file
First Line then Enter Key pressed
Second Line then Enter Key pressed


HEXADECIMAL View of Text File Created in WINDOWS system 

HEXADECIMAL View of Text File Created in WINDOWS system
 As we can see, for the file created in Windows system new line is represented by the characters 0d and 0a which is Carriage return and Line Feed

The Story of JavaScript - How did JavaScript originate and evolve

Hi Guys, in this post I will explore how JavaScript came into existence, as we all know, JavaScript is the world's most popular programming language, its good to know its history and it goes as below -

JavaScript is a client side scripting language developed by Brendan Eich of Netscape Corporations under the name Mocha.

Mocha was later renamed to LiveScript, and then finally to JavaScript mainly as was it was influenced by the Java programming language. 

LiveScript was the official name when it first shipped in beta releases of Netscape Navigator 2.0 in September 1995, it was renamed JavaScript in a joint announcement with Sun Microsystems on December 4, 1995 when it was deployed in the Netscape browser version 2.0 .

The change of name from LiveScript to JavaScript coincided with Netscape adding support for Java technology in its Netscape Navigator web browser. 

When to use Xml and when to use JSON

 

XML

JSON

You need message validation

You don't need Messages to be validated

XML transfer is slower then JSON since the file size is relatively compared to JSON.

JSON is a good if you want to transmit data faster from client to server, due to reduced file size

You need to transform the XML i.e. if  using XSLT

You're not transforming messages, or transforming their de serialization is simple

Your messages include a lot of marked-up text

Your messages are mostly data, not marked-up text

You need to interoperate with environments that don't support JSON

The messaging endpoints have good JSON tools

Used when objective is to carry or store data

Used when objective is to display the Data

When there is no concern for reduced file/transfer size.

When there is a requirement for reduced file/transfer size.

Processing is slower since XML has to be parsed with XML parser.

Processing is faster since JSON can be parsed by a standard JavaScript function.

 

How to Save or Read PDF or any File to or from a varbinary column in Sql Server - simplest C# code

Hi Guys, This post is a continuation to my earlier post where I wrote about searching for text in pdf files, In my earlier post I had shared query to insert the pdf's using Sql Server Open Rowset.

This post will explore saving and retrieving pdf files to or from a database table - In this sample code I have hard coded the values for better understanding, in real time we fetch these values from a database table or an excel.

The below code is for a Console Application -
Here the column "fileContent" is of type varbinary.

How to Search for Text inside PDF Files in SQL Server – Configuring Adobe PDF iFilter and implementing Full Text Search

Hi Guys,  In this post I will explore the implementation details for searching the contents inside the pdf's,
Writing this post based on a real-time requirement in my project, the requirement was to provide a UI where the user can enter the text to search, and display the results i.e. pdf's in a grid view that contains the search keyword with the option to download the pdf files.

Initial plan was to search by reading the pdf's from physical file system one by one, but since No of pdfs were large we decided to store the pdf files in the database.

Below Third-party libraries options were initially explored for searching pdf 's stored in File system –

Sl No
Library
Type
Remarks
1
iText
Paid

2
BitMiracle
Paid

3
FreeSpire.PDF
Free
Page limit per pdf is limited to 10 Pages in the Free version
4
Spire.PDF
Paid


To Search pdf files a software called as PDF iFilter is provided by Adobe so that we can search for pdf files that was stored and indexed in Sql Server.
Adobe PDFiFilter provides the ability for SQL Server to read the pdf contents.
Adobe PDFiFilter uses the Microsoft iFilter interface and allows third-party indexing tools to extract text from Adobe PDF files.

Below steps will all the configurations steps for configuring the Adobe PDF iFilter -  

Step 1) Full Text Search Installation check -

To check whether Full Text Search is installed or not, query the IsFullTextInstalled parameter.
select serverproperty('IsFullTextInstalled')
It should return 1
If it returns 0, the feature needs to be installed from SQL Server installation. install the Full Text Search in the feature selection page of Sql server installation


Simplest Code to Read and Import Excel file into DataSet or DataTable using C#

Hi everyone in this post I will show you the simplest Code I mean the most simplest code in C# to Read an Excel File.

Writing this post because of a scenario in Office where i had to generate XML Files by reading an Excel File,
So one of my approach was to read the Excel file using C# console application and populate into dataset and later use the xsd.exe to generate the XML File.
In this post I am talking only half my approach that is to populate the Excel data into DataSet Table using OLEDB.

Note About OLEDB - Microsoft designed ODBC to access SQL data and OLE DB to access any type of data in a COM environment.

Code to Read Excel and Store into Data Table using C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.OleDb;

namespace ReadExcelFileIntoDataSetInCSharp
{
    class Program
    {
        static void Main(string[] args)
        {
            String file_path = "C:\\ExcelData.xlsx";
            String excelConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file_path + ";Extended Properties=Excel 12.0;Persist Security Info=False";
            OleDbConnection excelConnection = new OleDbConnection(excelConnectionString);
            DataTable dataTableObj = new DataTable("ExcelDataTable");
            OleDbDataAdapter adp = new OleDbDataAdapter("SELECT [SLNo],[Name],[Age] FROM [Sheet1$]", excelConnectionString);
            adp.Fill(dataTableObj);
            excelConnection.Close();
          }
    }
}

C# program to Create a Unix style Text File with Line Termination of LineFeed 0a only How to

To Create a Unix style text file in C# we use the NewLine Property of the TextWriter class to set the line termination format to UNIX type.

For More information of different types of line Termination refer to my earlier post –

Syntax of NewLine property of TextWriter
public virtual string NewLine { get; set; }

If we need to create a file with Windows type of line termination, we set the NewLine property to "\r\n".
If we need to create a file with UNIX type of line termination, then we set the property to "\n"
If the NewLine property is not set, the default newline character("\r\n") is used.

Sample Code to Write Unix Type File in C#
using System;
using System.Text;
using System.IO;

namespace UnixFile
{
    class Program
    {
        static void Main(string[] args)
        {
            //Data for the text file, is stored inside a string array
            string[] lines = { "First Line", "Second Line" };

            //Variable fileDetails contains the file name and path
            String fileNameAndPath = "C:\\Sample Text File.txt";

            //Below line of code will delete the file if it already exists (OPTIONAL)
            if (File.Exists(fileNameAndPath)) { File.Delete(fileNameAndPath); }

            //Create a StreamWriter object
            using (TextWriter objFile = new StreamWriter(fileNameAndPath))
            {
                //Set the New line Property to Unix type of New line
                objFile.NewLine = "\n";
                objFile.WriteLine(lines[0]);
                objFile.WriteLine(lines[1]);
            }
        }
    }
}

How to add an Assembly automatically into GAC using Post Build Events in Visual Studio

Introduction: When we build a project, then the dll generated will not be added to GAC.
If we need to dll into the GAC we need to explicitly added it using the Visual Studio command prompt
There are scenarios where we need to add an assembly into the GAC after building the Visual Studio Project. To do this we can use the Post Build Events in VS.

Scripts inside Post Build Events will be executed once the build of the project is successful.

Steps to use Post Build Events in Visual Studio
Step 1) Right click the project Select Properties

Step 2) Select Build Events in the Left Pane
image

Why continue statement cannot be used inside finally block?

As we are aware that we can use continue statement inside a try or catch block, but not inside finally block. This post explores why continue statement cannot be present inside finally block.

Now for our understanding  let’s assume that we can write continue statements inside finally block also (in reality we get a compile timer error if we write inside finally).

Hence continue statements will be present in both Try block as well as Finally block.

Analysis - The code execution comes to try block and encounters the continue statement, the continue statement will be registered and all the remaining code in try block has to be skipped and control has to pass to the next iteration but since finally block is present, the code execution has to compulsorily pass to finally block.

Now the execution again encounters continue statement present inside the finally block, this will cause ambiguity to the execution, since two continue statements needs to be registered.