Friday, 13 February 2015

POCO classes in Entity FrameWork

POCO :- POCO class is the class which doesn’t depend on any framework specific base class. It is like any other normal .net class that is why it is called “Plain Old CLR Objects” These POCO entities (also known as persistence-ignorant objects) support most of the same LINQ queries as Entity Object derived entities. These classes (POCO classes) implement only the domain business logic of the application.
Some developers use DTOs (Data Transfer Objects) with classes to pass the data b/w layers., Because POCOs are also used to pass data b/w layers ,but they become heavy. So they use DTOs those are also classes.
The main difference b/w DTO and POCO is that DTOs do not contain any methods. They only contain public members. So sending data through DTO is easy because this is lightweight Objects .
The below  code defines a POCO class.



If you want to create POCO classes instead of entity classes or default entity object, then you can create POCO entity classes.
To create POCOs classes first we have to disable auto create classes or auto create code generation, Which generate Context classes entity code in Model1.designer.cs. For disable this code generation right click on model1.edmx(ado.net data modal) and  click on property then you will see   the value of ‘Custom Tool’ As ‘EntityModelCodeGenerator ’ you remove this value.




After removing value of “Custom tool” you will se that in modal1.edmx there is no  Model1.designer.cs class means now we have to create properties (Context and Entities ) so for this we have to create POCOs classes.
Now double click on Modal1.edmx and right click on designer surface and click on Code generation Items. There a screen will open from there you select ‘ADO.NET POCO Entity Generator’ and click ‘Add’.


After click on add button you will see 2 classes one is modal1.context.tt and other is modal1.tt


Model1.Context.tt is a context file and Model1.tt is entities file. You can modify this file if you want to generate your template. Model1.Context.cs file has a context class and .cs files under Model1.tt are entity classes.
Entity classes have all properties as "Virtual". Means that these entities fulfil the requirements of POCO Proxy entities . These entities can be used as POCO entities or POCO Proxy entities. By default it will be behave  as POCO Proxy entities, but you can disable proxy creation by setting  a property "ObjectContext.ContextOptions.ProxyCreationEnabled = false".
Important :
  • If you want to write a  unit test for context then replace ObjectSet<> to IObjectSet<>
  • If you are not able to see ‘ADO.NET POCO Entity Generator’ then you install  NuGet package library.



Tuesday, 3 February 2015

What is entity Framework


Entity FrameWork  is a ORM framework. ORM stands for  "Object Relational Mapping " .

Use of ORM framework to creates the classes of database table and vice versa.

Why we Use  Entity FrameWork  The main and the only benefit of EF is it auto-generates code for the Model (middle layer), Data Access Layer, and mapping code, thus reducing a lot of development time.

Difference b/w ADO.NET and Entity Framework 
The main use of EF is that it is directly create object of database tables and the process of updating the database. Its also reduce the lot of  development work otherwise We have to write and maintain code.  Further, because the mapping between your objects and your database is specified declaratively instead of in code. In EF Actually queries are written in LINQ. Except EF when we write any query we have to write different query for different database but when we write any query in linq its support all database. So that EF is very useful.

Creating the model from database:
STEP -1 :Open the new project  and give the name of that project.
STEP 2 : Install entity framework
(A)        : Go to tools and click on Extensions and Updates.

In this screen click on online and search “nueget package manager” and install it.

(B)        After installing “nueget package manager”  you will see a new node as “nueget package manager” in tools


(C)        Click on Manage NuGet Package For solution and Search for “ entity framework” (nugget.orgs) and install it.

STEP 3 : Right click on solution explorer and add new item and select  “ADO.NET Entity DataModal” and give the name for this

                 
Step(4) : When you will click on add you will see this following screen. Select EF designer from database and click on Next and give the connection Once the database has been selected, the wizard will generate the Entity connection string. This is something like a regular database connection string. The only difference is that it contains more information on the Entity Framework model. and give the name of App.config file




Step(5): After click on next you will see your database tables from here you select you table name (you also can say class objects).


After  select  tables click on finish  now you will see  your these tables as  entity data maodal as a relation.

This is the way for creating a Entity database modal. 

Tuesday, 12 August 2014

More details about generating PDF using Spire.PDF


