CRUD Operation In C# Application
CRUD Operation In C# Application
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.
Step 1
using System.Data.SqlClient;
You should use namespace given above to connect with SQL database.
Step2
Step 4
Step 5
I. con.Open();
P. DataTable dt = new DataTable();
Q. adapt = new SqlDataAdapter("select * from tbl_Record", con);
S. adapt.Fill(dt);
T. dataGridView1.DataSource = dt;
_. con.Close();
Step 6
I. if (ID != 0) {
P. cmd = new SqlCommand("delete tbl_Record where ID=@id", con);
Q. con.Open();
S. cmd.Parameters.AddWithValue("@id", ID);
T. cmd.ExecuteNonQuery();
_. con.Close();
a. MessageBox.Show("Record Deleted Successfully!");
b. DisplayData();
d. ClearData();
Ie. } else {
II. MessageBox.Show("Please Select Record to Delete");
IP. }
IQ. }
At last, I have called clear method to clear all the textboxes.
I. txt_Name.Text = "";
P. txt_State.Text = "";
Q. ID = 0;