Showing posts with label program. Show all posts
Showing posts with label program. Show all posts

Sunday, 5 June 2016

How to: Delete records from SQL table in VB.Net

How to: Delete records from SQL table in VB.Net 




Code to delete a record from SQL table using VB.Net is as follows:


Note-
1: Double Click the delete button and copy the following code
2:And also change the server in connection string and the database also

Private Sub Button3_Click(sender As System.Object, e As System.EventArgs) Handles Button3.Click
        Try
            con.ConnectionString = "Server=.\SQLEXPRESS;Database=Employee Details;Trusted_Connection=True"
            con.Open()
            cmd.Connection = con
            cmd.CommandText = "Delete From emp_det where emp_id like @emp_id"

            cmd.Parameters.Add(New SqlParameter("@emp_id", TextBox1.Text))
            cmd.ExecuteNonQuery()
            If MessageBox.Show("Do you really want to Delete this Record?", "Delete", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) = DialogResult.No Then

                MsgBox("Operation Cancelled")

                Exit Sub

            End If
        Catch ex As Exception
            MessageBox.Show("Error while deleting record on table..." & ex.Message, "Delete Records")

        Finally

            con.Close()

        End Try

    End Sub






Allow Numbers only in a TextBox - Visual Basic .NET



So the code for allowing only numbers in the textbox for employee ID is:

Just copy the code in the box given below:
Private Sub TextBox1_KeyPress(ByVal sender As ObjectByVal As System.Windows.Forms.KeyPressEventArgs) Handles TextBox1.KeyPress
        If Asc(e.KeyChar) <> 13 AndAlso Asc(e.KeyChar) <> 8 AndAlso Not IsNumeric(e.KeyChar) Then
            MessageBox.Show("Please enter numbers only")
            e.Handled = True
        End If
End Sub

The Result:



Wednesday, 4 May 2016

Copying the array elements using pointers.

#include<iostream.h>
#include<conio.h>
void main()
{
int a[5],*p,i,b[5],*q;
clrscr();
p=&a[0];
q=&b[0];
cout<<"Enter elements:";
for(i=0;i<5;i++)
{
cin>>*p;
p++;
}
p=&a[0];
for(i=0;i<5;i++)
{
*q=*p;
p++;
q++;
}
q=&b[0];
cout<<"Copied String:";
for(i=0;i<5;i++)
{
cout<<" "<<*q;
q++;
}
getch();
}

Friday, 29 April 2016

Friend Function Example.

#include<iostream.h>
#include<conio.h>
class square
{
int no;
public:
void accept()
{
cout<<"Enter no for square:";
cin>>no;
}
friend void sq(square s);
};
void sq(square s)
{
int ans;
ans=s.no*s.no;
cout<<"Square="<<ans;
}
void main()
{
clrscr();
square s;
s.accept();
sq(s);
getch();
}
/*Output:
Enter no for square:4
Square=16 */

WAP for finding out the weight on any planet using static data member

#include<iostream.h>
#include<conio.h>
class planets
{
static float g;
float w,m;
int ch;
public:
void accept()
{
cout<<"Find weight on:"<<endl;
cout<<"1:MERCURY\n2:VENUS\n3:EARTH\n4:MARS\n5:JUPITER\n6:SATURN\n7:URANUS\n8:NEPTUNE"<<endl;
cout<<"Enter your choice:"<<endl;
cin>>ch;
switch(ch)
{
case 1:
g=3.61;
break;
case 2:
g=8.83;
break;
case 3:
g=9.8;
break;
case 4:
g=3.75;
break;
case 5:
g=26.0;
break;
case 6:
g=11.2;
break;
case 7:
g=10.5;
break;
case 8:
g=13.3;
break;
}
cout<<"Enter mass:";
cin>>m;
}
void display()
{
w=m*g;
cout<<"Weight="<<w;
}
};
float planets::g;
void main()
{
clrscr();
planets p;
p.accept();
p.display();
getch();
}
/*Output:
Find weight on:
1:MERCURY                                                                      
2:VENUS                                                                        
3:EARTH                                                                        
4:MARS                                                                        
5:JUPITER                                                                      
6:SATURN                                                                      
7:URANUS                                                                      
8:NEPTUNE                                                                      
Enter your choice:                                                            
3
Enter mass:10
Weight=98  */

