Showing posts with label C#. Show all posts
Showing posts with label C#. Show all posts

Monday, November 15, 2010

String Manipulation in .NET

String is one of the most frequently used data types in programming. There are quite a few string functions out there and all of those functions are implemented differently in different programming languages. I am going to show here how this is done in .NET Framework.
In the example below I am going to demonstrate the use of StringBuilder class. The below example receives a string as a parameter and then reverses it:
private string ReverseString(String str)
{
    if (str != string.Empty)
    {
        StringBuilder sb = new StringBuilder();
        for (int i = str.Length - 1; i >= 0; i--)
        {
            sb.Append(str.Substring(i, 1));
        }
        str = sb.ToString();
    }
    return str;
}



Here is another example that accepts a string, splits it, and then reverses the order of words in that string:
private void ReOrderString()
{

string str = "Token A#$%@&Token B#$%@&Token C#$%@&Token D#$%@&Token E"; 
string[] Split = str.Split(new string[] { "#$%@&" }, StringSplitOptions.None);
    StringBuilder sb = new StringBuilder();
    for (int i = Split.Length - 1; i >= 0; i--)
    {
        sb.Append(Split[i]);
        if (i > 0)
            sb.Append("#$%@&");
    }
    lblOriginalString.Text = "Token A#$%@&Token B#$%@&Token C#$%@&Token D#$%@&Token E";
    lblProcessedString.Text = sb.ToString();
}

Thursday, January 14, 2010

ASP.NET AJAX Date Picker

    Recently, while working on my Tai Chi club’s website, I realized that my club’s website is lacking the capability of scheduling a Tai Chi class (or any other type of event for that matter). In order to schedule any type of event, I would need to specify that event’s date and time, location, and maybe some kind of description or explanation of what’s going to be happening during that event. So, I started looking at different ways to create a Date Picker. After some research, I found out that it is very easily achieved with Ajax Control Toolkit, which provides CalendarExtender control.

     The CalendarExtender control is designed to extend a TextBox. Its default behavior prompts the end user to pick a date from a calendar when the user clicks inside a textbox associated with the CalendarExtender.

This is what it looks like:
It is also possible to add a button to the right of the textbox and have the calendar popup whenever the user clicks that button (all you need to do is specify the button's ID as value of PopupButtonID property).

To customize the look of the popup calendar, add all or some of the following classes to your stylesheet and customize them appropriately:

To achieve the look above, I added the following class to my stylesheet:

.ajax__calendar_active
{
    border: solid 2px green;
   font-weight: bold;
}

and then in my page’s Page_Load event added the following line of code:

this.CalendarExtender1.SelectedDate = DateTime.Today;

.ajax__calendar_container
you can change the style of outer container of calendar control. Outer container holds the .ajax__calendar_header, .ajax__calendar_body, .ajax__calendar_footer as child container css classes.

.ajax__calendar_header
This css class holds the previous arrow, next arrow and current Month, Year. Child Css Classes are .ajax__calendar_prev, .ajax__calendar_next, .ajax__calendar_title.

.ajax__calendar_prev
This class is associated with an element that displays the left side arrow to view the previous month, year or decade based on the current view of calendar.

.ajax__calendar_title
This class is associated with an element that displays the month name, year or decade range according to the current view of the calendar control.

.ajax__calendar_next
This class is associated with an element that displays the right side arrow to view the next month, year or decade based on the current view of calendar.

.ajax__calendar_body
It is a container of the elements for displaying the different views of calendar such as month view, year view and day view. This class holds the .ajax__calendar_days, .ajax__calendar_months and .ajax__calendar_years as child css classes

.ajax__calendar_days
It changes the look of day view of month. Child Css Classes are .ajax__calendar_dayname and .ajax__calendar_day.

.ajax__calendar_dayname
It changes the look of day name at the top of day view of month.

.ajax__calendar_day
This class is associated with the day layout of month.

.ajax__calendar_months
It controls the month layout of month view. Child Css class is .ajax__calendar_month.

.ajax__calendar_month
It controls the look and feel of month names of month view.

.ajax__calendar_years
This class controls the layout of year view of calendar control. Child css class: .ajax__calendar_year.

.ajax__calendar_year
It changes the look and feel of years in the year layout of year view.

.ajax__calendar_footer
It is associated with Footer container that holds the current date.

.ajax__calendar_today
It displays the current date in the footer container of calendar control.

.ajax__calendar_hover
Dynamic color behavior onmouseover can be modified by using this class. It can be used with .ajax__calendar_title, .ajax__calendar_day, .ajax__calendar_month, .ajax__calendar_year, .ajax__calendar_today to change the mouse over behavior.

.ajax__calendar_active
It sets the style of currently selected date.

.ajax__calendar_other
It sets the style of day, month name or year that is outside the current view e.g. days that don’t belong to the currently visible month.

Thursday, March 29, 2007

Converting ASP.NET to PDF


I probably wouldn't be wrong if I said that most developers every now and then are tasked to present specific data contained in a webpage as a PDF document. There are two ways to do it:

  1. using the functionality of CrystalReportViewer's toolbar - this toolbar provides a button, which does exactly that. However, to use this approach you would have to actually display the crystal report on the page.

  2. programmatically convert the Crystal Report object into a PDF document using the Crystal Reports API

Below is the code for the second approach:

Dim report As CrystalReport1 = New CrystalReport1
SqlDataAdapter1.Fill(DataSet11)
report.SetDataSource(DataSet11)
report.ExportToHttpResponse _
(ExportFormatType.PortableDocFormat, _
Response, _
False, _
"ExportedReport")


Tuesday, March 27, 2007

Crystal Reports with ASP.NET

When it comes to developing a report for an ASP.NET application, one might find that there is very little information available on how it's done using Crystal Reports.

Visual Studio 2003 comes with a built-in version of Crystal Reports (CR 9). Using that built-in version of Crystal Reports might save you some money, since it's a part of VS 2003, however, from the Crystal Reports design prospective it is much more convenient to use a stand-alone version of Crystal Reports.

The latest version of Crystal Reports at the time of writing this post is Crystal Reports XI, which provides some new features, not available in previous versions (see the www.businessobjects.com website for more info), so in my last development I used that version of Crystal Reports.

In order for the report to display as a webpage, VS 2003 provides CrystalReportViewer control. This is how it works:

Instantiate the DataSet object

Dim ds As New DataSet

ds = GetDataForReport() 'some method that retrieves data for the report

Instantiate the ReportDocument object:

Dim rpt As New ReportDocument

'set the ReportObject as the report you want to display
rpt = New CrystalReport1
'Define the datasource of your report
rpt.SetDataSource(ds)

'define your report as a reportsource for the crystalreportviewer
CrystalReportViewer1.ReportSource = rpt

Below is C# version of this code:

DataSet ds = new DataSet();
ReportDocument rpt = new ReportDocument();
rpt = new CrystalReport1();
rpt.SetDataSource(ds);
CrystalReportViewer1.ReportSource = rpt;


In order for this code to work, the following namespaces must be imported/used:

CrystalDecisions.CrystalReports.Engine
CrystalDecisions.Shared
CrystalDecisions.Web.Design

The following namespaces should be used only if you are planning to convert the report into a PDF, Word, or Excel format.

CrystalDecisions.Shared.ExportDestinationType
CrystalDecisions.Shared.ExportFormatType
CrystalDecisions.Shared.ExportDestinationOptions