Working with System.DateTime – Date and Time String Formatting with C#

Working with System.DateTime – Date and Time String Formatting with C#

Software Development with C# needs to work with dates and times. To work efficiently with Date and Time .NET has the System.DateTime (DateTime) namespace to help us. You can use DateTime to get DateTime values, generate new DateTime values and format DateTime values (parse strings into DateTime or DateTime in the string format).

Creating New DateTime Objects with C#

The default value of DateTime is Jan 1, 0001 at 12:00 midnight.

DateTime newDate = new DateTime();
Console.WriteLine("date: {0}", newDate); //date: 1/1/0001 12:00:00 AM

Creating New DateTime through the constructor

You can initialize the DateTime through the constructor using several pre-define options.

System.DateTime newDate = new System.DateTime(2010, 9, 5, 21, 35, 15, 777);
Console.WriteLine("date: {0}", newDate);//date: 9/5/2010 9:35:15 PM

If you need the current date and time? DateTime.Now will do it for you.

System.DateTime newDate = System.DateTime.Now;
Console.WriteLine("date: {0}", newDate);// Current Date Time will be display

Date and Time String Formatting with C#

The date and time value can be represented by either a DateTime or a DateTimeOffset value. Date and Time String Formatting fall into two categories:

  • Standard Date and Time String Formatting
  • Custom Date and Time String Formatting

Continue reading “Working with System.DateTime – Date and Time String Formatting with C#”

GridView Custom Paging in ASP.NET 3.5 with SQL Server Stored Procedure

GridView Custom Paging in ASP.NET 3.5 with SQL Server Stored Procedure

GridView — Displays a set of data items in an HTML table. ASP.NET GridView control enables you to display, sort, page, select, and edit data.

Default gridview paging works best when you deal with limited pages. If there are more pages then, the performance suffers. In this case direct jump to desire page is a good alternative.

I have deal with a problem on GridView while working on user control (.ascx). I have a user control (.ascx) with GridView in it. I have used Stored Procedure to retrieve data. Bounding data to gridview display all the records and Default paging setting not work for me. Here Custom Paging helps me and only those database records that need to be displayed get retrieved. Using GridView Custom Paging solve the Issue for me.

Continue reading “GridView Custom Paging in ASP.NET 3.5 with SQL Server Stored Procedure”

Pause a Console Application for User Input

Pause a Console Application for User Input or Pause Command in a Console Application

While working with C# or VB.NET Console Applications, you may require to pause for the user input or display a message to the user. Console application window get exit as the program execution finished. Following Piece of code will do it for you.

C#.NET

using System;
using System.Collections.Generic;
using System.Text;
 
namespace Pause {
   class Program {
      static void Main(string[] args)
    {
     Console.WriteLine("Press any key to continue…");
     Console.ReadLine(); //Pause
    }
   }
}

Continue reading “Pause a Console Application for User Input”

Read XML data from a URL by using Visual C#

Read XML data from a URL by using Visual C#

C# has a powerful and flexible namespace System.Xml to manipulate XML. System.Xml namespace provides the XMLTextReader class to read XML (Extensible Markup Language) from a URL (Uniform Resource Locator).

Now we want to read xml file test.xml (http://localhost/test.xml) located at your localhost. You can also use www.tipsntracks.com site map at "http://www.tipsntracks.com/sitemap.xml". let us look at structure of test.xml, I am going to used.

Continue reading “Read XML data from a URL by using Visual C#”

Finding Last line in the text using regular expressions with .net

Finding Last line in the text using regular expressions with .net

In this regular expressions, we are going to find last line in the text.

Regular Expression Pattern

.+Z$

A description of the regular expression:

Any character, one or more repetitions
End of string or before new line at end of string
End of line or string

How It Works

This regular expression will check for Any character, one or more repetitions of End of string or before new line at end of string.

Continue reading “Finding Last line in the text using regular expressions with .net”