Friday 21 February 2014

Static Data Member and Static Member Functions




STATIC DATA MEMBER

As we know Each object of a particular specified class consist of its own data . But if we declared Data item is as static data item then this static data item or we can simply say static variable or static member function will be available for global access for all objects of class . It means if you have declared a variable for member function as static then this can be accessed by all objects of class .

I mean to say if you have declared a variable as static then you have assigned it value '5'  through Obj1 object of class . Then if you have created a new object Obj2 and accessing value of static variable that it is still holding value '5' if it is accessed through Obj2 .


A Static Variable is visible or accessible only within the class or only by members of class But its lifetime is throughout the program means it can hold the same value throughout the lifetime of program


DECLARATION :-

                                  Static int stvar;

INITIALIZATION :- 

                
*                                     int abc : : stvar =10

        In this Stvar is initialized with value '10' and abc is class name . 

*              int abc : :  : stvar ;
In this abc is classname and now stvar will be assigned '0' value



SIMPLE PROGRAM TO SHOW USE OF STATIC VARIABLE


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

class static_var
{

static int count;    //static member of class
public :


void incr_staticvar()
{
count++;
}

 void outputc()
{
cout<<"Value of Static variable Count :- "<<count<<endl;
}
};

int static_function : : count;

void main()
{
clrscr();
static_var obj1,obj2,obj3,obj4;

obj1.incr_staticvar();
obj2.incr_staticvar();
obj3.incr_staticvar();
obj4.incr_staticvar();

cout<<"\nAfter Increment of static variable by Four Different objects is :-\n";

obj1.outputc ( );
obj2.outputc ( );
obj3.outputc ( );
obj4.outputc ( );


      getch();
}

OUTPUT :-




STATIC MEMBER FUNCTION



The Word Static means fixed . To  make a member function static you have to precede the word static to it .

Facts About Static Member Functions :-

*  The main usage of static functions is they are made to access static members of class or static variables of class that are declared in same class in which static member functions are declared

* These can be called outside class or in main function without using Object of class.



HOW TO CALL STATIC MEMBER FUNCTION


CLASS_NAME  : :  NAME_OF_FUNCTION

SIMPLE PROGRAM TO SHOW USE OF STATIC MEMBER FUNCTIONS


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

class static_function
{

int a;
static int count;    //Declaration of static Data member of class 

public :

static_function( )    // Constructor that is initializing variable a to 0
{
a=0;
}

void non_static( )    // public member function that is incrementing a variable
{
a++;
}

void outputn ( )  // public member function to cout value of a
{
cout<<"A :-  "<<a<<endl;
}

void incr_staticvar( )   // public member function to increment static variable
{
count++;
}

static void outputc( )  // public member function to cout value of static variable
{
cout<<"Value of Static variable Count :- "<<count<<endl;
}
};

int static_function :: count;

void main( )
{
clrscr();
static_function obj1,obj2,obj3,obj4;   //creation of four object of class static_function

cout<<"Initially ";
static_function : : outputc();     // calling static function

obj1.incr_staticvar();    //calling public funtion to increment static variable
obj2.incr_staticvar();
obj3.incr_staticvar();
obj4.incr_staticvar();



cout<<"\nAfter Increment of static variable by Four Different objects is :-\n";
static_function ::outputc();

obj1.non_static();    // function to increment a variable
obj2.non_static();
obj3.non_static();
obj4.non_static();

cout<<cout<<"After Increment of Variable 'A' by Four Different objects i :-";
obj1.outputn();
obj2.outputn();
obj3.outputn();
obj4.outputn();

      getch();

}

OUTPUT :-



write Program To Print All Permutations of given string

Combination programs require good understanding of mathematics . In combination or permutation concepts some formulas are used to identify how this combination of letters will be formed and how many combinations will be made . 

Formula for Permutation :- 
                                                N! / (N-r)!
Where N is the total number of characters that are available in your input string . 
Where r is total number of characters that will be taken at one time .

 Like if total number of characters in string is '5 ' and total number of characters taken at one time is '4' Then

Permutation = 5! / (5-4)!  = 120


Formula for Combination :- 
                                                      N! / r!  * (N-r)!
                                                 
Where N is the total number of characters that are available in your input string . 
Where r is total number of characters that will be taken at one time .

