CRUD Operation and SQL App using C# with Source Code

Language

This is a simple Source Code called CRUD Operation with Search in C# and SQL. This simple prohram will help you to learn how create a C# program that has a Create, Read, Update and Delete features with Search data. The source code itself uses only a single form where the said operation/feature can be done.

Controls & Database

The below list are the controls I have used.

  • Forms
  • TextBox
  • Label
  • Button
  • ComboBox
  • DataGridView

I have created a test database in my SQL Server and created a "Register" Table as the storage of the data. The following list are the columns inside the Register Table.

  • id
  • Name
  • Salery
  • Age
  • Gender
  • Tax

Interface

The image presented below the interface I created for this simple program.

Scripts

The following scripts/code are ones I have used to manage the data.

Database Connection
  1. SqlConnection conn = new SqlConnection(@"Data Source=DESKTOP-2LLV29I\MSSQLSERVER01;Initial Catalog=test1;Integrated Security=True");
Save to Database
  1. private void btnsave_Click(object sender, EventArgs e)
  2. {
  3.  
  4. if (txtid.Text == "" || txtname.Text == "" || txtsalery.Text == "" || txttax.Text == "" || txtage.Text == "" || comboBox1.Text == "")
  5. {
  6. MessageBox.Show("please fill the cell first");
  7. }
  8. else
  9. {
  10. conn.Open();
  11. SqlDataAdapter sda = new SqlDataAdapter("insert into Register(id,name,gender,salery,age,tax)values('" + txtid.Text + "','" + txtname.Text + "','" + comboBox1.Text + "','" + txtsalery.Text + "','" + txtage.Text + "','" + txttax.Text + "')", conn);
  12.  
  13. sda.SelectCommand.ExecuteNonQuery();
  14. conn.Close();
  15. MessageBox.Show("data entered succesfully. . . .");
  16. panel1.Enabled = false;
  17.  
  18. }
  19.  
  20. }
View Data
  1. private void load_data()
  2. {
  3. conn.Open();
  4. SqlDataAdapter sda = new SqlDataAdapter("select * from Register", conn);
  5. DataTable data = new DataTable();
  6. sda.Fill(data);
  7. dataGridView1.DataSource = data;
  8. conn.Close();
  9. }
Update Data
  1. private void btnUpdate_Click(object sender, EventArgs e)
  2. {
  3. if (panel1.Enabled == true)
  4. {
  5.  
  6. if (txtid.Text == "" || txtname.Text == "" || txtsalery.Text == "" || txttax.Text == "" || txtage.Text == "" || comboBox1.Text == "")
  7. {
  8. MessageBox.Show("please fill the cell first");
  9. }
  10. else
  11. {
  12. conn.Open();
  13. SqlDataAdapter sda = new SqlDataAdapter("UPDATE Register SET id ='" + txtid.Text + "',name = '" + txtname.Text + "',Gender = '" + comboBox1.Text + "', Salery = '" + txtsalery.Text + "',age = '" + txtage.Text + "',tax = '" + txttax.Text + "' where id ='" + txtid.Text + "'", conn);
  14.  
  15. sda.SelectCommand.ExecuteNonQuery();
  16. conn.Close();
  17. MessageBox.Show("data updated succesfully. . . .");
  18. load_data();
  19. panel1.Enabled = false;
  20. }
  21.  
  22. }
  23. else
  24. {
  25. MessageBox.Show("please select what you want to update");
  26. }
  27.  
  28. }
Delete Data
  1. private void btndelete_Click(object sender, EventArgs e)
  2. {
  3. if (panel1.Enabled == true)
  4. {
  5.  
  6. if (txtid.Text == "" || txtname.Text == "" || txtsalery.Text == "" || txttax.Text == "" || txtage.Text == "" || comboBox1.Text == "")
  7. {
  8. MessageBox.Show("please select the record");
  9. }
  10. else
  11. {
  12. conn.Open();
  13. SqlDataAdapter sda = new SqlDataAdapter("delete from Register where id ='" + txtid.Text + "'", conn);
  14.  
  15. sda.SelectCommand.ExecuteNonQuery();
  16. conn.Close();
  17. MessageBox.Show("data deleted succesfully. . . .");
  18. load_data();
  19. panel1.Enabled = false;
  20. }
  21.  
  22. }
  23. else
  24. {
  25. MessageBox.Show("please select the record first");
  26. }
  27.  
  28. }

The source code is free to download. Feel Free to download the source code to understand more about how does the program work.

How to Run

Requirements
  • Download and Install Microsoft Visual Studio Software/li>
  • Download and Install Microsoft SQL Server Express
  • Download and Install SQL Server Management Studio
Installation/Setup
  1. Download and Extract the source code zip file. (download button is located the below)
  2. Locate the MDF File in the extracted source code folder. The file is known as "test1.mdf".
  3. Open your SQL Server Management Studio Software and connect to a server.
  4. Attach the MDF file in the database.
  5. Locate the solution file in the extracted source code folder. The file is known as "Data_Entry.sln".
  6. Open the solution file with your MS Visual Studio software.
  7. Configure the SqlConnection according to your database connection string.
  8. Press the "F5" key to start the program.

Demo

That's it! I hope this CRUD (Create, Read, Update, and Delete) with Search Program in C# and SQL will help you to develop your programming skills.

Enjoy :)

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. After downloading it, you will need a program like Winzip to decompress it.

Virus note: All files are scanned once-a-day by SourceCodester.com for viruses, but new viruses come out every day, so no prevention program can catch 100% of them.

FOR YOUR OWN SAFETY, PLEASE:

1. Re-scan downloaded files using your personal virus checker before using it.
2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

Comments

Submitted byAHMADsdddd (not verified)on Thu, 04/20/2017 - 20:31

cannot open database test1 requested by login the login faild

Add new comment