-->

Saturday, April 26, 2014

How to use LINQ Set Operators in ASP.NET

Introduction

The Set operators in LINQ refer to the query operations that produce a result set. The result is based on the presence or absence of the equivalent elements that are present within the same or separate collection. The Distinct, Union, Intersect, and Except clauses are classified as the Set operators. The Distinct clause removes duplicate values from a collection.
The syntax of the Distinct clause is:
For C#
public static IEnumerable<T> Distinct<T>( this IEnumerabIe<T> source);

The Union clause returns the union of two sets. The result elements are the unique elements that are common in the two sets.
The syntax of the Union clause is:
For C#
public static IEnumerabIe<T> Union<T>( this IEnumerabIe<T> source1, IEnumerabIe<T> source2);

The Intersect clause returns the set intersection. The elements appear in each of the two collections.
The syntax of the Intersect clause is:
For C#
public static IEnumerabIe<T> Intersect<T>( this IEnumerabIe<T> source1, IEnumerabIe<T> source2);
The Except clause returns the set difference. The result of the Except clause returns the elements of one collection that do not appear in the second collection.
The syntax of the Except clause is:
For C#
public static 1Enumerable<T> Except<T>( this 1Enumerable<T> sourcel, IEnumerab1e<T> source2);

lets take an simple example

Source Code
<form id="form1" runat="server">
    <div>
    
    </div>
    <asp:ListBox ID="ListBox1" runat="server" Height="137px" Width="221px">
    </asp:ListBox>
    <br />
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
        Text="common word" />
    </form>
Code behind
protected void Button1_Click(object sender, EventArgs e)
    {
        string[] fruits = { "apple", "mango", "banana" };
        string[] wds = { "my world", "mango", "operation" };
        var common = fruits.Intersect(wds);
        ListBox1.Items.Add("common in both array");
        foreach (var item in common)
        {
            ListBox1.Items.Add(item);
        }
    }
Code Generate the following output
How to use LINQ Set Operators in ASP.NET

How to add hour, minute and seconds in existing time ASP.NET Example

Introduction

If you want to add hour, minute and seconds in existing time then you should take TimeSpan structure with overload function. Now, you can easily add TimeSpan structure with existing date time object using Add method. Lets take an simple example for demonstration.
Step-1 : Add new web form into the project
Step-2 : Add one button and label control on it.
Step-3 : Handle Button click event.

<form id="form1"
    runat="server">
    <asp:Button ID="Button1"
    runat="server"
    BackColor="#33CCFF"
    onclick="Button1_Click"
    Text="Click" />
    <div>  
    <asp:Label ID="Label1"
    runat="server"
    BackColor="Yellow"></asp:Label>  
    </div>
    </form>
Code Behind
 protected void Button1_Click(object sender, EventArgs e)
        {          
            DateTime now = DateTime.Now;    
            TimeSpan TR = new TimeSpan(3, 41, 11);
            DateTime afterAddedSpanTime = now.Add(TR);
            Label1.Text = "current time : " + now.ToLongTimeString();
            Label1.Text += "<br /><br />";
            Label1.Text += "after added a spantime with current time <br />";
            Label1.Text += "time span: 3 hours|41 minutes|11 seconds <br />";
            Label1.Text += afterAddedSpanTime.ToLongTimeString();

        }
Code Generate the following output
How to add hour, minute and seconds in existing time ASP.NET Example

How to show AM and PM with time in ASP.NET

Introduction

You can show System time as well as universal time through DateTime class. Now, this time you want to show only am and pm text after the current time. You can use "tt" in the ToString ( ) method. Lets create an simple algorithm.
Step-1 : Take a web form into the project
Step-2 : Add two control (button and label control) onto the form window.
Step-3 : Raise button click event
Step-4 : Take single instance of DateTime class also pass "tt" parameter in ToString ( ) method.
Step-5 : Now Run your application from green triangle button or Ctrl+f5

Complete Source Code

<form id="form1" runat="server">
    <div>
   
        <asp:Button ID="Button1"
         runat="server"
         onclick="Button1_Click"
         Text="Click"
         BackColor="#99CCFF" />  
    </div>
        <asp:Label ID="Label1"
        runat="server"
        Text="Label"
        BackColor="Yellow"></asp:Label>
    </form>
Code Behind
 protected void Button1_Click(object sender, EventArgs e)
        {
          DateTime now = DateTime.Now;          
            Label1.Text = "current time PM AM: " + now.ToLongTimeString();
            Label1.Text += "<br /><br />";          
            Label1.Text += "current time [PM AM]: " + now.ToString("tt");

        }
Code generate the following output
How to show AM and PM with time in ASP.NET

How to find first day of next month in ASP.NET

If you want to get first day of next month , use DateTime structure with AddMonths( ) method. using this method you can get first day of next month. Now, create a new instance of DateTime structure with some parameter like DateTime(Int32, Int32, Int32). This parameterized method Initializes a new instance of the DateTime structure to the specified year, month, and day. Here you can pass integer 2 in day field. Now, you can delete 1 day from current DateTime object using AddDays (-1) method.

Source Code 

 <form id="form1" runat="server">
    <div>  
        <asp:Button ID="Button1"
        runat="server"
        BackColor="#66CCFF"
        onclick="Button1_Click"
        Text="Click" />  
    </div>
       <asp:Label ID="Label1"
       runat="server"
       BackColor="Yellow"
       Text="Label"></asp:Label>
    </form>

Code Behind

 protected void Button1_Click(object sender, EventArgs e)
        {            
            DateTime today = DateTime.Today;            
            DateTime tempDate = today.AddMonths(1);
            DateTime tempDate2 = new DateTime(tempDate.Year, tempDate.Month, 2);
            DateTime nextmonthfirstday = tempDate2.AddDays(-1);
            Label1.Text = "Today : " + today.ToLongDateString();
            Label1.Text += "<br /><br />next month first day = ";
            Label1.Text += nextmonthfirstday.ToLongDateString();

        }

Code Generate the following output

How to find first day of next month in ASP.NET

Tuesday, April 22, 2014

How to Create DropDownList in Asp.Net MVC

Drop down list is a common and usable control in every programming area to let the user can select only one item from the list. In MVC we have to create a list in action method of controller and then pass it through any method like ViewBag and ViewModel etc.

Programmer can create required type of list as per the requirements, we will create a SelectListItem type of list because this list provides two property Text and Value by default. Create a simple list of type SelectListItem and add some items (we have added four item here) as added below:

List<SelectListItem> items = new List<SelectListItem>();
items.Add(new SelectListItem { Text = "Value 1", Value = "1" });
items.Add(new SelectListItem { Text = "Value 2", Value = "2" });
items.Add(new SelectListItem { Text = "Value 3", Value = "3" });
items.Add(new SelectListItem { Text = "Other", Value = "4" });

Now to pass this to View page write a single line as written below:

ViewBag.ddlItems = new SelectList(items, "Value", "Text");

Value will be the dataValue field and Text will be dataText field for this dropdownlist. In the view page create a form and of course a dropdownlist with a submit button:

@using (Html.BeginForm())
{
    <div>
        @Html.DropDownList("selectedItem", (IEnumerable<SelectListItem>)ViewBag.ddlItems)
        <input type="submit" value="Submit" id="submitBtn" />
    </div>
}

First parameters of this helper Html.DropDownList() specifies name of the HTML select element, and also the name of the view model property that is bound to the element. Second parameter shows the list of options of select list.

This will create a dropdownlist with a submit button. Run the application and open particular view in the browser. Select any value and then submit. Oops! i haven't write the code to access the selected value. So write HttpPost type of action:

public ActionResult Index(string selectedItem)
{
....
....

Here parameter selectedItem is the same name of dropdownlist we have assigned in the view page. Again run the application, select any value("Value 2") and then submit. This parameter will contain the selected value(2).

How to add Hours in existing time in ASP.NET

Introduction

Using AddHours method  you can add Hours in integer, Object will be updated with new date and time. This method is used where you want to calculate time after some time. Like Library management project. Lets take an simple example

<form id="form1" runat="server">
    <div>  
        <asp:Button ID="Button1"
        runat="server"
        BackColor="#66FFFF"
        onclick="Button1_Click"
        Text="Click" />  
    </div>
        <asp:Label ID="Label1"
        runat="server"
        BackColor="Yellow"></asp:Label>
    </form>
Code Behind
 protected void Button1_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Now;          
            DateTime after3Hours = now.AddHours(3);            
            DateTime after4Hours = now.AddHours(4);
            Label1.Text = "present time = " + now.ToLongTimeString();
            Label1.Text += "<br /><br />";
            Label1.Text += "after added three hours with present time <br />";
            Label1.Text += after3Hours.ToLongTimeString();
            Label1.Text += "<br /><br />";
            Label1.Text += "after added four hours with present time <br />";
            Label1.Text += after4Hours.ToLongTimeString();

        }
 Code Generate the following Output
How to add Hours in existing time in ASP.NET

Monday, April 21, 2014

Add seconds in existing time ASP.NET example

Introduction

Using AddSeconds method  you can add seconds in integer, Object will be updated with new date and time. This method is used where you want to calculate time after some seconds. Like Library management project. Lets take an simple example

<form id="form1" runat="server">
    <asp:Button ID="Button1"
    runat="server"
    BackColor="#66CCFF"
    onclick="Button1_Click" Text="Click" />
    <div>  
    <asp:Label ID="Label1"
    runat="server"
    BackColor="Yellow"></asp:Label>  
    </div>
    </form>
Code Behind
 protected void Button1_Click(object sender, EventArgs e)
 
        {        
            DateTime now = DateTime.Now;          
            DateTime after56Seconds = now.AddSeconds(56);
            Label1.Text = "present time = " + now.ToLongTimeString();
            Label1.Text += "<br /><br />";
            Label1.Text += "after added 56 seconds with present time= ";
            Label1.Text += after56Seconds.ToLongTimeString();

        }
Code Generate the following output
Add seconds in existing time ASP.NET example

© Copyright 2013 Computer Programming | All Right Reserved