RunCodes
Programming & Tech

How to Insert, Update, Delete and Read in C#.NET using SQL Server Database? [CRUD App in C#] [With Source Code]

0 212

code:

at the top:


using System.Data.SqlClient;

 

inside form:


public Form1()
{
InitializeComponent();
dataGridView1.Visible = false;
}

 

inside clicking event of home button:


foreach (Control control in this.Controls)
{
if (control is DataGridView)
{
control.Visible = false;
}
else
{
control.Visible = true;
}
}

 

inside clicking event of create button:


bool isAnyEmpty = false;
foreach (Control control in this.Controls)
{
if (control is TextBox)
{
if (control.Text.Length == 0)
{
isAnyEmpty = true;
break;
}
}
}
if (isAnyEmpty)
{
MessageBox.Show("please fill the required form", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
else
{
SqlConnection con = new SqlConnection("Data Source=YOUR-CONNECTING-STRING");
con.Open();
string insertQuery = "INSERT INTO crudapp (email, name, username, password) VALUES (@email, @name, @username, @password)";
SqlCommand cmd = new SqlCommand(insertQuery, con);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@username", txtUser.Text);
cmd.Parameters.AddWithValue("@password", txtPass.Text);
int count = cmd.ExecuteNonQuery();
con.Close();
if (count > 0)
{
MessageBox.Show("create successfully", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);

}
else
{
MessageBox.Show("error in creation", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

 

}

inside clicking event of read button:


foreach (Control control in this.Controls)
{
if (control is TextBox)
{
control.Visible = false;
}
else if (control is NumericUpDown)
{
control.Visible = false;
}
else if (control is Label)
{
control.Visible = false;
}
else
{
control.Visible = true;
}

}
SqlConnection con = new SqlConnection("Data Source=YOUR-CONNECTING-STRING);
string readQuery = "SELECT * FROM crudapp";
SqlDataAdapter sda = new SqlDataAdapter(readQuery, con);
SqlCommandBuilder cmd = new SqlCommandBuilder(sda);
DataTable dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;

 

inside clicking event of update:


SqlConnection con = new SqlConnection("YOUR-CONNECTING-STRING");
con.Open();
string updateQuery = "UPDATE crudapp SET email=@email, name=@name, username=@username, password=@password WHERE id=@id";
SqlCommand cmd = new SqlCommand(updateQuery, con);
cmd.Parameters.AddWithValue("@email", txtEmail.Text);
cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@username", txtUser.Text);
cmd.Parameters.AddWithValue("@password", txtPass.Text);
cmd.Parameters.AddWithValue("@id", numericUpDown1.Value);
int count = cmd.ExecuteNonQuery();
con.Close();
if (count > 0)
{
MessageBox.Show("Updated Successfully", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);
}

 

inside clicking event of delete button:


SqlConnection con = new SqlConnection("Data Source=RAN\\SQLEXPRESS;Initial Catalog=crudapp;Integrated Security=True;TrustServerCertificate=True");
con.Open();
string deleteQuery = "DELETE FROM crudapp WHERE id=@id";
SqlCommand cmd = new SqlCommand(deleteQuery, con);
cmd.Parameters.AddWithValue("@id", numericUpDown1.Value);
int count = cmd.ExecuteNonQuery();
if (count > 0)
{
MessageBox.Show("deleted Successfully", "info", MessageBoxButtons.OK, MessageBoxIcon.Information);

}

More:

https://youtu.be/cJvRSlXi8BY

 

Leave a comment