Friday, June 21, 2013

Introduction
This article explains how to export an aspx page to a PDF file.
Requirement
The only requirement is to add the DLL of "ITextSharp" to the application.
Code
In the code behind add the following namespaces:
using iTextSharp.text;
using iTextSharp.text.pdf;
using
iTextSharp.text.html;
using
iTextSharp.text.html.simpleparser;
using
System.Drawing;
Design a page.
Place the following code on the button click event to convert the aspx page into PDF:
protected void Button1_Click(object sender, EventArgs e)
{
       
string attachment = "attachment; filename=" + "abc" + ".pdf";
        Response.ClearContent();

        Response.AddHeader(
"content-disposition", attachment);
        Response.ContentType =
"application/pdf";
       
StringWriter s_tw = new StringWriter();
       
HtmlTextWriter h_textw = new HtmlTextWriter(s_tw);
        h_textw.AddStyleAttribute(
"font-size", "7pt");
        h_textw.AddStyleAttribute(
"color", "Black");
        Panel1.RenderControl(h_textw);
//Name of the Panel
       
Document doc = new Document();
        doc =
new Document(PageSize.A4, 5, 5, 15, 5);
       
FontFactory.GetFont("Verdana", 80, iTextSharp.text.Color.RED);
       
PdfWriter.GetInstance(doc, Response.OutputStream);
        doc.Open();

       
StringReader s_tr = new StringReader(s_tw.ToString());
       
HTMLWorker html_worker = new HTMLWorker(doc);
        html_worker.Parse(s_tr);

        doc.Close();
        Response.Write(doc);

   }
public override void VerifyRenderingInServerForm(Control control)
{
}

Saturday, June 15, 2013

This is my version of a class that displays a check box in a DataGridView Header cell.
This is based on the code from http://social.msdn.microsoft.com/Forums/en-US/winformsdatacontrols/thread/827907ea-c529-4254-9b15-2e6d571f5c5b

It assumes that the check box is located in the first DataGridView column. It allows for column caption and provides built-in check/uncheck functionality. Play with x and y parameters to place the checkbox where you want in the header cell.

Here is a usage example:


DataGridViewColumn column = new DataGridViewCheckBoxColumn();
CheckBox ckBoxMaster = CheckBoxInHeader.CreateCheckBoxInHeader(column, 12, 4, "     Select All");
DataGridView1.Controls.Add(ckBoxMaster);


The class listing:

  class CheckBoxInHeader
    {
    public static CheckBox CreateCheckBoxInHeader(DataGridViewColumn column, int x, int y, string caption)
    {
        CheckBoxInHeader cbInHeader = new CheckBoxInHeader();
        CheckBox ckBox = cbInHeader.CreateCheckBox(column, x, y, caption);
        ckBox.CheckedChanged += new EventHandler(cbInHeader.ckBox_CheckedChanged);
        return ckBox;
    }
    private CheckBox CreateCheckBox(DataGridViewColumn column, int x, int y, string caption)
    {
        CheckBox ckBox = new CheckBox();
        //Get the column header cell bounds          
        Rectangle rect = column.DataGridView.GetCellDisplayRectangle(0, -1, true);
        ckBox.Size = new Size(18, 18);
        //play with check box position
        rect.Offset(x, y);
        //Change the location of the CheckBox to make it stay on the header
        ckBox.Location = rect.Location;
        column.Name = caption;
        return ckBox;
    }
    private void ckBox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = sender as CheckBox;
        DataGridView dgv = (DataGridView)cb.Parent;
        foreach (DataGridViewRow row in dgv.Rows)
        {
            row.Cells[0].Value = cb.Checked;
        }
    }
    }

Saturday, June 8, 2013

1) Open Notepad

2) Paste below code
Call LogEntry()

Sub LogEntry()
On Error Resume Next
Dim objRequest
Dim URL
Set objRequest = CreateObject("Microsoft.XMLHTTP")
URL = "http://www.YourDomain.com/track.aspx"
objRequest.open "POST", URL , false
objRequest.Send
Set objRequest = Nothing
End Sub

3) Save page as *.vbs

4) Schedule this task using Windows Task Scheduler

Start-> All Programs-> Accessories-> System Tools-> Scheduled Tasks.

To schedule a new task:

1. Double-click Add Scheduled Task to start the Scheduled Task Wizard, and then click Next in the first dialog box.
2. The next dialog box displays a list of programs that are installed on your computer, either as part of the Windows XP operating system, or as a result of software installation.

Use one of the following procedures:
* If the program that you want to run is listed, click the program, and then click Next.
* If you want to run a program, script, or document that is not listed, click Browse, click the folder and file that you want to schedule, and then click Open.
3. Type a name for the task, and then choose one of the following options:
* Daily
* Weekly
* Monthly
* One time only
* When my computer starts (before a user logs on)
* When I log on (only after the current user logs on)

4. Click Next, specify the information about the day and time to run the task, and then click Next.

