-->

Sunday, February 16, 2014

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

Saturday, February 15, 2014

Insertion in a unordered LINKED LIST for Data Structure in 'C'

Insertion in a unordered LINKED LIST

Here in this case the NEW node is inserted after the location LOC. LOC is found by comparing the given Information with every node of the Linked List. For example consider the following Linked List. ROOT points to FIRST node with information 10. If a NEW node is to be inserted after a node with Information   20, 20 is given as Information.20 is compared with the information of first node, it is not equal, so it is compared with the second node it is equal hence address of second node is LOC. Now node is inserted by copying address of third node which is given by LINK of LOC, in the LINK field of NEW node and the LINK field of LOC is copied with the new node’s address given by NEW. So after the second node of the original list there comes the just inserted NEW node. When the Linked List is traversed after the second node there comes the new node as the third node. If the Information is printed it will be 10 20 15 5.



Algorithm to Insert node in unordered linked list:

            INSERTNOLL(ROOT,IN)
            [IN is the information of the node after which insertion is done]
            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 to be data stored in the NEW node]

             LOC<--ROOT
             Repeat While IN< >LOC-->INFO AND LOC< >NULL
               LOC<--LOC-->LINK
             [End of While]
             If LOC = NULL Then:
                Write: ‘Error in Insertion’
                [Error because node with IN is not present]
                Exit.
              Else
                NEW-->LINK<--LOC-->LINK
                LOC-->LINK<--NEW
             [End of If]
             Exit.


Friday, February 14, 2014

How to Create and Apply Templates to Controls Dynamically

We already discuss about create template in .aspx page. Today we will learn, create template dynamically (using class). There are lots of steps to follow
Step-1 : Create a MyTemplate class , which is implement from ITemplate interface

public class MyTemplate : ITemplate
{
}

Here ITemplate interface create ASP.NET template.

Step-2 : Declare ListItemType enum variable in the class

ListItemType templateType;

Step-3 : Initialize variable in class constructor, which you want to take in Repeater control like HeaderTemplate, Item Template, Footer Template etc.

public MyTemplate(ListItemType type)
    {
        templateType = type;
    }

Step-4 : Define InstantiateIn method ( for more detail use link ) with proper template view
Step-5 : Bind Repeater Control through DataBinder class.

Full MyTemplate.cs file code


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

/// <summary>
/// Summary description for MyTemplate
/// </summary>
public class MyTemplate :ITemplate
{
    ListItemType templateType;
    public MyTemplate(ListItemType type)
    {
        templateType = type;
    }

    public void InstantiateIn(Control container)
    {
        PlaceHolder ph = new PlaceHolder();
        Label item1 = new Label();
        Label item2 = new Label();
        item1.ID = "item1";
        item2.ID = "item2";

        switch (templateType)
        {
            case ListItemType.Header:
                ph.Controls.Add(new LiteralControl("<table border=\"1\">" +
                    "<tr><td><b>sno</b></td>" +
                    "<td><b>Name</b></td></tr>"));
                break;
            case ListItemType.Item:
                ph.Controls.Add(new LiteralControl("<tr><td>"));
                ph.Controls.Add(item1);
                ph.Controls.Add(new LiteralControl("</td><td>"));
                ph.Controls.Add(item2);
                ph.Controls.Add(new LiteralControl("</td></tr>"));
                ph.DataBinding += new EventHandler(Item_DataBinding);
                break;
            case ListItemType.AlternatingItem:
                ph.Controls.Add(new LiteralControl("<tr bgcolor=\"lightblue\"><td>"));
                ph.Controls.Add(item1);
                ph.Controls.Add(new LiteralControl("</td><td>"));
                ph.Controls.Add(item2);
                ph.Controls.Add(new LiteralControl("</td></tr>"));
                ph.DataBinding += new EventHandler(Item_DataBinding);
                break;
            case ListItemType.Footer:
                ph.Controls.Add(new LiteralControl("</table>"));
                break;
        }
        container.Controls.Add(ph);
    }
    static void Item_DataBinding(object sender, System.EventArgs e)
    {
        PlaceHolder ph = (PlaceHolder)sender;
        RepeaterItem ri = (RepeaterItem)ph.NamingContainer;
        Int32 item1Value = (Int32)DataBinder.Eval(ri.DataItem, "sno");
        String item2Value = (String)DataBinder.Eval(ri.DataItem, "Name");
        ((Label)ph.FindControl("item1")).Text = item1Value.ToString();
        ((Label)ph.FindControl("item2")).Text = item2Value;
    }


Step-6 : Take a Web Form (.aspx) page, place Repeater control on it.
Step-7 : Bind Repeater control on Page_Load event.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using System.Data.SqlClient;
using System.Data;

public partial class Default3 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       SqlConnection conn =new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);

        SqlDataAdapter DA;
        DataSet DS;

        DA = new SqlDataAdapter("SELECT * FROM [userdata]", conn);
        DS = new DataSet();

        Repeater1.HeaderTemplate = new MyTemplate(ListItemType.Header);
        Repeater1.ItemTemplate = new MyTemplate(ListItemType.Item);
        Repeater1.AlternatingItemTemplate =new MyTemplate(ListItemType.AlternatingItem);
        Repeater1.FooterTemplate = new MyTemplate(ListItemType.Footer);
        DA.Fill(DS, "userdata");
        Repeater1.DataSource = DS.Tables["userdata"];
        Repeater1.DataBind();
    }
}

Code Generate the following output

How to Create and Apply Templates to Controls Dynamically

Thursday, February 13, 2014

How to Create Data Model using Database in MVC Application

As I have discussed in earlier article, MVC model folder have all the classes that may be used for application logic in the application. By default an MVC application have some of the models like AccountModels, LogOnModels, RegisterModels etc.

We will create a new model class by using some simple steps.

  • Right click on the Model folder and add a new class named Student.cs
  • Insert some properties in that class like written below.

namespace MvcApplication1.Models
{
    public class Student
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
        public string City { get; set; }
    }
public class StudentContext: DbContext
{
public DbSet<Student> Students { get; set; }
}
}

This class have all the four properties which are the columns in the student table of the database. Now to work with this class we have to add a controller (after debugging this application) class in the Controllers folder with these simple steps.

  • Right click on the Controller folder and add a new Controller. A window will appear with some of the options to be inputted.
  • Name the controller StudentController and more options like shown in the window.

How to Create Data Model using Database in MVC Application


After clicking on Add button visual web developer will add StudentController.cs file under the Controller folder, and Student folder under the Views folder.

The last steps of this article is to add Database views which are used for UI purpose in the application. There is a plus point in this MVC application i.e. we don’t need to add required views because they are automatically added by the visual web developer.

Lookout under the Views>Student Folder

  • Index.cshtml
  • Create.cshtml
  • Delete.cshtml
  • Details.cshtml
  • Edit.cshtml


© Copyright 2013 Computer Programming | All Right Reserved