Wednesday, 22 June 2011

Hosting coupons

How to Create Read only comboBox in C#


When you  create read only comboBox then you use two Events 

  1. KeyPress
  2. KeyDown

1.KeyPress Event:-
                                 Keypress Event arguments handled property is true.this property to all keypress object is handled and event is fire to no argument is apply all is handled by e.
e is a object that handled events.


2.KeyDown:-
                        In KeyDown Events all object handled by e object like Ctrl + C ,key arrows,keysdata etc are handled to this event to read only comboBox in C# this cod is very useful to this problem.
------------------------------ See this source code----------------------------------

  private void comboBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            e.Handled = true;
           
        }


        private void comboBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyData == (Keys)Shortcut.CtrlC)
            {
                Clipboard.SetData(DataFormats.Text, comboBox1.SelectedText);
            }
            else if (e.KeyData == Keys.Left || e.KeyData == Keys.Right || e.KeyData == Keys.Up || e.KeyData == Keys.Down) { }
            else
            {
                e.Handled = true;
            }
        }

Thursday, 16 June 2011

How to compare two tables - SQL Server

Sample SQL statements to compare data in two tables with identical structure.
(Statements work for MS SQL Server, as well as for many other databases.)
To find records which exist in source table but not in target table:
SELECT * FROM t1 WHERE NOT EXISTS (SELECT * FROM t2 WHERE t2.Id = t1.Id)
or
SELECT * FROM t1 LEFT OUTER JOIN T2 on t1.Id = t2.Id WHERE t2.Id IS NULL
If the primary key consists of more than one column, you can modify SQL statement:
SELECT Id, Col1 FROM t1 WHERE NOT EXISTS
(SELECT 1 FROM t2 WHERE t1.Id = t2.Id AND Col1.t1 = Col2.t2)
On SQL Server 2005 or newer you can use the EXCEPT operator:
SELECT Id, Col1 FROM t1 EXCEPT SELECT Id, Col1 FROM t2
To find records which exist in source table but not in target table,
 as well as records which exists in target table but not in source table:
SELECT * FROM (SELECT Id, Col1 FROM t1, 'old'
UNION ALL
SELECT Id, Col1 FROM t2, 'new') t
ORDER BY Id
Note: For tables with large amounts of data UNION statement might be very slow. 

Friday, 10 June 2011

Software development objective

The aim of software engineering is to find methods for developing high quality software products at a reasonable cost. As more and more computers are being used in areas in which a malfunction of the system can be a source of serious losses or disturbances to the functioning of the society, the quality of software becomes a more and more critical factor of business success, human security, and safety. Examples of such application areas are enterprise management, public administration, and social insurance or post delivery services. The quality of services offered to the society depends on the quality of software systems that support the functioning of the respective public or private organizations

Wednesday, 11 May 2011

How to Set Current Date in Date Time Picker

 Date Time Picker Set Current Date
  1. Open Page load Events
  2. Then DataTime Class  set instance <DateTime  dt ;>
  3. Then this object set to dateTimePicker value<dateTimePicker.Value=dt;>

    Here Example
      private void Student_Entry_Load(object sender, EventArgs e)
            {
                this.Load += new System.EventHandler(Student_Entry_Load);
                DateTime dt = DateTime.Now;
                dateTimePicker1.Value  = dt;
    }
        
     

Saturday, 7 May 2011

Age or Date Calculation in C#


Age calculation in combo box fetching in C# Windows Application

Dear friends this is critical problem to mange date and calculation age of any person
thus i develop a code to solve this problem.who is interested take use easily.

Following Steps Follows:-

1.Create three combo box for date,month and year.
2.Then create function for maintain date and a year table of database bind with year combo box.
3.Then create four list of date with 28,29,30,31 days in method.
4.Then create a list of month in month combo box.
5.After then a switch case statement use for show listed in different types days in combo box so it is with changed event of year and month.
6.If we want to calculating Age then following Code See:-