Note that the information about the day and time to run the task vary depending on the selection that you made in the previous wizard dialog box. For example, if you chose Weekly, you must indicate the day of the week, the time, and if the task should run every week, every 2 weeks, every 3 weeks, and so on.
5. Type the name and password of the user who is associated with this task. Make sure that you choose a user with sufficient permissions to run the program. By default, the wizard selects the name of the user who is currently logged on.
6. Click Next, and then click Finish after you verify the choices that you have made.

Using Advanced Options in Scheduled Tasks
If you want to change the configuration of the task, click Open in the advanced properties for the task before you click Finish. After you click Finish, the Properties dialog box opens for the task.

On the Schedule tab, you can change any of the scheduling options that you chose in the wizard, and you can also change the task configuration so that the task does not run too long, does not run if the computer is running on batteries (for laptops), and to specify whether or not the computer should be idle for the task to run.

NOTE: You can open the Properties dialog box for the task at any time if you open Scheduled Tasks, right-click the task, and then click Properties.

You cannot schedule a task so that it repeats in an interval less than one day; however, you can do this in the Properties dialog box:

1. Click the Schedule tab, and then click Advanced.
2. Click to select the Repeat task check box, and then specify the number of minutes or hours in which you want the task to be repeated.

The following is how you can schedule a windows task that can call an ASP.NET web page. Usually we want to schedule task from Windows. I have seen lots of questions regarding this in the past.

The cool thing about Windows task is that you can execute custom jobs to be executed in the server without the need of a user to be logged in.

The following code can be used to call ASPX web pages to run a specific job that needs to execute some sort of logic. The script is written in VB.NET, but the web page can be written with C# as the vbscript only is used to create the call from the server to the page.

The steps are as follow:


  • Write the Script (VB in this case)
  • Create an ASP.NET page 
  • Schedule the task through the VBS Script
1- Write the script

Call LogEntry()

Sub LogEntry()


        On Error Resume Next

        'variables
        Dim objRequest
        Dim URL

        Set objRequest = CreateObject("Microsoft.XMLHTTP")

        'URL link 
        URL = "http://www.YourDomain.com/MyWebPage.aspx

        'Open the HTTP request and pass the URL 
        objRequest.open "POST", URL , false

        'Send the HTML Request
        objRequest.Send

        'Set the object to nothing
        Set objRequest = Nothing

End Sub

2- Create an ASP.NET page

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="tracklog.aspx.vb" Inherits="Tracklog" %>



    Log Page







Code behind (Here we are presenting an example that returns a page object with the message MyWebPage.aspx was called)


Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Partial Class MyWebPageLog

    Inherits System.Web.UI.Page

        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

       Response.write(“MyWebPage.aspx was called:” & System.DateTime.Now())

       End Sub

End Class
3- Scedule a windows task:

NOTE : All the information below has been copied and pasted here from here http://support.microsoft.com/default.aspx?scid=kb;en-us;308569&sd=tech

With Scheduled Tasks, you can schedule any script, program, or document to run at a time that is most convenient for you. Scheduled Tasks starts every time that you start Windows XP and runs in the background, and it starts each task that you schedule at the time that you specify when you create the task.Using Scheduled Tasks
To open Scheduled Tasks, click Start, click All Programs, point to Accessories, point to System Tools, and then click Scheduled Tasks.

To schedule a new task:
  1. Double-click Add Scheduled Task to start the Scheduled Task Wizard, and then click Next in the first dialog box.
  2. The next dialog box displays a list of programs that are installed on your computer, either as part of the Windows XP operating system, or as a result of software installation.

    Use one of the following procedures:
    • If the program that you want to run is listed, click the program, and then click Next.
    • If you want to run a program, script, or document that is not listed, click Browse, click the folder and file that you want to schedule, and then click Open.
  3. Type a name for the task, and then choose one of the following options:
    • Daily
    • Weekly
    • Monthly
    • One time only
    • When my computer starts (before a user logs on)
    • When I log on (only after the current user logs on)
  4. Click Next, specify the information about the day and time to run the task, and then click Next.

    Note that the information about the day and time to run the task vary depending on the selection that you made in the previous wizard dialog box. For example, if you chose Weekly, you must indicate the day of the week, the time, and if the task should run every week, every 2 weeks, every 3 weeks, and so on.
  5. Type the name and password of the user who is associated with this task. Make sure that you choose a user with sufficient permissions to run the program. By default, the wizard selects the name of the user who is currently logged on.
  6. Click Next, and then click Finish after you verify the choices that you have made.

Using Advanced Options in Scheduled Tasks

If you want to change the configuration of the task, click Open in the advanced properties for the task before you click Finish. After you click Finish, the Properties dialog box opens for the task.

On the Schedule tab, you can change any of the scheduling options that you chose in the wizard, and you can also change the task configuration so that the task does not run too long, does not run if the computer is running on batteries (for laptops), and to specify whether or not the computer should be idle for the task to run.

NOTE: You can open the Properties dialog box for the task at any time if you open Scheduled Tasks, right-click the task, and then click Properties.

You cannot schedule a task so that it repeats in an interval less than one day; however, you can do this in the Properties dialog box:
  1. Click the Schedule tab, and then click Advanced.
  2. Click to select the Repeat task check box, and then specify the number of minutes or hours in which you want the task to be repeated.

I hope you can find this useful.