Search This Blog

Wednesday, April 9, 2014

DateTime Compare C# Example

C# > System Namespace > DateTime > Compare

Compares two dates of type DateTime.

Example


DateTime date1 = new DateTime(2014, 4, 1, 0, 0, 0);
DateTime date2 = new DateTime(2014, 5, 1, 0, 0, 0);
int cmp = DateTime.Compare(date1, date2);
if (cmp < 0)
MessageBox.Show("date1 is earlier than date2");
else if (cmp == 0)
       MessageBox.Show("date1 is equal to date2");
else
       MessageBox.Show("date1 is later than date2");





Tuesday, April 8, 2014

Check if a date is weekend day C#

C# > System Namespace > DateTimeDayOfWeek

The DayOfWeek enumeration represents the day of the week.
Its value ranges from zero (DayOfWeek.Sunday) to six (DayOfWeek.Saturday).

Example: Check if a date is weekend day

DateTime date1 = DateTime.Now;
           
if ((date1.DayOfWeek == DayOfWeek.Saturday ||  date1.DayOfWeek == DayOfWeek.Sunday ))
{
  // action

}






DateTime Structure C#

C# > System NamespaceDateTime 

DateTime structure represents an instant in time.




Example


  DateTime dt= DateTime.Now;




Days In Month C# Example

C# > System Namespace > DateTime > DaysInMonth 

Returns the number of days in the specified year and month.

Example

int daysInMonth = System.DateTime.DaysInMonth(2014, 4);




Friday, March 28, 2014

ASP.NET System Web UI HtmlControls

ASP.NET > System.Web.UI.HtmlControls

Contains classes that allow you to create HTML server controls on a Web Forms page, so it allows you to programmatically control the HTML elements on a Web Forms page.





Create dynamic table runtime ASP.NET

ASP.NET > System.Web.UI.HtmlControls > HtmlTable 

HtmlTable allows programmatic access on the server to the HTML
element.


Example: Create dynamic table runtime ASP.NET

HTML:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication2.WebForm1" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:Panel ID="Panel1" runat="server">
        </asp:Panel>
   
    </div>
    </form>
</body>
</html>






Code:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;

namespace WebApplication2
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            HtmlTable dTable = new HtmlTable();
            dTable.Border = 1;
            int tRow; 
            int tCell; 
            for (tRow = 0; tRow < 10; tRow++) 
            { 
                HtmlTableRow dTRow = new HtmlTableRow(); 
                for (tCell = 0; tCell < 5; tCell++) 
                { 
                    HtmlTableCell dTCell = new HtmlTableCell(); 
                    dTCell.InnerText = "Row: " + Convert.ToString(tRow + 1) + " Col: " + Convert.ToString(tCell + 1); 
                    dTRow.Controls.Add(dTCell); 
                } 
                dTable.Controls.Add(dTRow); 
            } 
            Panel1.Controls.Add(dTable); 

        }
    }
}




C# Keywords

C# > Keywords

Keywords are reserved identifiers used by compiler. 


  • cannot be used as identifiers in your program unless they include @ as a prefix (@then is a valid identifier)
  • contextual keywords have special meaning only in a limited program context and can be used as identifiers outside that context
Literal keywords
    Access keywords
      Access modifiers
      Operator
        Method
        Contextual