Saturday, 8 August 2015

state city concept

state city concept in windows application


using System;
using System.Data;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;
using System.Configuration;
using System.IO;


namespace WindowsFormsApplication10
{
    public partial class Form1 : Form
    {
   

             public Form1()
        {
            InitializeComponent();
        }
              private void Form1_Load(object sender, EventArgs e)
        {
            bindstate();
        }


        public void bindstate()
              {
                  SqlConnection con = new SqlConnection("Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123");
         
     
             SqlCommand cmd = new SqlCommand("SELECT StateId, StateName FROM State", con);
            con.Open();

            DataSet ds = new DataSet();
            SqlDataAdapter ad = new SqlDataAdapter();
            ad.SelectCommand = cmd;
            ad.Fill(ds);
            con.Close();
            comboBox2.ValueMember = "StateId";
            comboBox2.DisplayMember = "StateName";
            comboBox2.DataSource = ds.Tables[0];



       }
     
        public void bindcity( int stateid)
        {
            SqlConnection con = new SqlConnection("Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123");
            SqlCommand cmd = new SqlCommand("SELECT CityId, CityName FROM City where StateId=@StateId", con);
            cmd.Parameters.AddWithValue("@StateID", stateid);
            con.Open();

            DataSet ds = new DataSet();
            SqlDataAdapter ad = new SqlDataAdapter();
            ad.SelectCommand = cmd;
            ad.Fill(ds);
            con.Close();

            comboBox3.ValueMember = "CityId";
            comboBox3.DisplayMember = "CityName";
            comboBox3.DataSource = ds.Tables[0];



        }


     

        private void button2_Click(object sender, EventArgs e)
        {
            FileStream fs = new FileStream(textBox3.Text, FileMode.Open);
            byte[] bytarr = new byte[fs.Length];

            fs.Read(bytarr, 0, Convert.ToInt16(fs.Length));
            SqlConnection con = new SqlConnection("Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123");
         
     
            SqlCommand cmd = new SqlCommand("insert into registration values(@name,@fathername,@course,@gender,@state,@city,@photo");
            cmd.Connection = con;
           
            con.Open();
            cmd.Parameters.AddWithValue("@name", textBox1.Text);
            cmd.Parameters.AddWithValue("@fathername", textBox2.Text);
            if (radioButton1.Checked == true)
            {

                cmd.Parameters.AddWithValue("@gender", radioButton1.Checked.ToString());


            }
            else{
                cmd.Parameters.AddWithValue("@gender", radioButton2.Checked.ToString());


            }
            cmd.Parameters.AddWithValue("@course", comboBox1.Text);

            cmd.Parameters.AddWithValue("@state",comboBox2.Text);
            cmd.Parameters.AddWithValue("@city", comboBox3.Text);
            cmd.Parameters.AddWithValue("@photo",bytarr);
            cmd.ExecuteNonQuery();
            con.Close();


        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
       

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox2.SelectedIndex.ToString() != "")
            {
                int countryid = Convert.ToInt16(comboBox2.SelectedValue.ToString());
                bindcity(countryid);

            }

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox3.Text = openFileDialog1.FileName;

            }

        }
    }
}

No comments:

Post a Comment