0% found this document useful (0 votes)
9 views4 pages

CRUD Operation In C# Application

This document provides a guide on performing CRUD operations in a C# application using SQL database. It outlines the steps for inserting, updating, displaying, and deleting records, along with necessary code snippets and error handling tips. Basic knowledge of C# and Visual Studio is recommended for successful implementation.

Uploaded by

Mojtaba Sultani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views4 pages

CRUD Operation In C# Application

This document provides a guide on performing CRUD operations in a C# application using SQL database. It outlines the steps for inserting, updating, displaying, and deleting records, along with necessary code snippets and error handling tips. Basic knowledge of C# and Visual Studio is recommended for successful implementation.

Uploaded by

Mojtaba Sultani
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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.

I. SqlConnection con= new SqlConnection("Data Source=.;Initial Catalog


=Sample;Integrated Security=true;");
P. SqlCommand cmd;
Q. SqlDataAdapter adapt;
S.
T. int ID = 0;
Step 3

Insert data in the database, as sgiven below.

I. if (txt_Name.Text != "" && txt_State.Text != "") {


P. cmd = new SqlCommand("insert into tbl_Record(Name,State) values
(@name,@state)", con);
Q. con.Open();
S. cmd.Parameters.AddWithValue("@name", txt_Name.Text);
T. cmd.Parameters.AddWithValue("@state", txt_State.Text);
_. cmd.ExecuteNonQuery();
a. con.Close();
b. MessageBox.Show("Record Inserted Successfully");
d. DisplayData();
Ie. ClearData();
II. } else {
IP. MessageBox.Show("Please Provide Details!");
IQ. }

Step 4

Updating record is given below.

I. if (txt_Name.Text != "" && txt_State.Text != "") {


P. cmd = new SqlCommand("update tbl_Record set Name=@name,Sta
te=@state where ID=@id", con);
Q. con.Open();
S. cmd.Parameters.AddWithValue("@id", ID);
T. cmd.Parameters.AddWithValue("@name", txt_Name.Text);
_. cmd.Parameters.AddWithValue("@state", txt_State.Text);
a. cmd.ExecuteNonQuery();
b. MessageBox.Show("Record Updated Successfully");
d. con.Close();
Ie. DisplayData();
II. ClearData();
IP. } else {
IQ. MessageBox.Show("Please Select Record to Update");
IS. }

Step 5

Display record is shown below.

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

Proceed, as shown below to delete the record.

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;

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy