-->

Sunday, April 13, 2014

LINQ Grouping operator in ASP.NET

Introduction


The Grouping operators in LINQ are used to put data into groups so that the elements in each group share a common attribute. The Group clause is the Grouping operator used in LINQ. The Group clause returns a sequence of IGrouping<TKey, TElement> objects that contain zero or more items that match the key value for the group.
The syntax of using the GroupBy clause is:
 For C#
public static IEnumerable<IGrouping<K, T>> GroupBy<T, K>( Me IEnumerable<T> source, Function<T, K> keySeIector)

Lets take an simple example

<div>
    
        <asp:ListBox ID="ListBox1" runat="server"></asp:ListBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" 
            Text="Grouping Operator Linq" />
    
    </div>

Code behind code

protected void Button1_Click(object sender, EventArgs e)
    {
        string[] fruits = { "apple", "banana", "pineapple", "papaya", "blueberry", "cheery" };
        var groupword = from w in fruits
                        group w by w[0] into g
                        select new { FirstLetter = g.Key, fruits = g };
        foreach (var g in groupword)
        {
          ListBox1 .Items .Add ("Words that start from letter:"+g.FirstLetter .ToString());
            foreach (var w in g.fruits)
{
ListBox1 .Items .Add (w);
}
        }
    }

code generate the following output

LINQ Grouping operator in ASP.NET

According to above given example. Grouping index start from 0. Here we take two for-each loop first one for printing message with starting letter of English such as 'a' and second one for printing grouping letter words like 'a' for apple and 'p' for "pineapple" and "papaya". 

How to bind Dropdownlist Control in ASP.NET

In the previous example we have already discussed about  DropdownList like .

  1. How to change font name at runtime
  2. Bind DropdownList using XML file 

For binding DropdownList using ado.net you must use System.Data.SqlClient namespace, which is contains various classes like SqlConnection,SqlCommand etc.



<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ToString();
            con.Open();
            System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
            cmd.CommandText = "insrtdata";
            cmd.CommandType = System.Data.CommandType.StoredProcedure;
            cmd.Connection = con;
            System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
            System.Data.DataSet ds = new System.Data.DataSet();
            da.Fill(ds);
            DropDownList1 .DataTextField ="Name";
        DropDownList1 .DataValueField  ="Id";
        DropDownList1 .DataSource =ds;
        DropDownList1 .DataBind ();        
    }
</script>

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

Output
How to bind dropdownlist in asp.net

Saturday, April 12, 2014

How to insert string at specified position of existing string in ASP.NET

If you want to insert another string at 0 index of existing string, suppose you have a string like "jacob" and you want to insert another string like "hello" at 0 index , then you must use insert method of the StringBuilder class with 0 index.

<form id="form1" runat="server">
    <div>
 
        <asp:Button ID="Button1"
         runat="server"
          Height="29px"
          onclick="Button1_Click"
            Text="Button"
             Width="87px" />
 
    </div>
    <asp:Label ID="Label1"
     runat="server"
      BackColor="Yellow"
      Text="Label"></asp:Label>
    </form>

CodeBhindcode-

 protected void Button1_Click(object sender, EventArgs e)
    {
        System.Text.StringBuilder stringA = new System.Text.StringBuilder();
        stringA.Append("2nd string.");

        stringA.Insert(0, "include string in front of stringbuilder. ");

        Label1.Text = stringA.ToString();

    }

Output-

How to insert string at specified position of existing string in ASP.NET

Friday, April 11, 2014

How to remove last character from the string in ASP.NET

If you want to remove last character from the string, you can use Remove method of the StringBuilder class.
In this method, you must specify the length of the string also specify how many character remove from the string. Lets take an example , we take a StringBuilder class object, append this object with some text. After assigning some string value into object, you can remove characters from the string object.
<form id="form1" runat="server">
    
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Click"
            Width="80px" />
   
    <asp:Label ID="Label1" runat="server" BackColor="#FFFF66" Text="Label"></asp:Label>
    </form>

Code behind code-


 protected void Button1_Click(object sender, EventArgs e)
    {
        StringBuilder stringA = new StringBuilder();
        stringA.Append("it is a text. ");
        stringA.Append("it is another text.");

        Label1.Text = stringA.ToString();

        stringA.Remove(stringA.Length - 1, 1);

        Label1.Text += "<br /><br />after removing last character from stringbuilder....<br />";
        Label1.Text += stringA.ToString();
    }


Output-



Thursday, April 10, 2014

How to use LINQ Join operator in ASP.NET

Introduction

The Join operators in LINQ are used to join objects in one data source with objects that share a common attribute in another data source. The Join operators provided in LINQ are Join and GroupJoin. The Join clause implements an inner join, which is the type of join in which only those objects that have a match in other data set are returned. The GroupJoin clause joins two sequence based on a key selector functions and groups the results. The syntax of using the Join operator is:

Syntax in C#

public static IEnumerable<V> Join<T, U, K, V>( this IEnumerable<T> outer, IEnumerable<U> inner, Function<T, K> outerKeySeIector, Function<U, K> innerKeySelector, Function<T, U, V> resultSelector);

Lets take an simple example

<div>
        <asp:ListBox ID="ListBox1" runat="server" Height="151px" Width="134px"></asp:ListBox>
        <br />
        <br />
        <asp:Button ID="Button1" runat="server" Text="Join Operator example" 
            onclick="Button1_Click" />
    </div>
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 Default9 : System.Web.UI.Page
{
    public class Customer
    {
        public int key;
        public string Name;
    }
    public class order
    {
        public int key;
        public string OrderNumber;


    }
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        var customers = new List<Customer>()
        {
new Customer {key =1,Name ="jacob"},
new Customer {key=2,Name ="smith"}
        };
        var orders = new List<order>()
        {
            new order {key=1,OrderNumber ="Number 1"},
            new order {key =2,OrderNumber ="Number 2"}

        };

        var q = from c in customers
                join o in orders on c.key equals o.key
                select new { c.Name, o.OrderNumber };
        foreach (var item in q)
        {
            ListBox1.Items.Add(item.OrderNumber.ToString() + " " + item.Name);
        }
    }
}
Code generate the following output
How to use LINQ Join operator in ASP.NET

Wednesday, April 9, 2014

DropdownList with image in ASP.NET

Dropdownlist : We have already discuss about DropdownList, today we will learn how to take text item with image in DropdownList. There are following steps to design Dropdownlist with image.
Step-1 : Import JQuery file from desired resources, resource is

http://code.jquery.com/jquery-1.8.2.js
Step-2 : Attach jQuery link with <script> tag in Head section. Now your code look like


<head id="Head1" runat="server">
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
</head>

Step-3 : Download another JQuery file from mentioned resources, also attach with <script> tag in head section. Resource is
http://dl.dropboxusercontent.com/u/40036711/Scripts/jquery.ddslick.min.js

Step-4 : Create JQuery function, also bind DropDownlist with Text, description and Images.
Like
    $(function () {
        var dropdownlist = [
{
    text: "mango",
    value: 1,
    description: "summer fruit",
    imageSrc: "image001.jpg"
}
Similarly again. insert multiple item into the list.

Step-5 : Now call ddslick ( ) method , which is contain DropdownList parameters, such as Data, imagePosition, width etc in same scripting function.

 $('#drop').ddslick({
            data: dropdownlist,
            width: 200,
            imagePosition: "right"
            
        });

Step-6 : End your Jquery function
Step-7 : Take a <div> tag with drop id.

Now your code is 

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

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>DropdownList with image</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script src="jquery.ddslick.min.js"  type="text/javascript"></script>
<script type="text/javascript">
    $(function () {
        var dropdownlist = [
{
    text: "mango",
    value: 1,
    description: "summer fruit",
    imageSrc: "image001.jpg"
},
{
    text: "Apple",
    value: 2,
    description: "summer as well as winter fruit",
    imageSrc: "image002.jpg"
}

];
        $('#drop').ddslick({
            data: dropdownlist,
            width: 200,
            imagePosition: "right"
            
        });
    });
</script>
</head>
<body>
<form id="form1">
<div>
<div id="drop"></div>
</div>
</form>
</body>
</html>
Dropdownlist with image icon

How to check two string are equal in ASP.NET

Introduction

If you want to compare two string object then we can use compare( ) method of the string class. Using this method you can compare characters through casing rules and the alphabetic order. It returns 32-bit signed integer value. If returning value is equal to zero then both string are equal otherwise string is greater or less.
Suppose, your string returns less than zero value it means your first string is less than to string b.

Lets take an simple example

Source code
<div>
        <asp:Button ID="Button1" runat="server" Text="String Compare" onclick="Button1_Click" />
        <br />
        <br />
        <asp:Label ID="Label1" runat="server"></asp:Label>
    </div>

Business logic code

#region stringcompare

    protected void Button1_Click(object sender, EventArgs e)
    {
        string a = "hello";
        string b = "hello";
        int c = string.Compare(a, b);
        if (c == 0)
        {
            Label1.Text = "Both string are equal";

        }
        else
        {
            if (c < 0)
            {
                Label1.Text = "string a is less than string b";

            }
            else
            {
                Label1.Text = "string a is greater than string b";
            }

        }
    }

    #endregion 

Code generate the following output
How to check two string are equal in ASP.NET
© Copyright 2013 Computer Programming | All Right Reserved