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");
        }
    }
}