Friday, November 18, 2011

database layer

---   ExecuteNonQuery

public static int ExecuteNonQuery(DbCommand command)
        {
            // The number of affected rows
            int affectedRows = -1;
            // Execute the command making sure the connection gets closed in the end
            try
            {
                // Open the connection of the command
                command.Connection.Open();
                // Execute the command and get the number of affected rows
                affectedRows = command.ExecuteNonQuery();
            }
            catch (Exception ex)
            {
                (ex.Message, ex);
            }
            finally
            {
                command.Connection.Close();
            }
            // return the number of affected rows
            return affectedRows;
        }


public static string ExecuteScalar(DbCommand command)
        {
            // The value to be returned
            string value = String.Empty;
            // Execute the command making sure the connection gets closed in the end
            try
            {
                // Open the connection of the command
                command.Connection.Open();
                // Execute the command and get the number of affected rows
                var val = command.ExecuteScalar();
                if(val != null)   
                    value  = val.ToString();
            }
            catch (Exception ex)
            {
                (ex.Message, ex);
            }
            finally
            {
                command.Connection.Close();
            }
            // return the result
            return value;
        }

 public static IDataReader ExecuteSelectReaderCommand(DbCommand command)
        {
            DbDataReader reader;
            // Execute the command making sure the connection gets closed in the end
            try
            {
                // Open the data connection
                command.Connection.Open();
                command.CommandTimeout = 600;
                // Execute the command and save the results in a DataTable
                reader = command.ExecuteReader();
            }
            catch (Exception ex)
            {
                ///rethrow our exception wrapper to recognize it on the upper level
                (ex.Message, ex);
            }
            finally
            {
                command.Connection.Close();
            }
            return reader;
        }

grid view drop down event

Please refer to these url

1. http://asimsajjad.blogspot.com/2009/09/raising-dropdownlist.html
2.http://stackoverflow.com/questions/485390/why-isnt-the-selectedindexchanged-event-firing-from-a-dropdownlist-in-a-gridvie

Thursday, November 17, 2011

BINARY SEARCH in c++ with output

// Program to search an element from an array using BINARY SEARCH

#include
#include

int bsearch(int [],int,int);