private void button1_Click(object sender, EventArgs e)
        {
            string mth;
            double  age;
        year = Convert.ToInt32(comboBox7.Text);
        mth = comboBox6.SelectedValue.ToString();
           
        string a = comboBox7.Text + "." + mth;
        double  op = Convert.ToDouble(a);
        age =2011.7-op ;
       
        textBox1.Text = age .ToString ();
        }
Thus you calculate age of any person easily .

Here Example:-
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;
namespace Date_of_Birth
{
    public partial class Form1 : Form
    {
        SqlConnection con = new SqlConnection("Data Source=.\\sqlexpress;Initial Catalog=Enterprises;Integrated Security=True");
        static SqlDataAdapter ad1 = new SqlDataAdapter();
        private BindingSource bindingSource1 = new BindingSource();
   
        private void GetData(string selectCommand)
        {
            try
            {
                ad1 = new SqlDataAdapter(selectCommand, con);
                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(ad1);
                DataTable table = new DataTable();
                table.Locale = System.Globalization.CultureInfo.InvariantCulture;
                ad1.Fill(table);
                bindingSource1.DataSource = table;
                comboBox6.DisplayMember = "Month";
                comboBox6.ValueMember = "id";
            }
            catch (SqlException)
            {
                MessageBox.Show("To run this example, replace the value of the " + "\n" +
                    "ConnectionString variable with a connection string that is " +
                    "valid for your system.");
            }
        }
        public Form1()
        {
            InitializeComponent();
        }
        int year;


        private void comboBox7_SelectedIndexChanged(object sender, EventArgs e)
        {
            year = Convert.ToInt32(comboBox7.Text);          
        }

