Thursday 12 April 2012

Inheritance & Polymorphism (Part 1)

Its always nice to re-use something instead of creating it again & again. Since c-sharp is a pure Oop's oriented language its supports re-usability. To achieve re-usability we must create classes from existing one's. The process through which we can achieve this is known as Inheritance.It can be achieved in two ways

1.)Classical form.
2.)Containment form.

Inheritance represents represents a class of relationship b/w two classes

1.)Classical Inheritance:-

Simple Inheritance

2.)Containment Inheritance:-


Multiple Inheritance

Defining A Sub Class

     Class subclass-name : base class

           {

          variables dec;

          methods dec;

          }





Tuesday 10 April 2012

Ado.Net For Begginers -Connecting & Inserting Data To DataBase

Insertion Data In windows Form by Ado.Net For Begginers (Connecting & Inserting Data To DataBase<br /> By Using Try Catch Block & Sql Connection & Command Class Objects & Insert Command <br /> <br />Insertion Data In windows Form by Ado.Net For Begginers -Connecting &amp; Inserting Data To DataBase By Using Try Catch Block Sql Connection &amp; Command Class Objects & Insert Command

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.SqlClient;
using System.Data.Sql;
namespace AcceptButton
{
      public partial class Registration : Form
    {
        //Oblects for sql class
        SqlConnection cm;
        SqlCommand cmd ;
        public Registration()
        {
            //Connection String
            cm = new SqlConnection();
            cm.ConnectionString="Data Source=ONU-PC;Initial Catalog=AcceprtButton;User ID=sa; password=12345";
            cmd=new SqlCommand();
            cmd.Connection=cm;

            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //Object of form 1
            Form1 fr = new Form1();
            fr.Show();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }

        private void button9_Click(object sender, EventArgs e)
        {
                        try
            {
                cmd.CommandText = "insert into Ab values ( @username, @password ,@email,@Name ,@Mobileno)";
                cmd.Parameters.AddWithValue("@username", textBox1.Text);
                cmd.Parameters.AddWithValue("@password", textBox2.Text);
                cmd.Parameters.AddWithValue("@email", textBox4.Text);
                cmd.Parameters.AddWithValue("@Name", textBox5.Text);
                cmd.Parameters.AddWithValue("@Mobileno", textBox6.Text);
                cm.Open();
                if (cmd.ExecuteNonQuery() > 0)
                {
                    MessageBox.Show("Success");
                }
                else
                {
                    MessageBox.Show("Failed");
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Sry Exception");
            }
            finally
           {

            cm.Close();
           }
        }

        private void Registration_Load(object sender, EventArgs e)
        {

        }      
    }
}
In this MemReg:-Button 9
& Exit:-Button2

Thursday 5 April 2012

List Box In Window Form Using C#

 Simple Example Of List Box For Beginners


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;

namespace List_BoxAdd_RemoveBox
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                if (listBox1.Items.Count > 0)
                {
                    listBox2.Items.Add(listBox1.SelectedItem.ToString());
                    listBox1.Items.Remove(listBox1.SelectedItem);
                }
            }
            catch (Exception exx)
            {
                MessageBox.Show(exx.Message);
            }
            if (listBox1.Items.Count > 0)
            {
                listBox1.SelectedIndex = 0;
                listBox2.SelectedIndex = listBox2.Items.Count - 1;
            }

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            listBox1.Text = "";
           
        }

        private void button2_Click(object sender, EventArgs e)
        {
            try
            {
                if(listBox2.Items.Count>0)
                {
                    listBox1.Items.Add(listBox2.SelectedItem.ToString());
                    listBox2.Items.Remove(listBox2.SelectedItem);
                }
            }
            catch(Exception Exx)
            {
                MessageBox.Show(Exx.Message);
            }
            if(listBox2.Items.Count>0)
            {
                listBox2.SelectedIndex=0;
                listBox2.SelectedIndex=listBox2.Items.Count-1;
            }


        }
    }
}

Screen Shots:-