Class with Static Data Member

#include<iostream.h>
#include<conio.h>
class account
{
static int r;
int p,n,si;
public:
void accept()
{
cout<<"Enter Principal amount & No. of years:"<<endl;
cin>>p>>n;
}
void display()
{
si=(p*n*r)/100;
cout<<"Simple Interest="<<si;
}
};
int account::r=10;
void main()
{
clrscr();
account a;
a.accept();
a.display();
getch();
}
/*Output:
Enter Principal amount & No. of years:
500 2                                                                          
Simple Interest=100 */

Thursday, 28 April 2016

Another example for inheritance

#include <iostream>
 
using namespace std;

// Base class
class Shape 
{
   public:
      void setWidth(int w)
      {
         width = w;
      }
      void setHeight(int h)
      {
         height = h;
      }
   protected:
      int width;
      int height;
};

// Derived class
class Rectangle: public Shape
{
   public:
      int getArea()
      { 
         return (width * height); 
      }
};

int main(void)
{
   Rectangle Rect;
 
   Rect.setWidth(5);
   Rect.setHeight(7);

   // Print the area of the object.
   cout << "Total area: " << Rect.getArea() << endl;

   return 0;
}

Write a program showing Friend Function

 #include<iostream.h>
 #include<conio.h>
 class b;
 class a
 {
int no1;
public:
void accept()
{
cout<<"Enter no 1:";
cin>>no1;
}
void display()
{
cout<<"No 1 is greater";
}
friend void great(a,b);
 };
 class b
 {
int no2;
public:
void accept()
{
cout<<"Enter no 2:";
cin>>no2;
}
void display()
{
cout<<"No 2 is greater";
}
friend void great(a,b);
 };
 void great(a m,b n)
 {
if(m.no1>n.no2)
{
m.display();
}
else
{
n.display();
}
 }
 void main()
 {
clrscr();
a m;
b n;
m.accept();
n.accept();
great(m,n);
getch();
}
/*Output:
Enter no 1:23
Enter no 2:21                                                                  
No 1 is greater */

Single Inheritance

#include<iostream.h>
#include<conio.h>
class employee
{
protected:
int emp_id;
char emp_name[20];
public:
void accept()
{
cout<<"Enter name and emp id=";
cin>>emp_name>>emp_id;
}
void display()
{
cout<<"Name="<<emp_name<<endl;
cout<<"ID="<<emp_id<<endl;
}
};
class worker:public employee
{
protected:
int salary;
public:
void accept1()
{
cout<<"Enter Salary=";
cin>>salary;
}
void display1()
{
cout<<"Salary:"<<salary<<endl;
}
};

void main()
{
clrscr();
worker w;
w.accept();
w.accept1();
w.display();
w.display1();
getch();
}
/*Output:
Enter name and emp id=qqq 123
Enter Salary=200                                                              
Name=qqq                                                                      
ID=123                                                                        
Salary:200                                                                    
*/
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               
                                                                               




Array Of Objects With Conditions

#include<iostream.h>
#include<conio.h>
class city
{
int pop;
char name[20];
public:
void accept()
{
cout<<"enter name and population"<<endl;
cin>>name>>pop;
}
void display();
}c[5];
void city::display()
{
int max=0,i;
for(i=0;i<5;i++)
{
if(max<c[i].pop)
max=c[i].pop;
}
for(i=0;i<5;i++)
{
if(max==c[i].pop)
{
cout<<"Max Populated city="<<c[i].name<<endl;
cout<<"Population="<<max;
}
}
}
void main()
{
int i;
clrscr();
for(i=0;i<5;i++)
{
c[i].accept();
}
c[i].display();
getch();
}
/*Output:
enter name and population
qqq 123                                                                      
enter name and population
ddd 456
enter name and population
ttt 677
enter name and population
uuu 876
enter name and population
ttg 667
Max Populated city=uuu
Population=876 */