Program
In the following program we are making a program in C++ to print all possible combinations of  "abcd" string . As we can see we have to find permutation . So formula for it is :-

    N! / (N-r)!
    4! / (4-4)!  =24 
So 24 total combinations will be made from string "abcd" . There are four characters and 24 combinations . so each character will remain at one position for 24/4 time means 6 times. 

Program To Print all possible combinations of all characters of  "abcd" string .


#include< iostream.h >

#include< conio.h >
main ( )
{
clrscr ( );
int n=4;        // Here n is 4 so this programs prints all possible combinations for
char a[5];      // 4 character you can specify any value you want
cout<<"This program gives all possible combinations of four characters "<<endl;
cout<<"\nEnter Four Characters :-  ";

//Input four character from user in a [  ] array
for(int x=0;x<=3;x++)
{
 cin>>a[x];
}

cout<<"\nNow Combinations "<<" is :- \n"<<endl;

for(int z=0;z<=n-1;z++)
{
for(int j=0;j<=n-2;j++)
{
int temp;
temp=a[n-1];
a[n-1]=a[n-2];
a[n-2]=temp;
cout<<"\t";

for (int l=0;l<=n-1;l++)
{
 cout<<a[l];
 }

cout<<"\t";

temp=a[n-3];
a[n-3]=a[n-1];
a[n-1]=temp;

for ( int l2=0;l2<=n-1;l2++)
 {
cout<<a[l2];
 }
 }

 int temp1;
 temp1  =  a[0];
 a[0]  =  a[z+1];
 a[z+1]  =  temp1;

 cout<<"\n";
  }
getch();

}


OUTPUT :-



CODE EXPLANATION

cout<<"\nEnter Four Characters :-  ";

//Input four character from user in a [  ] array
for(int x=0;x<=n;x++)
{
 cin>>a[x];
}


In This Block of code we are getting input of four character whose combinations are generated by this program. In this we have used a for loop that consist of x counter that is initialized from 0 and it will continue upto 3 that allows user to enter 4 character to get their all possible combinations

Code below is logic of this program that given all possible combinations of entered characters
I have explained it with comments with it



