-->

Tuesday, March 25, 2014

Remove last element of array in C#

Using the Resize method you can decrement size of an array. Resize method creates a new array with new specified size Also copy old array items into newly created. Let's take an simple example to remove last element. Take less than one size of original array size. Newly created array skip last element of original array size.

<form id="form1" runat="server">
    <asp:Button ID="Button1" runat="server" Height="37px" onclick="Button1_Click"
        Text="Vegetable" Width="100px" />
    <div>
 
        <asp:Label ID="Label1" runat="server" Text="Label" Width="100px" ForeColor="Black" BackColor="Yellow"></asp:Label>
 
    </div>
    </form>

Business Logic code

 protected void Button1_Click(object sender, EventArgs e)
    {
        string[] Vegtables = new string[]
        {
            "1.Tomato",
            "2.Sweet-potato",
            "3.Onion",
            "4.Potato",
            "5.Carrot",
            "6.Locky",
            "7.Brinjal"
        };

        Label1.Text = "Vegtables array...<br />";
        foreach (string Vegtable in Vegtables)
        {
            Label1.Text += Vegtable + "<br />";
        }

        Array.Resize(ref Vegtables, Vegtables.Length - 1);

        Label1.Text += "<br />Vegtables array [last element remove...<br />";
        foreach (string Vegtable in Vegtables)
        {
            Label1.Text += Vegtable + "<br />";
        }
    }

Code Generate the following Output


Resize array method

Print element of array at specific position in c#, LINQ subset array

Using the LINQ subset array you can print element of array at specific position through Skip( ) method. If you want to print array element at position number 3 then you should skip three starting index of array. Like
ArrayName .Skip(3).Take(2).ToArray( ) .

Source

<form id="form1" runat="server">
    <div>  
        <asp:Button ID="Button1" runat="server" Height="33px" onclick="Button1_Click"
            Text="Button" Width="96px" />  
    </div>
    <p style="width: 118px">
        <asp:Label ID="Label1" runat="server" Height="40px" Text="Label" Width="100px"></asp:Label>
    </p>
    </form>

Business Logic Code

 protected void Button1_Click(object sender, EventArgs e)
    {
        string[] Vegetables = new string[]
        {
            "Tomato",
            "Potato",
            "Onion",
            "Carrot",
            "Sweet-Potato",
            "Locky"

        };

        Label1.Text = "Vegetables array...<br />";
        foreach (string Vegetable in Vegetables)
        {
            Label1.Text += Vegetable + "<br />";
        }
        string[] arraySubset = Vegetables.Skip(3).Take(2).ToArray();

        Label1.Text += "<br />Vegetables sub array [element after 3 and count 2]...<br />";
        foreach (string Vegetable in arraySubset)
        {
            Label1.Text += Vegetable + "<br />";
        }

Print element of array at specific position in c#, LINQ subset array

Insertion sort and let us consider an array of size 10 for Data Structure in 'C'

Insertion sort: 

                 In this technique of sorting, the fact of inserting the pivotal element  at its proper position from n (size of the array) number of elements is used. The pivotal element to be inserted is picked from the array itself. The selected pivotal element is inserted at the position from the left part of the array that is treated as sorted.
                   Initially the first element of the array is treated as left part of the array and being one element that is treated as sorted part of the array. The first pivotal element that is selected for insertion is the second element of the array. the pivotal element is compared with the first element and if the first element is greater than the pivotal element, then it is moved to the second position. Now we remain with no elements compared to be compared in the left part, the position where the pivotal element is found as the first position. The pivotal element is inserted at the first position to over the pass. If the first element is not greater than the pivotal element then the position of the pivotal element is right and remains at the same place. After the first pass the left part of the array containing two elements is in sorted order.
                 In the further passes the element of the unsorted part of the array is selected as pivotal element and a position to insert it in the sorted part (left array) of the array is found by shifting the sorted elements to the right if required. So after every pass the pivotal element is inserted at its proper position in the array. As the insertion operation is being performed to insert the pivotal element, this technique of sorting the array is called as ‘’insertion sort’’.

 Let us consider an array of size 10 with the following elements:

                              91 18 22 43 34 10 88 11 33 77

We can observe the elements and see that the elements are not in any order. So to arrange the elements in ascending order we can apply the insertion sort.
In the first pass, we select the element 18 is pivotal element. The left part of the of the array before the pivotal element contains only one element and it is the sorted part of the array. 18 is compared with 91,91 is greater than 18. So, 91 is shifted to the right by one position. Now there are no elements to be compared in the sorted part of the array with the pivotal element. Now the pivotal element is inserted at first position of the array. After this pass the array looks as follows: [sorted elements-BOLD, pivotal element – underlined].
                                    18 91 22 43 34 10 88 11 33 77
In the second pass, 22 is selected as pivotal element. 22 is compared with 91, as 91 is greater then 22, 91 is shifted towards its right by one position. Again 22 is compared with 18,18 is not greater then the pivotal element 22. So, the comparison is stopped because the position where the pivotal element 22 is to be inserted is found. The pivotal element 22 is inserted at second position. After this pass the array looks as follows:
                                     18 22 91 43 34 10 88 11 33 77
In the third pass, 43 is selected as pivotal element from the unsorted part of the array. 43 is compared with 91 and  91 is shifted towards its right by one position. 43 is again compared with 22. 22 is not greater then 43 and comparison is stops. 43 is inserted at third element of array.  After this pass the array looks as follows:
                                     18 22 43 91 34 10 88 11 33 77
In the fourth pass, 34 is selected as pivotal element from the unsorted part of the array. 34 is compared with 91 and 91 is shifted towards its right by one position. 34 is again compared with 43. 43 is also right shifted by one position. 34 is now comparison with 22. 22 is not greater then 34 and comparison stops. 34 is inserted at third element of array.  After this pass the array looks as follows:
                                     18 22 34 43 91 10 88 11 33 77
In the fifth pass, 10 is selected as pivotal element from the unsorted part of the array. As 10 smaller then all the sorted elements of the left part of the array, each element is shifted to right by one position respectively. 10 is inserted as the first element of array.  After this pass the array looks as follows:
                                     10 18 22 34 43 91 88 11 33 77
In the sixth pass, 88 is selected as pivotal element from the unsorted part of the array. 88 is compared with 91 and  91 is shifted towards its right by one position because it is greater then 88. 88 is now compared with 43. and comparison is stops. 88 is inserted at sixth element of array.  After this pass the array looks as follows:
                                     10 18 22 34 43 88 91 11 33 77
In the seventh pass, 11 is selected as pivotal element from the unsorted part of the array. 11 is compared with all the element up to 18 and they are  shifted to right by one position respectively. The comparison is stops when 11 is compared with 10. So, 11 is inserted at second element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 34 43 88 91 33 77
In the eighth pass, 33 is selected as pivotal element from the unsorted part of the array. 33 is compared with all the element up to 34 and they are  shifted to right by one position respectively. The comparison is stops when 33 is compared with 22. So, 33 is inserted at fifth element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 33 34 43 88 91 77
In the ninth and final pass, 77 is selected as pivotal element from the unsorted part of the array. 77 is compared with 91 and 88 and they are shifted to right by one position respectively. The comparison is stops when 77 is compared with 43. So, 77 is inserted at eighth element of the array.  After this pass the array looks as follows:
                                     10 11 18 22 33 34 43 77 88 91 

Change Header border style as dotted of Gridview in ASP.NET

Make border dotted of GridView header row using BorderStyle property using property window. This style enumeration is applies on compile time. First bind Gridview with SqlDataSource control , after that make some changes in style properties. This example cover dotted border style. Some steps are:

Step-1 : Open Visual Studio IDE
Step-2 : Add New Webform into your project
Step-3 : Add Gridview control on design window.
Step-4 : Bind Gridview with SqlDataSource control, make changes in border style

Example of Headerstyle BorderStyle of Gridview in asp.net


<!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">
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" 
        AutoGenerateColumns="False" DataKeyNames="Sno" DataSourceID="SqlDataSource1">
        <Columns>
            <asp:BoundField DataField="Sno" HeaderText="Sno" InsertVisible="False" 
                ReadOnly="True" SortExpression="Sno" />
            <asp:BoundField DataField="name" HeaderText="name" SortExpression="name" />
            <asp:BoundField DataField="address" HeaderText="address" 
                SortExpression="address" />
            <asp:BoundField DataField="contactno" HeaderText="contactno" 
                SortExpression="contactno" />
        </Columns>

 <HeaderStyle BorderStyle="Dotted" />

    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
        DeleteCommand="DELETE FROM [userdataTable] WHERE [Sno] = @Sno" 
        InsertCommand="INSERT INTO [userdataTable] ([name], [address], [contactno]) VALUES (@name, @address, @contactno)" 
        SelectCommand="SELECT * FROM [userdataTable]" 
        UpdateCommand="UPDATE [userdataTable] SET [name] = @name, [address] = @address, [contactno] = @contactno WHERE [Sno] = @Sno">
        <DeleteParameters>
            <asp:Parameter Name="Sno" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="address" Type="String" />
            <asp:Parameter Name="contactno" Type="Int32" />
            <asp:Parameter Name="Sno" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <div>
    
    </div>
    </form>
</body>
</html>

Code Generate the following output


Example of Headerstyle BorderStyle of Gridview in asp.net

How to use LINQ to SQL in ASP.NET

INTRODUCTION

LINQ to SQL is a component of .NET Framework, and is specifically designed for working with SQL server database. It provides a run-time infrastructure for managing relational data as objects. It allows you to write queries to retrieve and manipulate data from the SQL server. LINQ to SQL supports all the key functions that you would expect while developing SQL applications. You can retrieve the data from the database, insert, update, and also delete the information from the table. Your query expression is translated into parameterized SQL code, parameters are created, and the query is executed on the server. LINQ to SQL also supports transactions, views, and stored procedures. It also provides an easy way to integrate data validation and business logic rules into your data model.

LINQ to SQL is an object-relational mapping (ORM) framework that allows the direct 1-1 mapping of a Microsoft SQL Server database to .NET classes, and query of the resulting objects using LINQ. With the help of LINQ to SQL ORM mapping, the classes that match the database table are created automatically from the database itself and you can start using the classes immediately.

Let's take an simple Example

Step-1 : Add a LINQ to SQL class to the application 
Step-2 : Right-click the Solution name in the solution explorer and select Add New Item from The context menu. This will open the Add New Item dialog box.
Step-3 : In the dialog box, select LINQ to SQL Classes and rename the file as my.dbml. Default name for LINQ to SQL class is DataClasses.dbml

LINQ to SQL Class in asp.net

Step-4 : Click Add

The object Relational Designer window opens. In this window, you can drag and drop the table from the server Explorer. The action will add the table to your application, and you can retrieve any data from the table. In this case, the products table from the database is added to the object Relational Designer window, 
After you have added the table to the my.dbml file, you can find the code for the .aspx in the listing

Add database table into dbml file

Source code
<form id="form1" runat="server">
    <div>
        <asp:ListBox ID="ListBox1" runat="server" Height="97px" Width="98px"></asp:ListBox> 
    </div>
    </form>
Code Behind Code
protected void Page_Load(object sender, EventArgs e)
    {
        myDataContext dc = new myDataContext();
        var query = dc.usertables;
        foreach (usertable  item in query)
        {
            ListBox1.Items.Add(item.id.ToString() + " " + item.name);
        }
    }

Code generate the following output

How to use LINQ to SQL in ASP.NET
If your .dbml name is my then your DataContext name is myDataContext. After adding the table into dbml file , your table name look in the dbml file. 

C program to sort 10 elements of an array and a list of 11 names in ascending order using selection sort technique

C program to sort 10 elements of an array in ascending order using selection sort technique.

#include<stdio.h>
#include<conio.h>
void se1_sort_ao(int arr[],int n)
{
  int i, j, min, mops;
  for(i=0;i<n-1;i++)
   {
    min = arr[i]; mpos = 1;
    for(j=i+1; j<n; j++)
    if(arr[j] < min)
      {
      min = arr[j]; mops = j;

      }
    arr[mpos] = arr[i];   arr[i] = min;
   }
}
main()
{
    int a[10], i;
    printf("Enter 10 element of the array:\n \n");
    for(i=0;i<10;i++)
      scanf("%d",&a[i]);
    printf("The array before sorting:\n \n");
    for(i=0;i<10;i++)
      printf("%d",a[i]);
    sel_sort_ao(a,10);
    printf("\n \n The array after sorting:\n \n");
    for(i=0;i<10;i++)
      printf("%d",a[i]);
}

C program to sort a list of 11 names in ascending order using selection sort technique.

#include<stdio.h>
#include<conio.h>
#include<string.h>
void se1sortstrao(char a[][20],int n)
{
  int i, j, mops, k; char min[20];
  for(i=0;i<n-1;i++)
   {
    strcpy( min,a[i]); mpos=i;
    for(j=i+1; j<n; j++)
    if(strcmp(a[j],min)<0)
      {
       strcpy(min,a[j]);   mpos=j;

      }
    strcpy(a[mpos],a[i]);  strcpy(a[i],min);
   }
}
main()
{
    char a[11][20];
    int i;
    clrscr();
    printf("Enter List of 11 names:\n \n");
    for(i=0;i<11;i++)
    gets(a[i]);
    selsortstrao(a,11);
    clrscr();
    printf("The List after sorting:\n \n");
    for(i=0;i<11;i++)
    puts(a[i]);
}

Monday, March 24, 2014

'C' Function to implement 'selection sort' in ascending and descending order

‘C’ function to implement ‘selection sort’ in ascending order.


void se1_sort_ao(int arr[],int n)
{
    int i, j, min, mpos;
    for(i=0;i<n-1;i++)
    {
    min = arr[i];    mpos = i;
    for(j=i+1;j<n;j++)    /*selection of min element */
     if(arr[j] < min)
     {
      min = arr[j]; mpos =j;
     }
    arr[mpos] = arr[i];         /*replacing min element */
    arr[i] = min;
    }    
}

‘C’ function to implement ‘selection sort’ in descending order.

void se1_sort_ao(int arr[],int n)
{
    int i, j, max, mpos;
    for(i=0;i<n-1;i++)
    {
    max = arr[i];    mpos = i;
    for(j=i+1;j<n;j++)    /*selection of max element */
     if(arr[j] > max)
     {
      max = arr[j]; mpos =j;
     }
    arr[mpos] = arr[i];         /*replacing max element */
    arr[i] = max;
    }    
}

© Copyright 2013 Computer Programming | All Right Reserved