The intention behind writing this article is to introduce a new product for generating PDF which I felt is very simple and easy to use. Really generating a PDF makes it very simple using this component.
For this, you need not required to install Acrobat Reader and good thing is it can be integrated with .Net. So using C# or VB.NET we can create our PDF document.
Let me show some couple of example which will give you clear idea of how simple it is to use this component.
Steps to generate the simple PDF document –

1  1)      Add the reference of the Spire.Pdf dll in your project.

2)  Create an object of PDF document
PdfDocument doc = new PdfDocument();
3) Now you need a content place holder where actual content need to be loaded. So add a page in the PDF document.
PdfPageBase page = section.Pages.Add();
Creating PDF document with Text
Using Canvas.DrawString method, add the content. Here I have taken one textbox in my web page and loaded the content from that text box.
Code for add text object
private void LoadText(PdfSection section)
    {
        PdfPageBase page = section.Pages.Add();
        page.Canvas.DrawString(txtFreeText.Text,
                               new PdfFont(PdfFontFamily.Helvetica, 20f),
                               new PdfSolidBrush(Color.Red),
                               10, 10);
    }

Adding image in a PDF
For loading the image and adding it in a PDF document is very simple.
Create an object of PdfImage which takes parameter of Stream object. Here I have used FileUpload control in my web page and passed the InputStream as shown below:
PdfImage image = PdfImage.FromStream(FileUpload1.PostedFile.InputStream);

Code for adding Image
private void LoadImage(PdfSection section)
    {
        if (FileUpload1.HasFile)
        {
            PdfPageBase page = section.Pages.Add();
            PdfImage image = PdfImage.FromStream(FileUpload1.PostedFile.InputStream);
            float width = image.Width * 0.50f;
            float height = image.Height * 0.50f;
            float x = (page.Canvas.ClientSize.Width - width) / 2;

            page.Canvas.DrawImage(image, x, 60, width, height);
        }
    }

Adding grid in a PDF
This is the most important type of data representation which we usually require in our application.
i.e. - Show data in a grid format. This tool provides us easy way of creating grid structure.
Here we create an object of PdfTable and then using a DataSource property provides a collection object. I want to show First Name and Last Name in my grid.
PdfTable table = new PdfTable();

Code for generating grid

  PdfPageBase page = section.Pages.Add();

        //create table
        PdfTable table = new PdfTable();
        table.Style.CellPadding = 2;
        table.Style.DefaultStyle.BackgroundBrush = PdfBrushes.SkyBlue;
        table.Style.DefaultStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
       
        table.Style.AlternateStyle = new PdfCellStyle();
        table.Style.AlternateStyle.BackgroundBrush = PdfBrushes.LightYellow;
        table.Style.AlternateStyle.Font = new PdfTrueTypeFont(new Font("Arial", 10f));
       
        table.Style.HeaderSource = PdfHeaderSource.ColumnCaptions;
        table.Style.HeaderStyle.BackgroundBrush = PdfBrushes.CadetBlue;
        table.Style.HeaderStyle.Font = new PdfTrueTypeFont(new Font("Arial", 11f, FontStyle.Bold));
        table.Style.HeaderStyle.StringFormat = new PdfStringFormat(PdfTextAlignment.Center);
       
        table.Style.ShowHeader = true;

        table.DataSourceType = PdfTableDataSourceType.TableDirect;

//GenerateList return a data table       
table.DataSource = GenerateList();
       
        //Set the width of column
        float width = page.Canvas.ClientSize.Width - (table.Columns.Count + 1);
        table.Columns[0].Width = width * 0.24f * width;
        table.Columns[0].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Columns[1].Width = width * 0.21f * width;
        table.Columns[1].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Columns[2].Width = width * 0.24f * width;
        table.Columns[2].StringFormat = new PdfStringFormat(PdfTextAlignment.Left, PdfVerticalAlignment.Middle);
        table.Draw(page, new PointF(0, 10));



This is just a simple sample to show you how powerful is the tool. Not only loading text, images or grid, you can show lots of other data like
·         Adding Watermark
·         Drawing objects
·         Adding action to the links within the PDF
·         Adding Paging
·         Generating Barcodes
·         Merging more than one PDF into single PDF
·         Splitting one PDF into multiple
And many more…
One more feature about this component is providing Security to your PDF file means creating password protected PDF. Below is the code snippet to generate the password protected file.
doc.Security.KeySize = PdfEncryptionKeySize.Key128Bit;
        doc.Security.OwnerPassword = "pradeep";
        doc.Security.UserPassword = "123";
        doc.Security.Permissions = PdfPermissionsFlags.Print | PdfPermissionsFlags.FillFields;   