int main()
{ int AR[50],ITEM,N,index;
clrscr();
cout<<"ENTER DESIRED ARRAY SIZE(MAX 50): " ;
cin>>N;
cout<<"\n ENTER ARRAY ELEMENTS(MUST BE STORED IN ASC ORDER)\n";
for(int i=0;i cin>>AR[i];

cout<<"\n ENTER ELEMENTS TO BE SEARCHED FOR:";
cin>>ITEM;
index=Bsearch(AR,N,ITEM);
if(index==-1)
cout<<"\n SORRY!!! GIVEN DATA CAN'T BE OPENED \n";
else
cout<<"\n ELEMENTS FOUND AT INDEX:"< <<"\t,Position:"< return 0; }

int bsearch(int AR[],int size ,int item)
{ int beg ,last,mid;
beg=0;
last=size-1;
while(beg<=last)
{ mid=(beg+last)/2;
if(item==AR[mid])
return mid;
else if(item>AR[mid])
beg=mid+1;
else
last=mid-1;
}
return -1;
}

===================:OUTPUT:===============================================

ENTER DESIRED ARRAY SIZE(MAX 50): 4
ENTER ARRAY ELEMENTS(MUST BE STORED IN ASC ORDER)
4
25
60
78
ENTER ELEMENTS TO BE SEARCHED FOR: 78
ELEMENTS FOUND AT INDEX:3 , POSITION:4

Binary search in c with OUTPUT

    #include<conio.h>
       #include<stdio.h>
    
 
 void main()
  {
     int n[50],i,j,k,temp,first,mid,last,ser,flag=0;
     clrscr();
     printf("how many number u want to insert?");
     scanf("%d",&k);
     for(i=0;i<=k-1;i++){
          printf("Enter values?");
          scanf("%d",&n[i]);
    }
    for(i=0;i<=k-1;i++){

        for(j=i+1;j<=k-1;j++)
        {
          if(n[i]>n[j])
          {
            temp=n[i];
            n[i]=n[j];
            n[j]=temp;
         }
      }
   }
  
    printf("Sorting\n");

    for(i=0;i<=k-1;i++){

        printf("%d\n",n[i]);
    }  
   
    printf("Enter serching number?");
    scanf("%d",&ser);
    first=0;
    last=k-1;
    mid=(first+last)/2;
    while(last>=first)
    {
      if(ser==n[mid])
      {
         flag=1;
         break;
       }
       else if(n[mid]>ser)
        {
           first=first;
           last=mid-1;
           mid=(first+last)/2;
         }
        else if(n[mid]<ser)
         {
            first=mid+1;
            last=last;
            mid=(first+last)/2;
         }

    }
    if(flag==1){
   
         printf("\n");
         printf("SEARCHING NUMBER IS FOUND");
         printf("\n");
         printf(" POSITION OF SEARCHED NUMBER %d",mid+1);
    }
    else{
   
      printf("Searching Number not found");
    }
    getch();
 }




OUTPUT :
How many numbers u want to insert ? 5
Enter values ?22
Enter values ?34
Enter values ?6
Enter values ?78
Enter values ?44
Sorting :
6
22
34
44
78





Tuesday, November 15, 2011

New Project



#include<iostream.h>

class searching
{
private:
double *array;
int n;
public:
void input();
void linearsearch();
};

void searching::input()
{
cout<<”*********************************…
<<”This program is to implement linear search algorithm\n”
<<”*************************************…
cout<<”Enter how many numbers you are going to enter::”;
cin>>n;
array=new double[n+1];
cout<<”Now enter your elements ::\n”;
for(int i=1;i<=n;i++)
cin>>array[i];
}

void searching::linearsearch()
{
cout<<”Enter the number to be searched ::”;
double x;
cin>>x;
int i;
for(i=1;i<=n;i++)
{
if(array[i]==x)
{
cout<<”found at position ::”<<i<<endl;
break;
}
}
if(i>n)
cout<<”Not found\n”;
}

int main()
{
searching obj;
obj.input();
obj.linearsearch();
return 0;
}
/************* Please not copy this************************…
SAMPLE OUTPUT ::

**************************************…
This program is to implement linear search algorithm
**************************************…
Enter how many numbers you are going to enter::5
Now enter your elements ::
1.1
1.2
1.3
1.4
1.5
Enter the number to be searched ::1.5
found at position ::5
Press any key to continue




//Binary Searching

#include<iostream.h>
#include<conio.h>
#include<stdio.h>

void main()
{
clrscr();
int l_v=1;
int h_v;
int a[51];
int middle;
int num,i=0;
cout<<"Input your sorted num :
";
while(scanf("%d",&a[i])!=EOF)
i++;
h_v=i;
cout<<"
The input numbers are :
";
i=0;
while(i<h_v)
{
cout<<a[i]<<endl;
i++;
}
getch();
cout<<"

Input your searching number :
";
cin>>num;
for(int n=0;a[middle]!=num;n++)
{
middle = (l_v + h_v)/2;
if(a[middle]>num)
h_v = middle - 1;
else if(a[middle]<num)
l_v = middle + 1;
else if(a[middle]==num)
cout<<"
Found your number in "<<middle+1<<" position.";
}
cout<<"

This program's loop is executed "<<n<<" times to find out
the
number.";
getch();
}


Binary search program in C

#include <stdio.h>

int main()
{
int a[20] = {0};
int n, i, j, temp;
int *beg, *end, *mid, target;

printf(" enter the total integers you want to enter (make it less then 20):\n");
scanf("%d", &n);
if (n >= 20) return 0; // ouch!
printf(" enter the integer array elements:\n" );
for(i = 0; i < n; i++)
{
scanf("%d", &a[i]);
}

// sort the loaded array, a must for binary search!
// you can apply qsort or other algorithms here
for(i = 0; i < n-1; i++)
{
for(j = 0; j < n-i-1; j++)
{
if (a[j+1] < a[j])
{
temp = a[j];
a[j] = a[j+1];
a[j+1] = temp;
}
}
}
printf(" the sorted numbers are:");
for(i = 0; i < n; i++)
{
printf("%d ", a[i]);
}

// point to beginning and end of the array
beg = &a[0];
end = &a[n]; // use n = one element past the loaded array!
printf("\n beg points to address %d and end to %d",beg, end); // test

// mid should point somewhere in the middle of these addresses
mid = beg += n/2;
printf("\n mid points to address %d", mid); // test

printf("\n enter the number to be searched:");
scanf("%d",&target);

// binary search, there is an AND in the middle of while()!!!
while((beg <= end) && (*mid != target))
{
// is the target in lower or upper half?
if (target < *mid)
{
end = mid - 1; // new end
n = n/2;
mid = beg += n/2; // new middle
}
else
{
beg = mid + 1; // new beginning
n = n/2;
mid = beg += n/2; // new middle
}
}

// did you find the target?
if (*mid == target)
{
printf("\n %d found!", target);
}
else
{
printf("\n %d not found!", target);
}

getchar(); // trap enter
getchar(); // wait
return 0;
}












//----------- Java application-------------

Binary Search in Java


import java.util.*;
public class BinarySearch {
        public static void main(String[] args) {
                int[] intArray = new int[10];
                int searchValue = 0, index;
                System.out.println("Enter 10 numbers");
                Scanner input = new Scanner(System.in);
                for (int i = 0; i < intArray.length; i++) {
                        intArray[i] = input.nextInt();
                }
                System.out.print("Enter a number to search for: ");
                searchValue = input.nextInt();
                index = binarySearch(intArray, searchValue);
                if (index != -1) {
                        System.out.println("Found at index: " + index);
                } else {
                        System.out.println("Not Found");
                }
        }

        static int binarySearch(int[] search, int find) {
                int start, end, midPt;
                start = 0;
                end = search.length - 1;
                while (start <= end) {
                        midPt = (start + end) / 2;
                        if (search[midPt] == find) {
                                return midPt;
                        } else if (search[midPt] < find) {
                                start = midPt + 1;
                        } else {
                                end = midPt - 1;
                        }
                }
                return -1;
        }
}
Output
Enter 10 numbers:
1
2
3
4
5
6
7
8
9
10
Enter a number to search for:5
Found at index: 4

Tuesday, November 8, 2011

Move items one listbox to 2nd listbox [using j query]

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<script src="jquery-1.4.2.min.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
    $(document).ready(function () {
        $('#<%=btnMoveRight.ClientID %>').click(function ()
        {
            var selectedOptions = $('#<%=lstBox1.ClientID %> option:selected');
            if (selectedOptions.length == 0) {
                alert("Please select option to move");
                return false;
            }
            $('#<%=lstBox2.ClientID %>').append($(selectedOptions).clone());
            $(selectedOptions).remove();
            return false;
        });
        $('#<%=btnMoveLeft.ClientID %>').click(function () {
            var selectedOptions = $('#<%=lstBox2.ClientID %> option:selected');
            if (selectedOptions.length == 0) {
                alert("Please select option to move");
                return false;
            }
            $('#<%=lstBox1.ClientID %>').append($(selectedOptions).clone());
            $(selectedOptions).remove();
            return false;
        });
    });
</script>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    <asp:ListBox ID="lstBox1" runat="server" SelectionMode="Multiple" Height="138px"
            Width="108px">
            <asp:ListItem Text="A" Value="1"></asp:ListItem>
            <asp:ListItem Text="B" Value="2"></asp:ListItem>
            <asp:ListItem Text="C" Value="3"></asp:ListItem>
            <asp:ListItem Text="D" Value="4"></asp:ListItem>
            <asp:ListItem Text="1" Value="1"></asp:ListItem>
            <asp:ListItem Text="2" Value="2"></asp:ListItem>
            <asp:ListItem Text="3" Value="3"></asp:ListItem>
            <asp:ListItem Text="4" Value="4"></asp:ListItem>
</asp:ListBox>
<asp:Button ID="btnMoveRight" runat="server" Text=">>" />
<asp:Button ID="btnMoveLeft" runat="server" Text="<<" />
<asp:ListBox ID="lstBox2" runat="server" SelectionMode="Multiple" Height="134px"
            Width="104px">
            <asp:ListItem Text="E" Value="5"></asp:ListItem>
            <asp:ListItem Text="F" Value="6"></asp:ListItem>
            <asp:ListItem Text="G" Value="7"></asp:ListItem>
            <asp:ListItem Text="H" Value="8"></asp:ListItem>
</asp:ListBox>
    </div>
    </form>
</body>
</html>