        private void Form1_Load(object sender, EventArgs e)
        {

            this.Load += new System.EventHandler(Form1_Load);
            comboBox6.DataSource = bindingSource1;
            GetData("Select * From Month ");//where ((Name='" + textBox1.Text + "')AND(Phone_No='" + textBox2.Text + "'))");          
         

        }
        private void comboBox6_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox7.Text.Length < 2)
            {            
                comboBox7.Focus();
            }
            else if (comboBox6.Text.Length < 2)
            {
                MessageBox.Show("Select Month");
                comboBox6.Focus();
            }
            else
            {
                string month = comboBox6.Text;
                year = Convert.ToInt32(comboBox7.Text);
                switch (month)
                {
                    case "January":
                        comboBox6.SelectedValue = "1";
                        fullDay();
                        break;
                    case "February":
                        if (year % 4 == 0)
                        {
                            febFullDay();
                        }
                        else
                        {
                            febShortDay();
                        }
                        break;
                    case "March":
                        fullDay();
                        break;
                    case "April":
                        shortDay();
                        break;
                    case "May":
                        fullDay();
                        break;

                    case "June":
                        shortDay();
                        break;
                    case "July":
                        fullDay();
                        break;
                    case "August":
                        fullDay();
                        break;
                    case "September":
                        shortDay();
                        break;
                    case "October":
                        fullDay();
                        break;
                    case "November":
                        shortDay();
                        break;
                    case "December":
                        fullDay();
                        break;
                }
            }

        }

        private void comboBox5_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        void febShortDay()
        {
            comboBox5.Items.Add("1");
            comboBox5.Items.Add("2");
            comboBox5.Items.Add("3");
            comboBox5.Items.Add("4");
            comboBox5.Items.Add("5");
            comboBox5.Items.Add("6");
            comboBox5.Items.Add("7");
            comboBox5.Items.Add("8");
            comboBox5.Items.Add("9");
            comboBox5.Items.Add("10");
            comboBox5.Items.Add("11");
            comboBox5.Items.Add("12");
            comboBox5.Items.Add("13");
            comboBox5.Items.Add("14");
            comboBox5.Items.Add("15");
            comboBox5.Items.Add("16");
            comboBox5.Items.Add("17");
            comboBox5.Items.Add("18");
            comboBox5.Items.Add("19");
            comboBox5.Items.Add("20");
            comboBox5.Items.Add("21");
            comboBox5.Items.Add("22");
            comboBox5.Items.Add("23");
            comboBox5.Items.Add("24");
            comboBox5.Items.Add("25");
            comboBox5.Items.Add("26");
            comboBox5.Items.Add("27");
            comboBox5.Items.Add("28");
        }
        void febFullDay()
        {
            comboBox5.Items.Add("1");
            comboBox5.Items.Add("2");
            comboBox5.Items.Add("3");
            comboBox5.Items.Add("4");
            comboBox5.Items.Add("5");
            comboBox5.Items.Add("6");
            comboBox5.Items.Add("7");
            comboBox5.Items.Add("8");
            comboBox5.Items.Add("9");
            comboBox5.Items.Add("10");
            comboBox5.Items.Add("11");
            comboBox5.Items.Add("12");
            comboBox5.Items.Add("13");
            comboBox5.Items.Add("14");
            comboBox5.Items.Add("15");
            comboBox5.Items.Add("16");
            comboBox5.Items.Add("17");
            comboBox5.Items.Add("18");
            comboBox5.Items.Add("19");
            comboBox5.Items.Add("20");
            comboBox5.Items.Add("21");
            comboBox5.Items.Add("22");
            comboBox5.Items.Add("23");
            comboBox5.Items.Add("24");
            comboBox5.Items.Add("25");
            comboBox5.Items.Add("26");
            comboBox5.Items.Add("27");
            comboBox5.Items.Add("28");
            comboBox5.Items.Add("29");
        }
        void shortDay()
        {
            comboBox5.Items.Add("1");
            comboBox5.Items.Add("2");
            comboBox5.Items.Add("3");
            comboBox5.Items.Add("4");
            comboBox5.Items.Add("5");
            comboBox5.Items.Add("6");
            comboBox5.Items.Add("7");
            comboBox5.Items.Add("8");
            comboBox5.Items.Add("9");
            comboBox5.Items.Add("10");
            comboBox5.Items.Add("11");
            comboBox5.Items.Add("12");
            comboBox5.Items.Add("13");
            comboBox5.Items.Add("14");
            comboBox5.Items.Add("15");
            comboBox5.Items.Add("16");
            comboBox5.Items.Add("17");
            comboBox5.Items.Add("18");
            comboBox5.Items.Add("19");
            comboBox5.Items.Add("20");
            comboBox5.Items.Add("21");
            comboBox5.Items.Add("22");
            comboBox5.Items.Add("23");
            comboBox5.Items.Add("24");
            comboBox5.Items.Add("25");
            comboBox5.Items.Add("26");
            comboBox5.Items.Add("27");
            comboBox5.Items.Add("28");
            comboBox5.Items.Add("29");
            comboBox5.Items.Add("30");
        }
        void fullDay()
        {
            comboBox5.Items.Add("1");
            comboBox5.Items.Add("2");
            comboBox5.Items.Add("3");
            comboBox5.Items.Add("4");
            comboBox5.Items.Add("5");
            comboBox5.Items.Add("6");
            comboBox5.Items.Add("7");
            comboBox5.Items.Add("8");
            comboBox5.Items.Add("9");
            comboBox5.Items.Add("10");
            comboBox5.Items.Add("11");
            comboBox5.Items.Add("12");
            comboBox5.Items.Add("13");
            comboBox5.Items.Add("14");
            comboBox5.Items.Add("15");
            comboBox5.Items.Add("16");
            comboBox5.Items.Add("17");
            comboBox5.Items.Add("18");
            comboBox5.Items.Add("19");
            comboBox5.Items.Add("20");
            comboBox5.Items.Add("21");
            comboBox5.Items.Add("22");
            comboBox5.Items.Add("23");
            comboBox5.Items.Add("24");
            comboBox5.Items.Add("25");
            comboBox5.Items.Add("26");
            comboBox5.Items.Add("27");
            comboBox5.Items.Add("28");
            comboBox5.Items.Add("29");
            comboBox5.Items.Add("30");
            comboBox5.Items.Add("31");
        }

        private void comboBox6_Leave(object sender, EventArgs e)
        {
            //string s;
            //s = (comboBox6.SelectedValue).ToString ();
            //MessageBox.Show(s);
         
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string mth;
            double  age;
        year = Convert.ToInt32(comboBox7.Text);
        mth = comboBox6.SelectedValue.ToString();
         
        string a = comboBox7.Text + "." + mth;
        double  op = Convert.ToDouble(a);
        age =2011.7-op ;
     
        textBox1.Text = age .ToString ();
        }
     

    }
}