for(int z=0;z<=n-1;z++)    // loop continues from 0 to 3 (  if n is 4 )
{
for(int j=0;j<=n-2;j++)       // loop continues from 0 to 2 (  if n is 4 )
{
{
int temp;
temp=a[n-1];
a[n-1]=a[n-2];
a[n-2]=temp;
cout<<"\t";

for (int l=0;l<=n-1;l++)
{
 cout<<a[l];
 }

cout<<"\t";

temp=a[n-3];
a[n-3]=a[n-1];
a[n-1]=temp;

for ( int l2=0;l2<=n-1;l2++)
 {
cout<<a[l2];
 }
 }

 int temp1;
 temp1  =  a[0];
 a[0]  =  a[z+1];
 a[z+1]  =  temp1;

 cout<<"\n";
  }

This code of block produces the all possible combinations of given input character by placing each character at all positions within the string each character including its original position within string will exchanged with or replaced with every position within the string so as to generate unique string each time

Saturday 15 February 2014

Algorithm and Implementation Program of Quick Sort



Quick Sort is a Sorting Algorithm based on Divide And Conquer Technique. In this at every step element is placed in its proper position.It performs very well on a longer list. It works recursively, by first selecting a random "Pivot value" from the list. Then it partitions the list into elements that are less than the pivot and greater the pivot and greater than the pivot. The problem  of sorting a given list is reduced to the problem of sorting two sublists It is to be noted that the reduction step in the quick sort finds the final position of particular element; which can be accomplished by scanning that last element of list from the right to left and check with the element.

Quick sort is Developed by C.A.B. Hoare. The main purpose of quick sort is to find the element that partitions the array into tow halves and to place it at its proper location in the array.


Working Of Quick Sort


Before we start the learning how quick sort works, we make some assumptions . Set the index of first element of array to LOC and LEFT variable and index of last element of the array to RIGHT variable. Then proceeds as follows :-

1. Start with element pointed to by Right. The array is scanned from right to left , comparing each element on the way with the element pointed by LOC till either.

         ( a ) Element smaller than the element pointed to by LOC is found and elements are interchanged . Procedure continues with Step2.

         ( b ) If the value of the RIGHT becomes equal to value of LOC, the procedure terminates here. This indicates that element is placed in its final position.

2.  Start with element pointed to by LEFT. The array is scanned from left to right, comparing each element on the way with the element pointed by LOC till either.

         ( a ) Element greater than the element pointed to by LOC is found . In this case the elements are interchanged and procedure continues with step 1.
         ( b ) If the value of LEFT becomes equal to value of LOC , athea procedure terminates. This condition indicates that the element is placed in its final position

At the end of this procedure the first element ( PIVOT ) of original array is placed in its final location in the sorted array. The elements to its left will be less than this element and element to the right will be greater than this element . Now whole procedure that we applied on complete file will be applied on first subfile ( first index to LOC-1) and second (LOC + 1 to last index ) until we get the sorted array


Algorithm : QUICK SORT


Consider an Array( A ) with N elements having LB as lowerbound and UB as upperbound.

ALGORITHM : QUICK_SORT(A, LB, UB )

Step 1      If         ( LB < UB ) then
                           P = SUB_ARRAY (A, LB, UB )
                          QUICK_SORT(A, LB, P-1 )
                          QUICK_SORT(A, P+1, UB )            
               Else
                 
                   // Print sorted Array

Step 2    End

The above function is using the Divide and conquer strategy for algorithms. In above step we are dividing a large available Array named 'A' into smaller arrays and then we are passing these smaller arrays to SUB_ARRAY function that sorts the small list . And get back to Quick_Sort function and then again the divide the remaining list into another portion of a small array and then again take it to SUB_ARRAY function to sort this small array list in this way full or all elements of array are sorted


ALGORITHM : SUB_ARRAY(A, LB, UB )

Step 1      Set LEFT = LB,
               Set RIGHT = UB,
               Set LOC = LB

Step 2     Repeat step 3 and 4

Step 3      // Scanning Array from Right To Left
               Repeat while A[LOC] <= A[ Right] and LOC != Right
               Right = Right -1
                          ( a ) If LOC == Right then
                                 Return LOC
                          ( b ) Interchange A [LOC] & A [RIGHT]
                          ( c ) Set LOC == RIGHT

Explanation :- In Step 3 Array 'A' is scanned starting from Rightmost element of array and going towards the Left of array . In this step this algorithm finds the element which is greater the element at A[LOC] or pivot element and  when element which is greater than pivot is found it is replaced with A[LOC] and LOC is set the Location of element with which we have replaced the location it means A[RIGHT] and else the Right is decremented by 1 until it comes to location same as LOC

Step 4     // Scanning Array  from LEFT to Right
               Repeat while A[LOC] >= A[LEFT] and LOC != LEFT
               LEFT = LEFT +1
               ( a ) If LOC == LEFT then
                       Return LOC
               ( b ) Interchange A[LOC] and A[LEFT]
               ( c ) Set LOC = LEFT

Explanation :- In Step 4 Array 'A' is scanned starting from Leftmost element of array and going towards the Right of array . In this step this algorithm finds the element which is smaller the element at A[LOC] or pivot element and  when element which is smaller than pivot is found it is replaced with A[LOC] and LOC is set the Location of element with which we have replaced the location means A[LEFT].

Step 5      End


Implementation of Quick Sort Algorithm in Cplusplus ( c++ ) Program



#include< iostream.h >
#include< conio.h >
int a[20], n, lb, loc, ub, left, right, temp, temp1;

void quicksort(int[10],int,int);

int pivot(int[],int,int);

void main()
{
cout<<"Enter size of array";
cin>>n;
cout<<"Enter Array Elements ";
for(int i=0;i<n;i++)
{
cin>>a[i];
}
quicksort(a,0,n-1);
for(int z=0;z<n;z++)
{
cout<<" "<<a[z];
}
getch();
}
void quicksort(int a[], int lb, int ub)
{
int p;
if(lb<ub)
{
p=pivot(a,lb,ub);
quicksort(a,lb,p-1);
quicksort(a,p+1,ub);
}
}
int pivot( int a[],int lb,int ub )
{
for(int z=0;z<n;z++)
{
cout<<" "<<a[z];
}
cout<<endl;
left =lb;
right = ub;
loc  =lb;
cout<<"right is :- "<<right;
cout<<"\tloc is :-"<<loc;
cout<<"left is :- "<<left;
 cout<<"Now right \n";
while((a[loc]<=a[right]) && (loc!=right))
{
right=right-1;
}
if(loc==right)
{
return loc;
}
temp=a[loc];
a[loc]=a[right];
a[right]=temp;
loc=right;
cout<<"Now left \n";
while((a[left]<=a[loc]) && (loc!=left))
{
left=left+1;
}
if(loc==left)
{
return loc;
}
temp1=a[loc];
a[loc]=a[left];
a[left]=temp1;
loc=left;





}



OUTPUT QUICK SORT PROGRAM
quick sorting technique with quick sort algorithm programming
Output of Quick Sort Program

For any type of more explanation to this program and for any suggest to this post you can freely comment . your comment are very crucial to us.

Sunday 9 February 2014

C program to shutdown restart logoff hibernate computer in windowsxp windows7 and ubuntu

C program To Shutdown Computer in Windows xp

This is a program to Shutdown your computer by using C programming language code. This code will first ask you for the confirmation whether youwant to shutdown the computer or not. Depending on confirmation passed by user This program shutdown the computer or exit from executionif user don't want to shutdown computer. Firstly it will asks you
 to shutdown your computer if you press 'y' the  your computer will shutdown in 30 seconds, 


Logic Behind This program


In this program we have used library file that invoked or calls function of  "stdlib.h". This function in stdlib.h lubrary is used to  run an exe file "shutdown.exe" which is availiable in directory path
"C:\WINDOWS\system32"

Windows XP provides numerous options to use or parameters to pass to shutdown.exe 
while executing shutdown.exe 

For Example
  -s option shutdown the computer after 30 seconds

  -t :- This option allows you to specify the time along with shutdown command to specify after how much time computer will be shutdown If you want to shutdown your computre immediately then you canuse  "shutdown -s -t 0"

  Where -s parameter is stands for shutdown it denotes or pass parameter to shutdown.exe file to denotes shutdown.exe that user wants to shutdown this computer

"-t 0"  this denotes we are specifying time so that after that time computer must be shutdown. you can also specify time you want

-r This paramter is used to restart your computer


#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now )\n");
   printf("Press Y for Yes\n");
   printf("Press N for No\n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown -s");

   return 0;
}


C program To Shutdown Computer in Windows 7

#include <stdio.h>
#include <stdlib.h>

main()
{
   char ch;

   printf("Do you want to shutdown your computer now \n");
   printf("Press Y for Yes\n");
   printf("Press N for No\n");
   scanf("%c",&ch);

   if (ch == 'y' || ch == 'Y')
      system("C:\\WINDOWS\\System32\\shutdown /s");

   return 0;
}


C program To Shutdown Computer in Ubuntu


To Run this program in Ubuntu you must be logged in as root user
#include <stdio.h>

int main() {
  system("shutdown -P now");
  return 0;



C Program To Shutdown Restart Logoff and Hibernate Program


The below availibale program can be used to shutdown your computer , to restart, logoff and hibernate you computer using c programming language

#include<stdio.h>
#include<dos.h>
#include<stdlib.h>
#include<conio.h>

void main()
{
int ch;
clrscr();
printf("\n\n------------SHUTDOWN MENU----------------\n");
printf("Press1 For Shutdownn\nPress2 For Restart\n Press3  For Logoff\n Press4  for Hibernate\n5.exit”");
printf("\nEnter Your choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:system("shutdown -s");
break;
case 2:system("shutdown -r");
break;
case 3:system("shutdown -l");
break;
case 4:system("shutdown -h");
break;
case 5:exit(1);
break;
default:printf("Invalid choice");
}
getch();
}

Happy Coding 

Don't Forget To Share This Post on Social Networks like Facebook , Twitter. Share your questions and how you like this post

Tuesday 28 January 2014

selection sorting

 SELECTION SORT IN C++


Searching and sorting are popular in elementry data structures. Although now a days many advanced sorting techniques are used to sort a set of data, the basic sorting methods are still popular and are the used frequently.

The algorithm works is selection sort works by finding the smallest element in the list and by assuming that we are doing sorting in ascending order we replace that smallest found element with first element of list in this way we put the smallest element of list at first  position . Then we go on repeating this task for rest of elements. We then find the second smallest element in the list and then replace it with second element of list and thus we have our first two elements of list in sorted order of ascending order .


 I have given  C++ code for selection sort . The algorithm for selection sort is quite simple. In the first iteration, 1st element is compared against all the other elements (from array index 1 to array index n). In the second iteration 2nd element is compared with the elements from array index 2 to n. This process is repeated n-1 times.

 Disadvantages of Selection Sort
  • It is slow as the sorting process is too long and time consuming so it is mainly used for sorting when N (number of elements is less)


 Advantages of Selection Sort


  • Selection sort is easy to implement and does not require additional storage
  • It implements better on small lists




Code For Selection Sort
//It is for 10 elements and to change number of elements change 10 arguement in smallest function call


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

int small , a1=0 , loc;
int a[ 10 ];
int main( )
{
int n, temp, temp1, count, compare;
int smallest( int , int ) ;
clrscr( );
cout<<"enter number of elements you want to sort";
cin>>n;
count=n-1;
for(int i=0; i<=n-1; i++)
{
cout<<"ARRAY ["<<i<<"] :- ";
cin>>a[ i ];
}
for(int j1=0;j1<=n-1;j1++)
{
compare = smallest(10,a1);            // call smallest() function to calculate smallest element
temp1 = a[ j1 ];
a[ j1 ] = a[ loc ];
a[ loc ] = temp1;
a1 = a1 + 1;
}
cout<<endl;
cout<<" sorted array is ";cout<<endl;

for(i=0;i<=n-1;i++)                                      // Print sorted Array 
{
cout<<"ARRAY ["<<i<<"] :- ";
cout<<a[i];cout<<endl;
}
 getch();
}
int smallest(int n,int a1)      // function to calculate smaller number
{
for(int k=a1;k<=a1;k++)
{
small=a[k];                       // smaller is assigned first element
for(int j=k+1;j<=n-1;j++)
{
if(small>a[j])                       // if value smaller than small variable found
                                             then small = a[j] 

{                                                      
small = a[j];
loc = j;                                 // and location of smallest loc = j
}
}
}
return(loc);                              //return location of smallest element
     }



Sunday 26 January 2014

Error dnndev/desktopmodules/ could not be found

http://geeksprogrammings.blogspot.com/2014/01/donetnuke.html
geeksprogrammings.blogspot.in

 DotNetNuke is one of best Web Content Management System Based on Microsoft .Net. The main ease is it's community Edition is OpenSource. After successfull installation of DotnetNuke if you want to develop your own modules for Dotnetnuke .you require following things

1. Microsoft Visual Studio
Download Visual Studio 2010

2. Microsoft Sql Server
Download SqlServer 2008 32/64 Bit

3 DotNetNuke C# Compiled Module  OR DotNetNuke Vb# Compiled Module
DotNetNuke C# / Vb Compiled Module For vs 2010

First I will recommend if you are using only one website of dotnetnuke at one time one one Host or computer . Put all your website files in C:\inetpub\wwwroot that will be ease not put it in folder in wwwroot as it also works in folder but there could be easy url if you keep them in wwwroot


Configure Host File

1. Press windowskey +r from keyboard
2. In Run dialog Box typein "c:\WINDOWS\system32\drivers\etc" and Hit Enter
3. Now in New window opened right click on hosts file and open it in your favourite notepad editor
4. Now put the following line in it and it looks something like in image below
Put this line :-
127.0.0.1       dnndev
5. Save this file and close it
http://geeksprogrammings.blogspot.com/2014/01/donetnuke.html
geeksprogrammings.blogspot.in

                      e.g. Hosts file

6. Now check it its done properly
7. To check it open any browser Typein- "dnndev/" and Hit enter
8. if your dotnetnuke site opens then host file configuration done.


Installation of C# Compiled Module Development 

1. Download DotNetNuke C# Compiled Module  OR DotNetNuke Vb# Compiled Module
DotNetNuke C# / Vb Compiled Module For vs 2010

2. Installing the Visual Studio Template is very straight forward.
3. First close all instances of VS2008  
4. Now copy the ZIP file that you downloaded from Codeplex in previous step
5. Now  paste this ZIP file into a very specific folder.
6. Open the following folder "My Documents\Visual Studio 2008\Templates folder". 
7. Within that folder go into ProjectTemplates\Visual C#\
8. within the Visual C# folder  simply create a new folder Web folder
9. Place zip file in web folder
10.  Now open  visual studio 2010 and click on file -> New Project 
11. christoc.com C# compiled module template give filename and click ok

ERROR

The Web Application Project hello is configured to use IIS. The Web server "http://dnndev/desktopmodules/hello/' could not be found."


Solution

Now we will do configuration for module development. check Which Version of IIS you are using .Follow the steps below to know which version of IIs you are using

Steps To Know IIS Version

1. Press windowskey +r from keyboard
2. In Run dialog Box typein "inetmgr" and Hit Enter
3. Now click on Help menu item in Menu Bar
4. click on About Internet Information Services
5. This gives version of IIs you are using like in image below
http://geeksprogrammings.blogspot.com/2014/01/donetnuke.html
geeksprogrammings.blogspot.in



6. Now depending on version using steps below


Configure Bindings In IIS 5

1. Press windowskey +r from keyboard
2. In Run dialog Box typein "inetmgr" and Hit Enter
3. This opens Internet Information Services
4. Right click on Default WebS ite
5. click on Properties. This opens Default Website Properties

http://geeksprogrammings.blogspot.com/2014/01/donetnuke.html

6. Focus on Web Site Tab under Web Site Identification Section
7. In this section you see Advance Button after Ip Address Label
8. click on Advanced Button
9. It opens Advanced Multiple Web Site configuration window
11. 10. Now click on Add button under Multiple identities for this Web site
http://geeksprogrammings.blogspot.com/2014/01/donetnuke.html
geeksprogrammings.blogspot.in


12. Put Ip Address field equals to All Unassigned
13. Put TCP Port equals to 80
14. Put Host Header Name equals to dnndev
15. click ok ....again click ok on other window
16. At last click apply and ok to close properties window


Configure Bindings In IIS 7

1. Press windowskey +r from keyboard
2. In Run dialog Box typein "inetmgr" and Hit Enter
3. This opens Internet Information Services
4. In the Connections pane, expand the server name, expand Sites, and then click the Web site on which you want to configure the bindings.
5. In the Actions pane, click Bindings
6. In the Site Bindings dialog box, click Add.
7. In the Add Site Binding dialog box, add the binding information
to solve dotnetnuke error add binding to iis
geeksprogrammings.blogspot.in


8.Put Ip Address field equals to All Unassigned
9. Put Host Header Name equals to dnndev
10 click ok

For any more problem or for Configure Bindings In IIS 7 or other problem you can comment here i am here to help you anytime

Tuesday 22 October 2013

Datagridview Cellvaluechanged Event VB.net



DatagridView is a windows forms grid Control . It was first introduced in 
.Net framework 1.0 and It comes with advanced and improved features in .Net Framework 2.0  It allows you to show the data in the tabular form. It contains data organized in rows and columns. It can be used to retrieve data in Tabular from from Database . It can be bound to Sql Database or Microsoft-Access Database.


     
Datagridview Cellvaluechanged Event in VB.net is used to make the change to occur or to call an event when value within particular cell is changed. In this app I have put event on each . when value of cell is changed its corresponding value in Maskedtextbox will change


Download Datagridview cellvaluechanged App



Design For Datagridview Cellvaluechanged Event VB.net


-- Add DatagridView To Form


1. Click on Tools


2. Then Scroll down and Find Datagridview .






3. Now Double click on it to put it on windows form.




-- Add Columns To Datagridview

1. Single Click on Datagridview


2. Click on small arrow on Top-right of Datagridview


3. A pop-up Menu appears. Click on "Add Column"





4. Now in Add Column Dialog Box Change HeaderText to "Name" and Click on Add . This Adds column Name to Datagridview


5. Now in second column give HeaderText to "Salary"


6. Now in Third column give HeaderText to "Bonus"


7. Now in Fourth column give HeaderText to "Total Salary"


8. Now Add Four Labels to Form and change its text property to :-


( I ) Name

( II ) Salary
( III )Bonus
( IV )Total Salary

9. Take four Masked Textbox from Toolbox and drag them to Windows form






10. Take a Button and Change its Text Property to "Get Data".





How To Operate


1 click on Get Data Button


2. It will load Default data in datagridview


3. If you change data in any Datagridview column Then the corresponding value in MaskedTextbox also change




Code For Datagridview 


Cellvaluechanged Event vb.net App



Public Class Form5 Public isdirty As Boolean Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click DataGridView1.Rows(0).Cells(0).Value = "Jorge" DataGridView1.Rows(0).Cells(1).Value = 12000 DataGridView1.Rows(0).Cells(2).Value = 2900 DataGridView1.Rows(0).Cells(3).Value = DataGridView1.Rows(0).Cells(1).Value + DataGridView1.Rows(0).Cells(2).Value End Sub Private Sub MaskedTextBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox1.TextChanged DataGridView1.Rows(0).Cells(0).Value = MaskedTextBox1.Text End Sub Private Sub MaskedTextBox2_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles MaskedTextBox2.TextChanged DataGridView1.Rows(0).Cells(1).Value = MaskedTextBox2.Text DataGridView1.Rows(0).Cells(3).Value = Val(DataGridView1.Rows(0).Cells(1).Value) + Val(DataGridView1.Rows(0).Cells(2).Value) End Sub Private Sub MaskedTextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaskedTextBox3.TextChanged DataGridView1.Rows(0).Cells(2).Value = MaskedTextBox3.Text DataGridView1.Rows(0).Cells(3).Value = Val(DataGridView1.Rows(0).Cells(1).Value) + Val(DataGridView1.Rows(0).Cells(2).Value) End Sub Private Sub MaskedTextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MaskedTextBox4.TextChanged DataGridView1.Rows(0).Cells(3).Value = MaskedTextBox4.Text DataGridView1.Rows(0).Cells(3).Value = Val(DataGridView1.Rows(0).Cells(1).Value) + Val(DataGridView1.Rows(0).Cells(2).Value) End Sub Private Sub EndEdit(ByVal sender As System.Object, ByVal e As EventArgs) Handles DataGridView1.CurrentCellDirtyStateChanged If DataGridView1.IsCurrentCellDirty Then DataGridView1.CommitEdit(DataGridViewDataErrorContexts.Commit) End If End Sub Private Sub DataGridView1_TextChanged(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView1.CellValueChanged If e.RowIndex = -1 Then isdirty = True End If If e.ColumnIndex = 0 Then MaskedTextBox1.Text = DataGridView1.Rows(0).Cells(0).Value End If If e.ColumnIndex = 1 Then MaskedTextBox2.Text = DataGridView1.Rows(0).Cells(1).Value DataGridView1.Rows(0).Cells(3).Value = Val(DataGridView1.Rows(0).Cells(1).Value) + Val(DataGridView1.Rows(0).Cells(2).Value) End If If e.ColumnIndex = 2 Then MaskedTextBox3.Text = DataGridView1.Rows(0).Cells(2).Value DataGridView1.Rows(0).Cells(3).Value = Val(DataGridView1.Rows(0).Cells(1).Value) + Val(DataGridView1.Rows(0).Cells(2).Value) End If If e.ColumnIndex = 3 Then MaskedTextBox4.Text = DataGridView1.Rows(0).Cells(3).Value Dim c As Integer = DataGridView1.Rows(0).Cells(3).Value Dim str As String = CInt(c) If Len(str) = 1 Then MaskedTextBox4.Mask = "0" ElseIf Len(str) = 2 Then MaskedTextBox4.Mask = "00" ElseIf Len(str) = 3 Then MaskedTextBox4.Mask = "0,00" ElseIf Len(str) = 4 Then MaskedTextBox4.Mask = "0,000" ElseIf Len(str) = 5 Then MaskedTextBox4.Mask = "00,000" ElseIf Len(str) = 6 Then MaskedTextBox4.Mask = "0,00,000" ElseIf Len(str) = 7 Then MaskedTextBox4.Mask = "00,00,000" End If End If End Sub Private Sub Form5_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load MaskedTextBox4.Mask = "0000000" '' End Sub End Class




OUTPUT :-




After Changing value of Cell of Datagridview :- 




Friday 11 October 2013

Masked TextBox in .NET

Masked TextBox Control in .NET are used to Restrict the input from user . Masked Edit controls are also used to change the format of output that is to be delivered . In vb 6 we have Mask Edit control But in VB.NET we can get Maskedtextbox 
from Toolbox. 

Saturday 5 October 2013

Connect Access Database with C#


Connect Access Database with C#

To connect Database with your Windows Application to make Application for querying the database and retrieve the desired result from database . Database in widely used in management software's . Them main purpose of making Management software is to store the database of the work being done in the organisation in to a secure place and easily accessible place .

Database dependent applications are widely used in the market . ERP projects of organisation are highly Database dependent applications . The main two types of database that are widely used in .NET Applications :-

* Microsoft Access Database
* MSSQL Server ( Microsoft Structured Query Language )


The main Languages used in .NET Platform for connecting windows application to database are :-

* C#
* VB#

Microsoft Access Database is most simplest approach to connect your windows application to database . Microsoft is easy to use and very effective database Software that uses simple Query approach.  In old Microsoft Access Database is used with VB 6 to connect VB 6 Applications to Database and Microsoft also extend it for VB.NET to connect .NET Platform applications to Database

DOWNLOAD This Demo To Connect Access Database with C#


Steps to Make Microsoft Access Database:

1. Go to desired location you want to make your Database
2. Right click then click on new and 
3. Then click Microsoft Access 2007 Database
4. Create Desired Table with required Fields 
5. Save it as Microsoft Access database 2003 format 
6. Click OK 

DOWNLOAD THIS Demo Database To Connect Access Database with C#


Steps To Make This Demo Project :-


1. Start your Visual Studio 2010 
2. At start page click on Create New Project 
3. In Dialog Box provide Project name and path where to save this project and click ok
4. Now from Toolbox Drag four Textboxes 
5. Drag 1 Button and make design as show in below image

Design of Form 1 


6. Now copy the code given in code section below :-


Code To Connect Access Database with C#



using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;
namespace access_db_csharp
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        OleDbConnection con=new OleDbConnection ();
        OleDbCommand cmd=new OleDbCommand ();
        string connectionstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\test.mdb";
                
               private void button1_Click(object sender, EventArgs e)
        {
            cmd.CommandText = "insert into info_table values (" + textBox1 .Text + ",'" + textBox2.Text + "','" + textBox3.Text + "'," + textBox4.Text + ")";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            MessageBox.Show("Done");


                    }

        private void Form1_Load(object sender, EventArgs e)
        {
            con.ConnectionString = connectionstring;
            con.Open();
        }
    }
}

