Saturday, 8 August 2015

cruid command through by . windows .net

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Data;


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

        private void button1_Click(object sender, EventArgs e)
        {
        try
{
//create  object  of Connection Class..................
SqlConnection con = new SqlConnection();
//Set Connection String property of Connection object..................
con.ConnectionString = @"Data Source=KING_OF_JAUNPUR\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
//Open Connection..................
con.Open();
//Create object of Command Class................
SqlCommand cmd = new SqlCommand();
//set Connection Property  of  Command object.............
cmd.Connection = con;
//Set Command type of command object
//1.StoredProcedure
//2.TableDirect
//3.Text   (By Default)
cmd.CommandType = CommandType.Text;
//Set Command text Property of command object.........
cmd.CommandText = "insert into Customer(SrNo,FirstName,LastName,Dob) values(" + Convert.ToInt32(textBox1.Text) + ",'" +textBox2.Text+ "','" + textBox3.Text + "','" +Convert.ToDateTime(textBox4.Text).ToShortDateString() + "')";

//Execute command by calling following method................
 //  1.ExecuteNonQuery()
 //      It  query using for  insert,delete,update command...........
 //2.ExecuteScalar()
 //    It  query return a single value and insert all record...................(using select,insert command)
//   3.ExecuteReader()
//      It  query return one or more than one record....................................

cmd.ExecuteNonQuery();


MessageBox.Show("Data Saved");
//TextBoxClear();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}


        }

        private void button2_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "select * from Customer where SrNo='" + Convert.ToInt32(textBox1.Text) + "'";
            cmd.Connection = con;
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

                textBox2.Text = dr["FirstName"].ToString();
                textBox3.Text = dr[2].ToString();
                textBox4.Text = dr.GetDateTime(3).ToShortDateString();
            }
            else
                MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
            con.Close();


        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection();
            con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123";
            con.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "Delete from Customer where SrNo='" + Convert.ToInt32(textBox1.Text) + "'";
            cmd.Connection = con;
            int i = cmd.ExecuteNonQuery();
            if (i > 0)
            {
                MessageBox.Show("Data delete Successfully for SrNo" + textBox1.Text);
              //  TextBoxClear();
            }
            else
            {
                MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            con.Close();
        }

        private void button4_Click(object sender, EventArgs e)
        {
           SqlConnection con = new SqlConnection();
           con.ConnectionString = "Data Source=KING_OF_JAUNPUR\\SQLEXPRESS;Initial Catalog=SqlPractice;User ID=sa;Password=123;";
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "Update Customer set  FirstName='" + textBox2.Text + "',LastName='" + textBox3.Text  + "',Dob='" +Convert.ToDateTime(textBox4.Text ) + "' where SrNo='"+Convert.ToInt32(textBox1.Text )+"'";
cmd.Connection = con;
int i = cmd.ExecuteNonQuery();
if (i > 0)
{
MessageBox.Show("Data updated Successfully for SrNo" +textBox1.Text );
//TextBoxClear();
}
else
{
MessageBox.Show("Record not found", "No record", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
con.Close();
}
        }
    }

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;

            }

        }
    }
}