Write PDF files using .Net

This item was filled under [ .Net ]

“iText is a library that allows you to generate PDF files on the fly” – using this library, one can easily write PDFs in .Net. To get started, download the library from http://www.lowagie.com/iText/ (or SourceForge.net).

After downloading the .dll file, add it as a reference in your application.

Sample code (C#.Net):

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using iTextSharp.text;
using iTextSharp.text.pdf;

namespace ConsoleApplication
{
class Program
{
static void Main(string[] args)
{
//1. Create a document obj.
Document myDoc = new Document(PageSize.A4.Rotate());

//2. Create a writer & stream that will write to the file
PdfWriter.GetInstance(myDoc, new FileStream("Em.pdf", FileMode.Create));

//3. Open document
myDoc.Open();

//4. Write content to the document
myDoc.Add(new Paragraph("Hello World!"));

//5. Close document
myDoc.Close();
}
}
}

Calculate time difference in hh:mm

This item was filled under [ SQL Server ]

A simple query that computes time difference between two dates and displays the result in “hh:mm” format.

-- 2008-08-10 00:00:04 < Start_Time
-- 2008-08-10 08:32:17 < End_Time
-- 8:32 << Output (HHMM_Duration)

SELECT Start_Time, End_Time,
CONVERT(VARCHAR, DATEDIFF(hour, Start_Time, End_Time)) + ':' + CONVERT(VARCHAR, DATEDIFF(minute, Start_Time, End_Time)%60) as HHMM_Duration
FROM Events

*The above query was executed on Microsoft SQL Server 2000.

Manage hostnames on local PC

This item was filled under [ Uncategorized ]

Every OS has a ‘hosts’ file that defines mapping of hostnames to IP address. The computer file is used to store information on where to find a node on a computer network, and is under the control of local computer’s administrator.

‘Hosts’ file can be helpful for blocking/redirecting hostnames and accelerating DNS resolution. It is used by variety of OS including Windows, Linux, MacOS, Symbian etc.

In Microsoft Windows NT/2000/XP/20003, its default location is:
%SystemRoot%\system32\drivers\etc\

If you open a hosts file using any text editor, it will look like:

# Copyright (c) 1993-1999 Microsoft Corp.
#
# This is a sample HOSTS file used by Microsoft TCP/IP for Windows.
#
# This file contains the mappings of IP addresses to host names. Each
# entry should be kept on an individual line. The IP address should
# be placed in the first column followed by the corresponding host name.
# The IP address and the host name should be separated by at least one
# space.
#
# Additionally, comments (such as these) may be inserted on individual
# lines or following the machine name denoted by a ‘#’ symbol.
#
# For example:
#
#      102.54.94.97     rhino.acme.com          # source server
#       38.25.63.10     x.acme.com              # x client host

127.0.0.1    localhost

To add more entries, simply add IP address followed by the hostname (as shown in the file) and your hostnames will be redirected to that IP address!

Wordpress: ‘Recent Comments’ snippet

This item was filled under [ PHP, WordPress ]

A little piece of code that I found on BlogOhBlog.com. However, the code wasn’t doing proper formatting since all comments exceeding the sidebar width ended like the normal ones, so I simply added “..” at the end of lengthy comments. Also modified the display format to:

<Author> (on <PostTitle>):
<Comment text>

The code snippet can be used to display recent comments in sidebar without having to install a plugin.

<?php

global $wpdb;

$sql = "SELECT DISTINCT ID, post_title, post_password, comment_ID,
comment_post_ID, comment_author, comment_date_gmt, comment_approved,
comment_type,comment_author_url,
comment_content AS com_excerpt
FROM $wpdb->comments
LEFT OUTER JOIN $wpdb->posts ON ($wpdb->comments.comment_post_ID =
$wpdb->posts.ID)
WHERE comment_approved = '1' AND comment_type = '' AND
post_password = ''
ORDER BY comment_date_gmt DESC
LIMIT 10";

$comments = $wpdb->get_results($sql);

$output = $pre_HTML;
$output .= "\n<ul>";

foreach ($comments as $comment)
{
if(strlen($comment->com_excerpt) > 30) $comment->com_excerpt = substr($comment->com_excerpt, 0, 30) . "..";

$output .= "\n<li>".strip_tags($comment->comment_author)
." (on <a href=\"" . get_permalink($comment->ID) ."\">".$comment->post_title."</a>): <br>" . "<a href=\"" . get_permalink($comment->ID) .
"#comment-" . $comment->comment_ID . "\">" . strip_tags($comment->com_excerpt)
."</a></li>";
}

$output .= "\n</ul>";
$output .= $post_HTML;
echo $output;

?>

List of Google bots

This item was filled under [ SEO/Adsense ]

Google uses several user-agents. Using the bot name, you can block access or track any user-agent easily.

  • Googlebot: crawl pages from web index and news index
  • Googlebot-Mobile: crawls pages for mobile index
  • Googlebot-Image: crawls pages for image index
  • Mediapartners-Google: crawls pages to determine AdSense content. Only used to crawl your site if AdSense ads are displayed on your site
  • Adsbot-Google: crawls pages to measure AdWords landing page quality. Only used if you use Google AdWords to advertise your site

Sending email through .Net

This item was filled under [ .Net ]

In .Net, you can easily send an email through MailMessage() class. Below is a little ConsoleApplication that shows how to send a simple email in .Net:

using System;
using System.Collections.Generic;
using System.Text;
using System.Net.Mail;

namespace ConsoleEmailApp
{
class Program
{
static void Main(string[] args)
{
MailMessage objMail = new MailMessage("<a href="mailto:from-email@domain.com">from-email@domain.com</a>", "<a href="mailto:to-email@domain.com">to-email@domain.com</a>");
SmtpClient objSMTP = new SmtpClient("smtp.host.com", 25);

objMail.Subject = "Sending email through .Net";
objMail.Body = "Hello World!";

objSMTP.Credentials = new System.Net.NetworkCredential("<a href="mailto:username@domain.com">username@domain.com</a>", "password");
try
{
objSMTP.Send(objMail);
Console.Write("Email sent!");
}
catch (Exception e)
{
Console.Write(e.Message.ToString());
}
}
}
}

Creating a Google Sitemap

This item was filled under [ SEO/Adsense ]

Google Sitemaps allows you to provide additional information about your pages, including when they were last updated, so that the crawler exactly knows when does the page need to be indexed again. It is also useful for websites that have content which may other wise be ignored, such as flash based navigation interfaces etc.

To create a sitemap:

  • Create an empty .xml file
  • Add the following on the top of the file
    <?xml version="1.0" encoding="UTF-8"?>
    <urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  • Add the following to the bottom of the file:
    </urlset>
  • For every URL on your web site, create an entry for URL as shown below:
    <url>
    <loc>http://www.example.com/</loc>
    <lastmod>2005-01-01</lastmod>
    <changefreq>monthly</changefreq>
    <priority>0.8</priority>
    </url>

    Except for <loc> tag, all other are optional

  • Once done, simply upload the sitemap on your site and then submit it to Google using Webmaster Tools

There are many third party tools available that can generate the sitemap for you. For more details, visit: http://code.google.com/sm_thirdparty.html

Get more relevant Ads!

This item was filled under [ SEO/Adsense ]

Often your page displays irrelevant advertisement specially when there are many topics on a single page. In such case, use Google’s section targeting technique. Implementation is very simple. You just need to place an HTML tag over the content that either needs more attention or needs to be ignored. Like Google states:

To implement section targeting, you’ll need to add a set of special HTML comment tags to your code. These tags will mark the beginning and end of whichever section(s) you’d like to emphasize or de-emphasize for ad targeting.

The HTML tags to emphasize a page section take the following format:

<!-- google_ad_section_start -->

<!-- google_ad_section_end -->

And to ignore some content, add a (weight=ignore) to the starting tag:

<!-- google_ad_section_start(weight=ignore) -->

*To learn more about section targeting, click here.

Get notified when Google crawls your website

This item was filled under [ PHP, SEO/Adsense ]

Ever wanted to know when Google crawls your website? Here is a simple PHP script which emails you whenever GoogleBot crawls your site.

<?php
if(strpos($_SERVER['HTTP_USER_AGENT'], 'Googlebot') !== false)
{
$email = 'you@domain.com';
mail($email,'Googlebot Alert', 'Googlebot has crawled your page: '.$_SERVER['REQUEST_URI']);
}
?>

Consuming a Web Service through Console application

This item was filled under [ .Net ]

In my last post, I created a basic web service using ASP.Net/C#. Now, I will simply consume it. The basic advantage of Web Services is that it can be consumed through any language/platform.

1. Create a new Console Application Project in .Net

2. After creating the project, right click your project name from the “Solution Explorer” and Add Web Reference.

3. Paste the URL of your Web Service (e.g. http://localhost:4601/EmService/Service.asmx), press GO. You can get the URL of web service from your browser (after running it). This will load your web service. Type a name in “Web reference name” text field (here in this example, I entered: MyService) and click “Add Reference.” You will see that your web reference has been added to Solution Explorer.

4. Now all you have to do is create an instance of web service class using the reference provided, and get set go! Below is the sample code:

static void Main(string[] args)
{
MyService.Service objSrv = new MyService.Service();
string strOutput = objSrv.HelloWorld();
Console.WriteLine(strOutput);
}

5. Run your application to see the output!

Page 2 of 6«12345»...Last »