Search This Blog

Thursday 9 February 2017

insert,update,delete search,display in ADO .net Example !

Check out video :



Check other videos to know from starting how to connect and all other things we have to do in VS !


code :

first of all make table in sql server find in my channel !

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;
namespace Adoinsert
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        private SqlConnection connection;
        private SqlCommand cmd;
        string con = "Data Source=KANU\\SQLEXPRESS;Initial Catalog=Gajjar;Integrated Security=True";

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {
                //open connection and execute query in all the method !!
                connection = new SqlConnection(con);
                connection.Open();
                string sql = "insert into student values('" + textBox1.Text + "','" + textBox2.Text + "')";
                cmd = new SqlCommand(sql, connection);
                cmd.ExecuteNonQuery();
                MessageBox.Show("Record inserted !");
                display();
                //check my simple insert video to get this from begin !!


            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex));
            }
            finally {
                connection.Close();
            }
        }

        private void btndel_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "")
                {
                    MessageBox.Show("plz enter id of name to delete !");
                }
                else
                {

                    connection = new SqlConnection();
                    connection.ConnectionString = con;
                    connection.Open();
                    string sql = "delete from student where id = '"+textBox1.Text+"'";
                    cmd = new SqlCommand(sql, connection);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Record deleted !");
                    display();
                    //this will be same only we have to query according to our requirement !!

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex));
            }
            finally
            {
                connection.Close();
            }
        }

        private void btndisp_Click(object sender, EventArgs e)
        {
            display();
        }

        private void btnsearch_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox2.Text == "") {
                    MessageBox.Show("plz enter name to search");
                }
                else
                {
                    connection = new SqlConnection();
                    connection.ConnectionString = con;
                    connection.Open();
                    string sql = "select * from student where name = '" + textBox2.Text + "' ";
                    SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
                    DataSet ds = new DataSet();
                    adapter.Fill(ds);
                    dataGridView1.DataSource = ds;
                    dataGridView1.DataMember = ds.Tables[0].ToString();
                    //this is how we fill the dataGridView :)
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex));
            }
            finally
            {
                connection.Close();
            }
        }

        private void btnup_Click(object sender, EventArgs e)
        {
            try
            {
                if (textBox1.Text == "" || textBox2.Text=="")
                {
                    MessageBox.Show("plz enter id of old and new name in feilds");
                }
                else
                {
                   
                    connection = new SqlConnection();
                    connection.ConnectionString = con;
                    connection.Open();
                    string sql = "update student set name ='"+textBox2.Text+"' where id = '"+textBox1.Text+"' ";
                    cmd = new SqlCommand(sql, connection);
                    cmd.ExecuteNonQuery();
                    MessageBox.Show("Record updated !");
                    display();

                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex));
            }
            finally
            {
                connection.Close();
            }
        }
        void display() {
            try
            {
                connection = new SqlConnection();
                connection.ConnectionString = con;
                connection.Open();
                string sql = "select * from student";
                SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);
                DataSet ds = new DataSet();
                adapter.Fill(ds);
                dataGridView1.DataSource = ds;
                dataGridView1.DataMember = ds.Tables[0].ToString();
                //this is how we get datasource and we assign it to datagridview!!
            }
            catch (Exception ex)
            {
                MessageBox.Show(Convert.ToString(ex));
            }
            finally
            {
                connection.Close();
            }
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            display();
        }
    }
}



No comments:

Post a Comment