Explanation of Code


Imports System.Data.OleDb


This line of code tells we are importing Oledb Namespace. Oledb stands for Object Linking and Embedding . For using Access database with .net applications we have to inherit classes from this oledb Namespace . Oledb Namespace contains all classes that are required to connect vb.net/c# application to Microsoft Access
Database




Public Class Form1
    Dim connection As New OleDbConnection
    Dim command As New OleDbCommand
    Dim ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & "C:\test.mdb
End Class

Now In These lines of code we declared 3 things
1. OledbConnection :- OledbConnection is a class contained in System.Data.Oledb Namespace and it is used to make connectivity to Access Database . It receives the connection string that tells the path of Microsoft Access Database to connect to it. In this line of code we have created instance of Oledbconnection class

2. OledbCommand :- OledbCommand is a class contained in S
ystem.Data.Oledb Namespace and it is used to define or specify the command that we are going to execute on Microsoft Access Database. In this line of code we are creating instance of OledbCommand to use this instance in further code

3. Connection string :- Now we are creating a variable named connectionString that will receive the string or path that tell how we connect to our Access database. It receives two parameters :-

Provider=Microsoft.Jet.OLEDB.4.0 -- 
Data Source=" & "C:\test.mdb

Now Provider is main part here in connection string . Provider is different for different approaches used for connecting data to database . The Connection string contains information that the Provider needs to know to connect to database Once connected then rest of job is done by provider


    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        connection.ConnectionString = ConnectionString
        connection.Open()
    End Sub

Now in this block of code we have initialized our OledbConnection instance with connection string variable that we have initialized in previous step. So in this code we have given the Oledbconnectin instance means connection some information about Provider to use and location of database file on computer

Then we have used connection.open() method opens port for Enter into Access Database. this opens a pipe to execute query in database 

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        command.CommandText = "insert into info_table values (" & TextBox1.Text & ",'" & TextBox2.Text & "','" & TextBox3.Text & "'," & TextBox4.Text & ")"
        command.CommandType = CommandType.Text
        command.Connection = connection
        command.ExecuteNonQuery()
        MsgBox("Record inserted ")
    End Sub


Now at last we have used insert query that is coded on Button Click Event . In this code I have told which commandtext to choose and what will be commandType test or storeprocedure . and also we have initialized the command with Oledbconnection instance. Then we have execute command using ExecuteNonQuery() method. This method returns the number of rows affected the database 

Tuesday 1 October 2013