Note: One another important thing I would like to share with you is - This tool comes with the proper documentation along with sample demos which you can find in their website. It is a paid tool but the worth tool.
Conclusion
Using Spire.PDF, create you PDF files as per your requirement easily and efficiently. I shared some of its simple features, but there are lots more. So visit the website given below:
http://www.e-iceblue.com/Introduce/pdf-for-net-introduce.html#.U8pJYuOSzSl
Hope you like this article. Please share your comments whether it’s good or bad.


Tuesday, 5 August 2014

Easy way for taking data in pdf file from Visual Stdio


                                                                                                                                           Next

Work with Spire.PDF for creating and writing PDF files on any .NET applications
Introduction
Spire.PDF  is a professional PDF .NET library which is  designed for .NET developers in order to create, read, write, convert and print PDF Files from any .NET Platform fast and with a high quality performance in this way of user for taking data in PDF file. And in present lot of Big organization  using this product.
This software support  C#, VB.NET,ASP.NET, AND ASP.NET MVC
Platform: Microsoft visual studio 2010 Express.
Why we use Spire.PDF

Spire.PDF is very useful software for reading, writing and editing a dot net file without using any other software. Sometimes we have huge data in our project which we would like to save in pdf format and for this we have to use some external software. Spire.PDF solves such problems. It helps to convert the dot net file to PDF file on one click.

How to Install this Spire.PDF

Step(1): First download this software from this link.
Step(2): After download double click on software After that this window will open.
Here click on next



Step(3): In next step accept the terms and condition and click on next after that a window will open where you select a feature.
If you are using a free version then this window will  open



Otherwise this window will open


Step(4): After click on next it will ask for install and this window will open

Step(5): After that install this software and use this software.

How to use this product with code.
For using this feature we have to use libraries as shown below.
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;

These namespace are used for graphics.
Convert Text file to PDF format in C# .net
Converting a text file to PDF is very difficult in dot net. In organization they convert this text file to PDF format by using other external software.
But this is very difficult for users. For removing this problem, Spire.Pdf  make it very simple. Only we have to use some code for this.
Go to the below link and download the software.
Step(1):- First of all open your visual studio and select a console application.


Step(2):- Add library

This are some libraries which we have to use for generate PDF files
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;

Step(3):- Code

using System.Drawing;
using System.Drawing.Text;
using Spire.Pdf;
using Spire.Pdf.Graphics;
using Spire.Pdf.Graphics.Fonts;
using System.IO;


namespace ConvertTextFile_to_pdfFile
{
    class TextFile
    {
        static void Main(string[] args)
        {
            PdfDocument PDF = new PdfDocument();
            DrawText(PDF.Sections.Add());
            PDF.SaveToFile("pdfFile.pdf");
            PDF.Close();
            System.Diagnostics.Process.Start("pdfFile.pdf");
        }

        private static void WriteTextFile(PdfSection section)
        {
            string text = File.ReadAllText(@"..\..\test.txt");
            PdfPageBase page = section.Pages.Add();
            PdfFont font = new PdfFont(PdfFontFamily.Helvetica, 11);
            PdfStringFormat format = new PdfStringFormat();
            format.LineSpacing = 20f;
            float pageWidth = page.Canvas.ClientSize.Width;
            PdfBrush brush = PdfBrushes.LightSeaGreen;
            float y = 30;
            PdfStringLayouter textLayouter = new PdfStringLayouter();
            PdfStringLayoutResult result
                = textLayouter.Layout(text, font, format, new                         SizeF(pageWidth, 0));

            foreach (LineInfo line in result.Lines)
            {
                page.Canvas.DrawString(line.Text, font, brush, 0, y, format);
                y = y + result.LineHeight;
            }
        }
    }
}


Step(4):-  Image of PDF

C# program Selection Sorting

Selection sort is a straightforward sorting algorithm. This algorithm search for the smallest number in the elements array and then swap i...