Sunday, June 9, 2013

Creating PDF with ASP.Net MVC and RazorPDF

Update: I have written a new blog post about better approach to create a PDF with asp.net mvc- You can find that following location.- A Better Solution to create PDF with Rotativa and ASP.NET MVC

In this post we are going to learn how we can easily create PDF from ASP.Net application with the help of Razor PDF NuGet package.

About Razor PDF:


This NuGet package is created by Al Nyveldt It internally uses ITextSharp an open source PDF convertor library. RazorPDF uses Razor View engine to create iTextXML which is in tern used to produce PDF file. You can get more information about that at below link.

https://www.nuget.org/packages/RazorPDF

Example(Creating PDF with ASP.Net MVC):


So what we are we waiting for ? Let’s create a simple example. To create example first thing we need to a create and ASP.Net MVC application.
Share:

Horizontal and vertical scrollbar in HTML5 and CSS3

In this post we are going to learn how we can put horizontal and vertical scrollbar in HTML5 and css3. In earlier version of HTML and CSS if we have to put only horizontal or vertical scrollbar that was not possible. Because there was no such properties there in CSS to describe whether we have horizontal scrollbar or vertical scrollbar.

Scroll bar in earlier version of HTML and CSS3:


In earlier version of HTML and CSS we have to use overflow property to display scrollbar for a particular div tag and If we add scrollbar it will add both horizontal and vertical scrollbar even if there is no need for that.Following is a code for that.
Share:
Saturday, June 8, 2013

Rounded corner div with HTML5 and CSS3

I have been recently playing with HTML5 and CSS3 and it is really fun. I am learning lots of new features of HTML5. Rounded corner is one of them. I thought it will be a good idea to share that with my readers. So In this post we are going to learn how we can create rounded corner div with HTML5.

In earlier version of HTML and CSS, If we need rounded corner div then we all needs to use image and without image it was not really possible. But now with the HTML5 and CSS3, It is very easy to create round corner div.

How to apply round corner to div?


There is a new CSS3 declaration for that. Border-Radius, With this you apply round corners to boxes and div without using images for rounded corners.

Example:


Let’s create an example for rounded corner div. I have created an sample page with Visual Studio 2012.

Rounded Corner div html5 and css3
Share:

Entity Framework : There is already an open DataReader associated with this Command

Recently working on an application with entity framework 4.1 When I was retrieving data and updating data with same data context I was getting following error. It was working fine on another machine but on mine it was giving this error.

There is already an open DataReader associated with this Command.

After digging into the problem for some time I have found that I have not opened multiple active result set in my connection string. So what was happening when I retrieved data with entity framework there was an already opened data reader with that and after that I was updating data so we all know that an data reader is always have connected to particular connection. So it was not allowing me to update the data.
So I have added following attribute to my connection string.

“MultipleActiveResultSets=True”

Then I again test above scenario and it was working fine. It’s very easy. Hope you like it. Stay tuned for more..
Share:
Sunday, June 2, 2013

How to call ASP.Net page method with jQuery

In this blog post we are going to learn about how we can call asp.net page methods from jQuery.

What is ASP.Net Page Method?


Page methods are available from ASP.Net 2.0. It’s an alternative to web services in ASP.Net application. This methods are exposed at client side and you can call that page methods directly from JavaScript.It is an easy way to communicate asynchronously with the server using Ajax.

Creating a page method:


So for creating a page method let’s create a asp.net web empty web application.

Calling asp.net page methods with jquery
Share:
Thursday, May 23, 2013

How to sort a data table in C# with LINQ

One of friend today ask how we can sort data table based on particular column? So I thought it’s a good idea to write a blog post about it. In this blog post we learn how we can learn how we can sort database with LINQ queries without writing much more long code. So let’s write a code for that.

using System;
using System.Data;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            DataTable dataTable = CreateDataTalble();
            AddRowToDataTable(dataTable);
            Console.WriteLine("Before sorting");
            PrintDatable(dataTable);
            var Rows = (from row in dataTable.AsEnumerable()
                        orderby row["FirstName"] descending
                        select row);
            dataTable = Rows.AsDataView().ToTable();
            Console.WriteLine("==============================");
            Console.WriteLine("After sorting");
            PrintDatable(dataTable);
        }

        public static DataTable CreateDataTalble()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("FirstName", typeof(string));
            dt.Columns.Add("LastName", typeof(string));
            return dt;
        }
        public static void AddRowToDataTable(DataTable dt)
        {
            dt.Rows.Add("Jalpesh", "Vadgama");
            dt.Rows.Add("Vishal", "Vadgama");
            dt.Rows.Add("Teerth", "Vadgama");
            dt.Rows.Add("Pravin", "Vadgama");
        }

        public static void PrintDatable(DataTable dt)
        {
            foreach (DataRow row in dt.Rows)
            {
                Console.WriteLine(string.Format("{0} {1}",
                    row[0], row[1]));
            }
        }
    }
}

Share:
Wednesday, May 22, 2013

Replace line breaks in C#

In recent days I was working on a project want to replace \r\n to a new character but it was giving very strange behaviour. It was not replacing proper. Let’s create that scenario. I have written following code.
using System;

namespace WebApplication1
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string sample = @"This is string for replacing new
                                 line \r\n This may not work";
            Response.Write(sample.Replace(@"\r\n",@"<br />"));
        }
    }
}

If you see the above code looks like it should work fine. But when you run this in browser in some environment it working fine and in some environment it was not working fine. Just like below.

Share:

Support this blog-Buy me a coffee

Buy me a coffeeBuy me a coffee
Search This Blog
Subscribe to my blog

  

My Mvp Profile
Follow us on facebook
Blog Archive
Total Pageviews