c# insert update delete and view data from database in .net



This is A C# Practice 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;
            using System.Threading;


            namespace dataentry
            {
                public partial class Form1 : Form
                {
                    public Form1(string Status)
                    {
                        InitializeComponent();

                        label22.Text = Status;
                        timer1.Start();
                    }
                  
                    SqlConnection CON = new SqlConnection("Data Source=.;Initial Catalog=DATAENTRY;Integrated Security=True;Pooling=False");
                    private void button4_Click(object sender, EventArgs e)
                    {
                        CON.Open();
                        for (int beepcount = 0; beepcount <=3; beepcount++)
                        {
                            Console.Beep();
                            Thread.Sleep(1000);
                        }
                        SqlDataAdapter SDA = new SqlDataAdapter("SELECT * FROM REGISTER", CON);
                        DataTable DATA = new DataTable();
                        SDA.Fill(DATA);
                        dataGridView1.DataSource = DATA;
                        CON.Close();
                       
                       
                    }

                    private void button1_Click(object sender, EventArgs e)
                    {
                        CON.Open();
                        SqlDataAdapter SDA = new SqlDataAdapter("INSERT INTO REGISTER (ID,NAME,GENDER,STATUS,AGE,SALARY,TAX,DOB) VALUES ('" + textBox1.Text + "','" + textBox2.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','"+this.dateTimePicker1.Text+"')", CON);
                        SDA.SelectCommand.ExecuteNonQuery();
                        CON.Close();
                        MessageBox.Show("SAVED SUCCEESSFULLY !!!!!");
                        button4_Click(sender, e);
                    }

                    private void textBox1_TextChanged(object sender, EventArgs e)
                    {

                    }

                    private void button2_Click(object sender, EventArgs e)
                    {
                        CON.Open();
                        SqlDataAdapter SDA = new SqlDataAdapter("UPDATE REGISTER SET NAME='" + textBox2.Text + "',GENDER='" + comboBox1.Text + "',STATUS='"+comboBox2.Text+"',AGE='" + textBox3.Text + "',SALARY='" + textBox4.Text + "',TAX='" + textBox5.Text + "' WHERE ID='"+textBox1.Text+"'", CON);
                        SDA.SelectCommand.ExecuteNonQuery();
                        CON.Close();
                        MessageBox.Show("UPDATED SUCCEESSFULLY !!!!!");
                    }

                    private void dataGridView1_MouseDoubleClick(object sender, MouseEventArgs e)
                    {
                        textBox1.Text = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
                        textBox2.Text = dataGridView1.SelectedRows[0].Cells[1].Value.ToString();
                        comboBox1.Text = dataGridView1.SelectedRows[0].Cells[2].Value.ToString();
                        comboBox2.Text = dataGridView1.SelectedRows[0].Cells[6].Value.ToString();
                        textBox3.Text = dataGridView1.SelectedRows[0].Cells[3].Value.ToString();
                        textBox4.Text = dataGridView1.SelectedRows[0].Cells[4].Value.ToString();
                        textBox5.Text = dataGridView1.SelectedRows[0].Cells[5].Value.ToString();
                    }

                    private void button3_Click(object sender, EventArgs e)
                    {
                        CON.Open();
                        SqlDataAdapter SDA = new SqlDataAdapter("DELETE FROM REGISTER WHERE ID='" + textBox1.Text + "'", CON);
                        SDA.SelectCommand.ExecuteNonQuery();
                        CON.Close();
                        MessageBox.Show("DELETED SUCCEESSFULLY !!!!!");
                    }

              }
          }
            
Video For Result:



Share on Google Plus

About Unknown

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Facebook Comment