List of Exercises

Insert Delete and Update SQL in | C#

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

namespace Insert_Delete_Record_SQL
{
  public partial class Form1 : Form
  {
    SqlConnection cnn = new SqlConnection(@"Data Source=DESKTOP-J9K51EL\AMSSQL;Initial Catalog=Database1;Integrated Security=True");
    SqlCommand cmd;
    SqlDataAdapter ad;
    //ID variable used in Updating and Deleting Record 
    int ID = 0; 
    public Form1()
    {
      InitializeComponent();
      Display_Data();
    }
    private void Display_Data()
    {
      string query = "select * from Table1";
      System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand(query, cnn);
      cnn.Open();
      // create data adapter
      System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(cmd);
      DataTable dataTable = new DataTable();
      // this will query your database and return the result to your datatable
      da.Fill(dataTable);
      dataGridView1.DataSource= dataTable;
      cnn.Close();
    }

    private void insert_Click(object sender, EventArgs e)
    {
      if (value_Text.Text != "")
      {
        cmd = new SqlCommand("insert into Table1(Id,Value1) values(@id, @val)", cnn);
        cnn.Open();
        cmd.Parameters.AddWithValue("@id", ID+1);
        cmd.Parameters.AddWithValue("@val", value_Text.Text);
        cmd.ExecuteNonQuery();
        cnn.Close();
        MessageBox.Show("Record Inserted Successfully");
        Display_Data();
        Clear_Textboxes();
        ID++;
      }
      else
      {
        MessageBox.Show("Please Provide Details!");
      } 
    }

    private void Clear_Textboxes()
    {
      value_Text.Text = "";
    }

    private void delete_Click(object sender, EventArgs e)
    {
      cmd = new SqlCommand("delete Table1 where Value1=@val", cnn);
      cnn.Open();
      cmd.Parameters.AddWithValue("@val", value_Text.Text);
      cmd.ExecuteNonQuery();
      cnn.Close();
      MessageBox.Show("Record Deleted Successfully!");
      Display_Data();
      Clear_Textboxes();
      ID = ((DataTable)dataGridView1.DataSource).Rows.Count;
    }

    private void update_Click(object sender, EventArgs e)
    {
      if (id_textbox.Text != ""&&value_Text.Text != "")
      {
        cmd = new SqlCommand("update Table1 set Value1=@val where Id=@id", cnn);
        cnn.Open();
        cmd.Parameters.AddWithValue("@id", id_textbox.Text);
        cmd.Parameters.AddWithValue("@val", value_Text.Text);
        cmd.ExecuteNonQuery();
        MessageBox.Show("Record Updated Successfully");
        cnn.Close();
        Display_Data();
        Clear_Textboxes();
      }
      else
      {
        MessageBox.Show("Please Select Record to Update");
      } 
    }
  }
}
		
	 


Download the project of Visual Studio 2013 in DropBox Download


How to Insert Delete and Update Record SQL Server Database in C#


Video Thumbnail