Thursday, March 3, 2011

Telerik MVC Grid - Export to PDF VB.NET

I was thinking since last few weeks to do this task, finally I got time and success. I saw some post saying that "How to export grid data to PDF". This is what I have done as per my understanding.

Version Info

a) Visual Studio 2010

b) ASP.NET MVC 2 ( Framework 4)

c) MS Sql 2005

d) Telerik ASP.NET MVC Components (2010.3.1116.235)

c) itextsharp (5.0.5.0)

Steps

1. Create New Telerik Project; I named it GridToPdfExport.

2. I used standard northwind database.

3. Model

a) create a new dbml file; say DbContext.dbml; drag and drop customer table to this file.

b) Add ICustomer interface. I have declared only one method i.e. FindAllCustomers()

c) Implement interface to SqlCustomers.vb

3. Controller

a) use the standard home controller. As I am going to use ajax binding; I wrote _Index method.

4. View

a) In "Home" folder; index.aspx use the telerik grid. I used the ajax binding and wrote _Index method in Home Controller ( Step 3, point (b))

b) In Telerik Grid; you have to define export to pdf custom button just like we used to do it when we want to export data to excel / csv. And write the method in the same way. There will be a change in that method.

c) ExportToPdf Method

1. Signature

Public Function ExportToPdf(ByVal page As Integer, ByVal groupBy As String, ByVal orderBy As String, ByVal filter As String) As ActionResult

2. Get the data

Dim customers = _repository.FindAllCustomers()
customers = customers.ToGridModel(1, customers.Count(), groupBy, orderBy, filter).Data

3. Create of a document-object with pagesize and margin ( this will be from itextsharpe )

Dim document As Document
document = New Document(PageSize.A4.Rotate, 10, 10, 10, 10)

4. Create a memory stream that listens to the document.

Dim output As New MemoryStream()
pdf.PdfWriter.GetInstance(document, output)

5. Open the document

document.Open()

6. Add content to the document

Dim numOfColumns As Integer = 8
Dim dataTable As pdf.PdfPTable
dataTable = New pdf.PdfPTable(numOfColumns)
dataTable.DefaultCell.Padding = 3
dataTable.DefaultCell.BorderWidth = 2
dataTable.DefaultCell.HorizontalAlignment = Element.ALIGN_CENTER

7. Add headers

dataTable.AddCell("Customer Id")
dataTable.AddCell("Contact Name")
dataTable.AddCell("Company Name")
dataTable.AddCell("Address")
dataTable.AddCell("City")
dataTable.AddCell("Phone")
dataTable.AddCell("Postal Code")
dataTable.AddCell("Region")

dataTable.HeaderRows = 1
dataTable.DefaultCell.BorderWidth = 1

For Each cust In customers
dataTable.AddCell(cust.CustomerID)
dataTable.AddCell(cust.ContactName)
dataTable.addCell(cust.CompanyName)
dataTable.AddCell(cust.Address)
dataTable.AddCell(cust.City)
dataTable.AddCell(cust.Phone)
dataTable.AddCell(cust.PostalCode)
dataTable.AddCell(cust.Region)
Next

8. Add table to the document

document.Add(dataTable)

9. Close the document don’t forget this

document.Close()

10. Return file as ajax response

Return File(output.ToArray(), "application/pdf", "CustomerData.pdf")

That’s it. Your data will be exported in pdf.

Monday, July 12, 2010

Exchange 2007 Error - 451 4.4.0 DNS Query Failed

Hi Guys,

I was facing a problem in sending mails using Exchange 2007. In queue viewer I could see the 451 4.4.0 DNS Query failed error. After spending some time on google to solve this problem; I have followed the steps mentioned below;

Setup: behind firewall, windows 2003 standard edition server and Exchange 2007.
I am not using any smart host to send the mail.

1. Check that the Send Connector (Organization Configuration -> Hub Transport -> Send Connectors) created as "Internet" in Intended type of usage.
2. In Organization Configuration -> Hub Transport -> Send Connectors, right click on the created send connector;
=> Go To the "Network" tab.
=> 1. There "Use domain name system (DNS) "MX" record to route mail automatically" should be selected;
2. Keep "Enable Domain Security (Mutual Auth TLS)" unchecked.
3. Check "Use the External DNS lookup settings on transport server"
3. Go to the Server Configuration -> Hub Transport
=> Right click on exchange server -> properties
=> click on "External DNS Lookups"
=> click on "Use these DNS Servers"
=> Here, add internal as well as your ISPs DNS.
=> If "Use these DNS Server" is already clicked; check the added DNS IPs and confirm that added DNSs are reachable. (PING those DNS IPs)
4. Also make sure if you are behind the firewall; 53 port is opened for DNS Lookups.
5. I have also executed command "dnscmd /Config /EnableEDnsProbes 0" on command prompt.

Many Thanks to

This may solve your problem.

Have a nice Time..