C# Windows Application

C# Windows Application with SQL Server Database.
I will explain step by step how to create a  C# windows application.
This post will help you to Create, View, Update, Delete operation works.


1. Open Visual Studio .>> File >>  New >> New Project
2. On the left side Under Visual C# category Select Windows.
3. Now You Can see Windows Forms Application.
4. Please Type the appropriate name to your application.
My Application name is windowsCRUDoperations.   
you are not allowed to type any special charter to your program name.











Create the relevant SQL database.

 






My User Interface Like this









my project seeing like this. 











you should create a DAL folder inside the Solution  and create a class file and name it as filmdbcontext
inside the class, you should create a connection string.

 public static string sqlcon = "Data Source=My-PC;Initial Catalog=Contactdetails;Integrated Security=True"; 




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace windowsAppCRUDoperations
{
public partial class Form1 : Form
{

public Form1()
{
InitializeComponent();

}


private void bt_save_Click(object sender, EventArgs e)
{
try
{

SqlConnection con = new SqlConnection();
con.ConnectionString = DAL.filmdbcontext.sqlcon;
con.Open();
string command = @"insert into PersonDetails (Name,Mobile,Location)values('" + tb_name.Text + " ','" + tb_mobile.Text + "' ,'" + tb_location.Text + " ')";
SqlCommand com = new SqlCommand(command, con);
com.ExecuteNonQuery();
con.Close();
cleartextbox();
MessageBox.Show("Successfully Saved ", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
searchdetails();

}
catch(Exception ex)
{
MessageBox.Show(ex + "");
}


}
//details search method
public void searchdetails()
{
DataSet ds = new DataSet();
SqlConnection con = new SqlConnection();

try
{
con.ConnectionString = DAL.filmdbcontext.sqlcon;
con.Open();
SqlDataAdapter da = new SqlDataAdapter("SELECT * from PersonDetails where Name like'%" + tb_name.Text + "%' Order by ID ", con);
da.Fill(ds, "PersonDetails");
dataGridView1.DataSource = ds;
dataGridView1.DataMember = "PersonDetails";
con.Close();
}
catch(Exception ex)
{
MessageBox.Show(ex + "");
}
}

private void Form1_Load(object sender, EventArgs e)
{
searchdetails();
}

private void tb_name_TextChanged(object sender, EventArgs e)
{
searchdetails();
}

private void bt_clear_Click(object sender, EventArgs e)
{
//clear the text boxes
cleartextbox();
}
public void cleartextbox()
{
tb_name.Text = "";
tb_mobile.Text = "";
tb_location.Text = "";
}

private void bt_update_Click(object sender, EventArgs e)
{



try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = DAL.filmdbcontext.sqlcon;

con.Open();

//for (int i = 0; i < dataGridView1.Rows.Count - 1; i++)//loop the GridView Rows
//{

//id = dataGridView1.Rows[i].Cells[0].Value.ToString();
//name = dataGridView1.Rows[i].Cells[1].Value.ToString();
//mobile = dataGridView1.Rows[i].Cells[2].Value.ToString();
//location = dataGridView1.Rows[i].Cells[3].Value.ToString();

string command = @"update PersonDetails set Name='" + tb_name.Text + "',Mobile='" + tb_mobile.Text + "',Location='" + tb_location.Text + "' where ID='" + lb_ID.Text + "'";

SqlCommand com = new SqlCommand(command, con);
com.ExecuteNonQuery();

//}

con.Close();//Data Saved
MessageBox.Show("Successfully Updated ", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
searchdetails();

}
catch(Exception ex)
{
MessageBox.Show(ex + "");
}
}

private void dataGridView1_Click(object sender, EventArgs e)
{
try
{
lb_ID.Text= this.dataGridView1.CurrentRow.Cells[0].Value.ToString();
tb_name.Text = this.dataGridView1.CurrentRow.Cells[1].Value.ToString();
tb_mobile.Text = this.dataGridView1.CurrentRow.Cells[2].Value.ToString();
tb_location.Text = this.dataGridView1.CurrentRow.Cells[3].Value.ToString();

}
catch (Exception ex)
{
MessageBox.Show(ex + "");
}
}

private void tb_location_TextChanged(object sender, EventArgs e)
{
searchdetails();
}

private void bt_delete_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection();
con.ConnectionString = DAL.filmdbcontext.sqlcon;

string command = "DELETE from PersonDetails where ID='" + lb_ID.Text + "' ";
con.Open();
SqlCommand com = new SqlCommand(command, con);


com.ExecuteNonQuery();
con.Close();
MessageBox.Show("Successfully Deleted ", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
cleartextbox();
searchdetails();

}
catch(Exception ex)
{
MessageBox.Show(ex + "");
}
}
}
}




when you insert Data you can see like this







when you want to update, Delete the details you should click on the data then fill the text boxes with relevant data,

you can Download complete Project here but you should create the SQL database
Download the Project
thank you. if you have any concern please leave a comment

Comments