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.
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.
- SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog=Sample;Integrated Security=true;");
- SqlCommand cmd;
- SqlDataAdapter adapt;
- //ID variable used in Updating and Deleting Record
- int ID = 0;
Step 3
Insert data in the database, as sgiven below.
Insert data in the database, as sgiven below.
- if (txt_Name.Text != "" && txt_State.Text != "") {
- cmd = new SqlCommand("insert into tbl_Record(Name,State) values(@name,@state)", con);
- con.Open();
- cmd.Parameters.AddWithValue("@name", txt_Name.Text);
- cmd.Parameters.AddWithValue("@state", txt_State.Text);
- cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("Record Inserted Successfully");
- DisplayData();
- ClearData();
- } else {
- MessageBox.Show("Please Provide Details!");
- }

asha pawarPosted Jul 23, 2018, 6:28 AM
Give mi a code of these operations when id is autoincremented and update delete records by using id