Wednesday, 27 April 2016

What is Array Of Objects

Like, Array is a collection of variables of same data type, Similarly the Array Of Objects are the collection of objects of same class type.

Arrays of variables of type "class" is known as "Array of objects". The "identifier" used to refer the array of objects is an user defined data type.

Example:



#include <iostream.h>
const int MAX =100;
class Details
{
private:
int salary;
float roll;
public:
void getname( )
{
cout << "\n Enter the Salary:";
cin >> salary;
cout << "\n Enter the roll:";
cin >> roll;
}
void putname( )
{
cout << "Employees" << salary <<
"and roll is" << roll << '\n';
}
};
void main()
{
Details det[MAX];
int n=0;
char ans;
do{
cout << "Enter the Employee Number::" << n+1;
det[n++].getname;
cout << "Enter another (y/n)?: " ;
cin >> ans;
} while ( ans != 'n' );
for (int j=0; j<n; j++)
{
cout << "\nEmployee Number is:: " << j+1;
det[j].putname( );
}
}

Result:



Enter the Employee Number:: 1
Enter the Salary:20
Enter the roll:30
Enter another (y/n)?: y
Enter the Employee Number:: 2
Enter the Salary:20
Enter the roll:30
Enter another (y/n)?: n

Array Of Objects With Classes

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

class account
{
int accno;

public:
int balance;

void accept()
{
cout<<endl<<"Enter account no:";
cin>>accno;
cout<<"Enter balance:";
cin>>balance;

}

void display()
{
cout<<"-Balance after given interest of 10%:- "<<endl;
cout<<"Account no:"<<accno;
cout<<endl<<"Balance:"<<balance<<endl;
}
}a[10];

void main()
{
int i,balance;
clrscr();
for(i=0;i<10;i++)
{
a[i].accept();
if(a[i].balance>=5000)
{
a[i].balance=a[i].balance+(a[i].balance*0.1);
a[i].display();
}

}
getch();
}
/*Output:
Enter account no:45
Enter balance:5000
-Balance after given interest of 10%:-
Account no:45
Balance:5500

Enter account no:22
Enter balance:2000

Enter account no:48
Enter balance:200

Enter account no:33
Enter balance:100

Enter account no:66
Enter balance:777

Enter account no:88
Enter balance:100

Enter account no:19
Enter balance:9000
-Balance after given interest of 10%:-
Account no:19
Balance:9900

Enter account no:30
Enter balance:200

Enter account no:99
Enter balance:7777
-Balance after given interest of 10%:-
Account no:99
Balance:8554

Enter account no:77
Enter balance:1200

*/

Class and Objects Program

#include<iostream.h>
#include<conio.h>
class day        //class
{
int hrs,min,sec;
public:
void accept()
{
cout<<"Enter min:";
cin>>min;
}
void display()
{
if(min>60)
{
hrs=min/60;
min=min%60;
cout<<hrs<<":"<<min;
}
else
{
sec=min*60;
cout<<sec;
}
}
};
void main()
{
clrscr();
day d;     //object
d.accept();
d.display();
getch();
}
/*Output:
Enter min:138
2:18 */

Simple Example For Class

#include<iostream.h>
#include<conio.h>
class calender
{
private:
int day,month,year;
public:
void accept()
{
cout<<"Enter the day,month,year:";
cin>>day>>month>>year;
}
void display()
{
cout<<day<<"/"<<month<<"/"<<year;
}
};
void main()
{
calender c;
clrscr();
c.accept();
c.display();
getch();
}

/*OUTPUT:
Enter the day,month,year:2 4 2016
2/4/2016*/