Thursday, 5 May 2011

How to bind ComboBox in C# Windows Application

 Follows Following Steps to Binding ComboBox in C# :
1.Define namespace for sqlconnection 
<using System.Data.SqlClient;>
2.define Object for Binding Source 
<private Binding Source bindingSource1 = new BindingSource();>
3.Define Adapter Object for Adopt data from Database
 < private SqlDataAdapter dataAdapter = new SqlDataAdapter();>
4.Define Connection String for Connection in forms
 < String connectionString = "Data Source=.\\sqlexpress;Initial Catalog=schoolDB;Integrated Security=True"; >
5.Then Adapter Call and pass command and connection that two argument pass 
<dataAdapter = new SqlDataAdapter(selectCommand, connectionString);>
6.Then Sql commandbuilder call for execuate command in the database
 <  SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);>
7.Then DataTable Call for Store the Value in table 
< DataTable table = new DataTable();>
8.Then this data to localize in local System or Space
< table.Locale = System.Globalization.CultureInfo.InvariantCulture;>
9.Then This Table Data fill by adapter
<dataAdapter.Fill(table);>
10.Then Set BindSource to Table
<bindingSource1.DataSource = table;>
11.Then Set ComboBox DisplayMember and ValueMember Property
<ComboBox1.DisplayMember = "School_name";
                ComboBox1.ValueMember = "id";>
12 Then This code to call specific event use function and set DataSource pass a argument.
< this.Load += new System.EventHandler(button1_Click);
               ComboBox1.DataSource = bindingSource1; 
 GetData("select School_name,id from School_info where id=13");
>
Thus You Successfully Bind the combo Box to Data Base

Example Source code

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;
namespace ComboBox
{
    public partial class buttionEvent : Form
    {
        private BindingSource bindingSource1 = new BindingSource();
        private SqlDataAdapter dataAdapter = new SqlDataAdapter();

        private void GetData(string selectCommand)
        {
            try
            {

                String connectionString = "Data Source=.\\sqlexpress;Initial Catalog=schoolDB;Integrated Security=True";

                dataAdapter = new SqlDataAdapter(selectCommand, connectionString);


                SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);


                DataTable table = new DataTable();

                table.Locale = System.Globalization.CultureInfo.InvariantCulture;

                dataAdapter.Fill(table);

                bindingSource1.DataSource = table;
                ComboBox1.DisplayMember = "School_name";
                ComboBox1.ValueMember = "id";


            }
            catch (SqlException)
            {
                MessageBox.Show("To run this example, replace the value of the " +
                    "connectionString variable with a connection string that is " +
                    "valid for your system.");
            }
        }
        public buttionEvent()
        {
            InitializeComponent();
        
          
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Load += new System.EventHandler(button1_Click);

            this.Text = "DataGridView databinding and updating demo";
            ComboBox1.DataSource = bindingSource1;
            ComboBox1.Visible = true;



            GetData("select School_name,id from School_info where id=13");
        }
    }
}