CRUD Operation In C# Application

CRUD operation, using C# is the common program for beginner, intermediate and an expert. During CRUD operation, the programmer is facing different types of errors and it will take lot of time to resolve.

This article shows how to insert, update and delete the records from the database, using C# Server side code. If the progammer has a basic knowledge of C# and Visual Studio, then he will not face any difficulty during the program execution.

Here, I am using SQL database to insert, update and delete operation. Before starting, you should add DLL and afterwards, you should add namespace under it.

Step 1

using System.Data.SqlClient;

You should use namespace given above to connect with SQL database.

Step2

You have to declare connection string outside the class.
  1. SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");  
  2. SqlCommand cmd;  
  3. SqlDataAdapter adapt;  
  4. //ID variable used in Updating and Deleting Record  
  5. int ID = 0; 
Step 3

Insert data in the database, as sgiven below.
  1. if (txt_Name.Text != "" && txt_State.Text != "") {  
  2.     cmd = new SqlCommand("insert into tbl_Record(Name,State) values(@name,@state)", con);  
  3.     con.Open();  
  4.     cmd.Parameters.AddWithValue("@name", txt_Name.Text);  
  5.     cmd.Parameters.AddWithValue("@state", txt_State.Text);  
  6.     cmd.ExecuteNonQuery();  
  7.     con.Close();  
  8.     MessageBox.Show("Record Inserted Successfully");  
  9.     DisplayData();  
  10.     ClearData();  
  11. else {  
  12.     MessageBox.Show("Please Provide Details!");  
  13. }  
Step 4

Updating record is given below.
  1. if (txt_Name.Text != "" && txt_State.Text != "") {  
  2.     cmd = new SqlCommand("update tbl_Record set Name=@name,State=@state where ID=@id", con);  
  3.     con.Open();  
  4.     cmd.Parameters.AddWithValue("@id", ID);  
  5.     cmd.Parameters.AddWithValue("@name", txt_Name.Text);  
  6.     cmd.Parameters.AddWithValue("@state", txt_State.Text);  
  7.     cmd.ExecuteNonQuery();  
  8.     MessageBox.Show("Record Updated Successfully");  
  9.     con.Close();  
  10.     DisplayData();  
  11.     ClearData();  
  12. else {  
  13.     MessageBox.Show("Please Select Record to Update");  
  14. }   
Step 5

Display record is shown below.
  1. con.Open();  
  2. DataTable dt = new DataTable();  
  3. adapt = new SqlDataAdapter("select * from tbl_Record", con);  
  4. adapt.Fill(dt);  
  5. dataGridView1.DataSource = dt;  
  6. con.Close(); 
Step 6

Proceed, as shown below to delete the record. 
  1. if (ID != 0) {  
  2.     cmd = new SqlCommand("delete tbl_Record where ID=@id", con);  
  3.     con.Open();  
  4.     cmd.Parameters.AddWithValue("@id", ID);  
  5.     cmd.ExecuteNonQuery();  
  6.     con.Close();  
  7.     MessageBox.Show("Record Deleted Successfully!");  
  8.     DisplayData();  
  9.     ClearData();  
  10. else {  
  11.     MessageBox.Show("Please Select Record to Delete");  
  12. }  
  13. }  
At last, I have called clear method to clear all the textboxes.
  1. txt_Name.Text = "";  
  2. txt_State.Text = "";  
  3. ID = 0;