-->

Monday, February 17, 2014

Datetime difference in days in C# Programming

Source Code 

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

//Code behind code

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

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime currenttime = DateTime.Now;

    

        DateTime after14days = currenttime.AddDays(14);

        TimeSpan ts = after14days - currenttime;


        Label1.Text += "current Date and Time is " + currenttime.ToString () + "<br/>";
        Label1.Text += "After 14 days Date and time are " + after14days+"<br/>";
        Label1.Text += "Remaining Days are" + ts;

          
    }
}

Code generate the following output

Datetime difference in days in C# Programming

Deletion Opration in Linked List for Data Structure in C Programming

Deletion
Removing a node from the linked list is called deletion. Get the Information of the node, which is to be     removed. Compare the Information with the INFO of ROOT . If it is equal update ROOT with LINK of ROOT, otherwise set pervious location PLOC with ROOT and location LOC with LINK of ROOT. Compare the information with that of INFO of LOC, if it is not equal then copy LOC in PLOC and update LOC with LINK of LOC. If the node is found then come out and set LINK of PLOC with LINK of LOC, and the deletion is over. Add the deleted node to Free Pool, by assigning LINK of LOC with AVAIL and AVAIL with LOC.
       If the PLOC is assigned with LOC, then when the list is traversed automatically the node with address LOC is skipped, that is why PLOC is used in this case.

         For example consider the below linked list:


       If a node with information 20 is to be deleted, the operation is done as shown below:


       The original list after deletion of the node with information 20 is shown e:


Algorithm to delete a node from the Linked List:

DELETIONLL(ROOT, INFO)
If INFO=ROOT-->INFO Then:
LOC<--ROOT
ROOT<--ROOT-->LINK
Else:
 PLOC<--ROOT
 LOC<--ROOT-->LINK
Repeat While LOC< >NULL AND INFO< >LOC-->INFO
 PLOC<--LOC
 LOC<--LOC-->LINK
[End of While]
PLOC--> LINK<-- LOC-->LINK
[End of If]
[Following statements to add deleted node to free pool]
 LOC-->LINK<--AVAIL
AVAIL<--LOC
Exit.


Sunday, February 16, 2014

DateTime difference in minutes in C# Programming

Source Code

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

// Code behind Code

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

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime currenttime = DateTime.Now;

        Label1.Text = "At Time : " + currenttime.ToString();

        DateTime Timeafter4minute = currenttime.AddMinutes(4);

        TimeSpan totalTime = Timeafter4minute - currenttime;

        int minute = (int)totalTime.TotalMinutes;
        
        Label1.Text += "<br ><br />after 4 minutes: ";
        Label1.Text += Timeafter4minute.ToString();

        Label1.Text += "<br ><br />minute,difference between to datetime object : ";
        Label1.Text += minute;  
    }
}

Code generate the following output

DateTime difference in minutes in C# Programming

Insertion in an ordered LINKED LIST Data Structure in C programmong

Insertion in an ordered LINKED LIST

Here in this case to find the location LOC, the list is traversed, by comparing the Information to be stored with the information of the nodes. First compare the NEW node information with that of the information of the first node. If it is less then the first node information, insert the new node as the first node by copying the address of new node NEW in ROOT before that copy ROOT in LINK of NEW. So initially set LOC with LINK of ROOT and another pointer variable TEMP with ROOT. Traverse till LOC < >NULL or till the Information of the NEW node less than the Information of any node by updating TEMP with LOC and LOC with LINK of LOC. Once the location LOC is found, copy LOC, the address of the node before which insertion is to be done in LINK of NEW and copy address of the previous node given by LINK of TEMP with NEW.

    For example consider the below Linked List in which the node are stored in ascending order.


If a NEW node is to be inserted with Information 15, first create the NEW node and store the Information part of it with 15 and initialize LINK with NULL. Compare 15 with information of first node i.e. 10. 15 is not less than 10. So set LOC with LINK of ROOT and TEMP with ROOT. Compare INFO of NEW node with INFO of node pointer by LOC. So 15 is not greater than 20, come out LOC is the location at which the NEW node is to be inserted. Copy LINK of NEW with LOC and copy LINK of TEMP with NEW. Insertion is over.


Algorithm to Insert node in an ordered linked list when the location LOC is not given:

NSERTOLL(ROOT)
If AVAIL=NULL Then:
  write: ‘Memory Allocation Error’
  Exit.
[End of If]
NEW<--AVAIL
AVAIL<--AVAIL-->LINK
NEW-->LINK<--NULL
NEW-->INFO<--Information
[Information is the data to be stored in node]
If NEW-->INFO < ROOT-->INFO Then
NEW-->LINK<--ROOT
ROOT<--NEW
Else:
TEMP<--ROOT
LOC<--ROOT-->LINK
Repeat While LOC< >NULL AND NEW-->INFO > LOC-->INFO
  TEMP<--LOC, LOC<--LOC-->LINK
[End of While]
NEW-->LINK<--LOC
TEMP-->LINK<--NEW
[End of If]
Exit.

DateTime difference in seconds in C# Programming

This code is useful, where you want to calculate time such as train traveling time. In previous example , we have already discussed about that

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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


// Code behind Model


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

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime currenttime = DateTime.Now;

        Label1.Text = "At Time : " + currenttime.ToString();

        DateTime Timeafter300second = currenttime.AddSeconds(300);

        TimeSpan totalTime = Timeafter300second - currenttime;

        int seconds = (int)totalTime.TotalSeconds;
        
        Label1.Text += "<br ><br />after 300 seconds: ";
        Label1.Text += Timeafter300second.ToString();

        Label1.Text += "<br ><br />seconds,difference between to datetime object : ";
        Label1.Text += seconds;  
    }
}

Code generate the following code

DateTime difference in seconds in C# Programming

DateTime difference in milliseconds in C# Programming

In my previous article , we have already discussed about that topic for train concept.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default5.aspx.cs" Inherits="Default5" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

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

// Code behind Code


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

public partial class Default5 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DateTime currenttime = DateTime.Now;

        Label1.Text = "At Time : " + currenttime.ToString();

        DateTime Timeafter5minute = currenttime.AddMinutes(5);

        TimeSpan totalTime = Timeafter5minute - currenttime;

        int milli = (int)totalTime.TotalMilliseconds;

        Label1.Text += "<br ><br />after 5 minutes: ";
        Label1.Text += Timeafter5minute.ToString();

        Label1.Text += "<br ><br />second in millieconds difference between to datetime object : ";
        Label1.Text += milli;  
    }
}

Code generate the following output


DateTime difference in milliseconds in C# Programming

Determine which web server control raised an event example in ASP.NET

In event handler use sender object for getting ID property of the button. Using the sender object you can determine Which object raised the event. Lets take an simple example

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default4.aspx.cs" Inherits="Default4" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
    </div>
    <asp:Button ID="Button1" runat="server" onclick="evnt_raise" Text="Button-1" 
        Width="79px" />
    <br />
    <br />
    <asp:Button ID="Button2" runat="server" onclick="evnt_raise" Text="Button-2" 
        Width="76px" />
    <br />
    <br />
    <asp:Label ID="Label1" runat="server"></asp:Label>
    </form>
</body>
</html>

// Code behind Code

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

public partial class Default4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void evnt_raise(object sender, EventArgs e)
    {
        Button b1;
        b1 = (Button)sender;
        switch (b1 .ID)
        {
            case "Button1":Label1 .Text ="Button 1 Pressed";
                break;
            case "Button2" : Label1 .Text ="Button 2 pressed";

                break;
        }
    }
}
Code generate the following output
Determine which web server control raised an event example in ASP.NET

Determine which web server control raised an event example in ASP.NET

© Copyright 2013 Computer Programming | All Right Reserved