C# Windows Form Application And Microsoft SQL Database Insert Update Delete Retrieve/Select/Display (CRUD)
Windows Form Application C# And Microsoft SQL Database Insert Update Delete Retrieve/Select/Display (CRUD)
This a complete (crud) Create, read, update and delete, C# Windows Form Application And Microsoft SQL Database Application/program. This desktop app demonstrates how to insert, update/edit, delete and select/retrieve data from Microsoft SQL server database and display retrieved data on c# treeview.

How To Connect To Microsoft SQL Server Database Using Microsoft SQL Server Management Studio
To access Microsoft SQL Server database you need to pass some database properties like database name and server name in sql connection string.
Below is an example of a connection String
string connectionString = "Data Source=DESKTOP-C7B0LHA\\SQLEXPRESS;Initial Catalog=Staff;Integrated Security=True";
You can get the server name when Microsoft SQL Server Management Studio is starting. As shown below.

Database.

TABLE CREATION QUERY
SID Column Auto Increments Whenever a new record is added to the database. It’s also the table primary key, that means only unique data is accepted/allowed.
USE [Staff] GO /****** Object: Table [dbo].[Newstaff] Script Date: 6/3/2021 3:50:18 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO CREATE TABLE [dbo].[Newstaff]( [SID] [int] IDENTITY(1,1) NOT NULL, [Fname] [varchar](150) NOT NULL, [lname] [varchar](150) NOT NULL, [gender] [varchar](20) NOT NULL, [dob] [varchar](100) NOT NULL, [mobile] [bigint] NOT NULL, [email] [varchar](150) NULL, [joindate] [varchar](100) NOT NULL, [city] [varchar](100) NOT NULL, [basicSalary] [decimal](8, 2) NULL, [post] [varchar](max) NULL, CONSTRAINT [PK_Newstaff] PRIMARY KEY CLUSTERED ( [SID] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY] GO
SOURCE CODE
DatabaseConnection.cs
Database connection class has getConn method that uses provided connection string to connect to microsoft server database and return connection.
using System; using System.Collections.Generic; using System.Data.SqlClient; using System.Linq; using System.Text; using System.Threading.Tasks; namespace InsertUpdateDeleteDisplayStaffWindowsFormsApplication { class DatabaseConnection { //Method For Connecting To Microsoft SQL Server Database public SqlConnection getConn() { //Connection String. string connectionString = "Data Source=DESKTOP-C7B0LHA\\SQLEXPRESS;Initial Catalog=Staff;Integrated Security=True"; SqlConnection connection = new SqlConnection(connectionString); //Return Connection. return connection; } } }

Form1.cs
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Data.SqlClient; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace InsertUpdateDeleteDisplayStaffWindowsFormsApplication { public partial class Form1 : Form { //Creating DatabaseConnection Object DatabaseConnection databaseConnection = new DatabaseConnection(); public Form1() { InitializeComponent(); //populating treeView From SQL Server populatetreeViewFromSQLServer(); } private void Form1_Load(object sender, EventArgs e) { } //Search button click event. private void button1_Click(object sender, EventArgs e) { SqlConnection connection = databaseConnection.getConn(); SqlCommand command = null; string sql = "SELECT * FROM Newstaff WHERE SID = " + textBoxSearch.Text; SqlDataReader dataReader = null; try { connection.Open(); command = new SqlCommand(sql, connection); dataReader = command.ExecuteReader(); while (dataReader.Read()) { textBoxSearch.Text = dataReader.GetInt32(0).ToString(); textBoxFname.Text = (string)dataReader["Fname"]; textBoxLName.Text = (string)dataReader["lname"]; textBoxMobile.Text = dataReader.GetInt64(5).ToString(); if ((string)dataReader["gender"] == "Male") { radioButtonMale.Checked = true; } else if ((string)dataReader["gender"] == "Female") { radioButtonFemale.Checked = true; } dateTimePickerDateOfBirth.Text = (string)dataReader["dob"]; dateTimePickerDateJoined.Text = (string)dataReader["joindate"]; textBoxMail.Text = (string)dataReader["email"]; textBoxCity.Text = (string)dataReader["city"]; textBoxSalary.Text = dataReader.GetDecimal(9).ToString(); textBoxPost.Text = (string)dataReader["post"]; } } catch (Exception ex) { MessageBox.Show("Can not open connection ! " + ex.Message); } finally { dataReader.Close(); command.Dispose(); if (connection.State == ConnectionState.Open) { //MessageBox.Show("Closing Connection = After Select "); connection.Close(); } } } private void treeView1_MouseClick(object sender, MouseEventArgs e) { } private void treeView1_AfterSelect(object sender, TreeViewEventArgs e) { SqlConnection connection = databaseConnection.getConn(); SqlCommand command = null; string sql = "SELECT * FROM Newstaff WHERE SID = " + treeView1.SelectedNode.Text; SqlDataReader dataReader = null; try { connection.Open(); command = new SqlCommand(sql, connection); dataReader = command.ExecuteReader(); while (dataReader.Read()) { textBoxSearch.Text = dataReader.GetInt32(0).ToString(); textBoxFname.Text = (string)dataReader["Fname"]; textBoxLName.Text = (string)dataReader["lname"]; textBoxMobile.Text = dataReader["mobile"].ToString(); if ((string)dataReader["gender"] == "Male") { radioButtonMale.Checked = true; }else if ((string)dataReader["gender"] == "Female") { radioButtonFemale.Checked = true; } dateTimePickerDateOfBirth.Text = (string)dataReader["dob"]; dateTimePickerDateJoined.Text = (string)dataReader["joindate"]; textBoxMail.Text = (string)dataReader["email"]; textBoxCity.Text = (string)dataReader["city"]; textBoxSalary.Text = dataReader["basicSalary"].ToString(); textBoxPost.Text = (string)dataReader["post"]; } } catch (Exception ex) { MessageBox.Show("Can not open connection ! " + ex.Message); } finally { dataReader.Close(); command.Dispose(); if (connection.State == ConnectionState.Open) { //MessageBox.Show("Closing Connection = After Select "); connection.Close(); } } } private void populatetreeViewFromSQLServer() { SqlConnection connection = databaseConnection.getConn(); SqlCommand command = null; string sql = "SELECT * FROM Newstaff"; SqlDataReader dataReader = null; try { connection.Open(); command = new SqlCommand(sql, connection); dataReader = command.ExecuteReader(); while (dataReader.Read()) { treeView1.BeginUpdate(); TreeNode node = new TreeNode(dataReader.GetInt32(0).ToString()); node.Nodes.Add((string)dataReader["Fname"]); node.Nodes.Add((string)dataReader["lname"]); node.Nodes.Add((string)dataReader["gender"]); node.Nodes.Add((string)dataReader["dob"]); node.Nodes.Add(dataReader.GetInt64(5).ToString()); node.Nodes.Add((string)dataReader["email"]); node.Nodes.Add((string)dataReader["joindate"]); node.Nodes.Add((string)dataReader["city"]); node.Nodes.Add(dataReader.GetDecimal(9).ToString()); treeView1.Nodes.Add(node); treeView1.EndUpdate(); } } catch (Exception ex) { MessageBox.Show("Can not open connection ! " + ex.Message); } finally { dataReader.Close(); command.Dispose(); if (connection.State == ConnectionState.Open ) { //MessageBox.Show("Closing Connection"); connection.Close(); } } } ////Update button click event. private void button2_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(textBoxSearch.Text)) { MessageBox.Show("text Box Search Is Empty"); } else { SqlConnection connection = databaseConnection.getConn(); SqlCommand command = null; string sql = "UPDATE Newstaff SET Fname = @Fname, lname = @lname, gender = @gender, dob = @dob, mobile = @mobile, email = @email, joindate = @joindate, city = @city, basicSalary = @basicSalary, post = @post WHERE SID = " + textBoxSearch.Text; try { string gend = ""; if (radioButtonMale.Checked == true) { gend = "Male"; } else if (radioButtonFemale.Checked == true) { gend = "Female"; } connection.Open(); command = new SqlCommand(sql, connection); command.Parameters.Add("@Fname", SqlDbType.NVarChar).Value = textBoxFname.Text; command.Parameters.Add("@lname", SqlDbType.NVarChar).Value = textBoxLName.Text; command.Parameters.Add("@gender", SqlDbType.NVarChar).Value = gend; command.Parameters.Add("@dob", SqlDbType.NVarChar).Value = dateTimePickerDateOfBirth.Text; command.Parameters.Add("@mobile", SqlDbType.NVarChar).Value = textBoxMobile.Text; command.Parameters.Add("@email", SqlDbType.NVarChar).Value = textBoxMail.Text; command.Parameters.Add("@joindate", SqlDbType.NVarChar).Value = dateTimePickerDateJoined.Text; command.Parameters.Add("@city", SqlDbType.NVarChar).Value = textBoxCity.Text; command.Parameters.Add("@basicSalary", SqlDbType.NVarChar).Value = textBoxSalary.Text; command.Parameters.Add("@post", SqlDbType.NVarChar).Value = textBoxPost.Text; if (command.ExecuteNonQuery() > 0) { MessageBox.Show("Updated Data From Microsoft SQL Server Management Studio Successfully"); } } catch (Exception ex) { MessageBox.Show("Error ! " + ex.Message); } finally { populatetreeViewFromSQLServer(); command.Dispose(); connection.Close(); } } } private void clearAllFields() { textBoxSearch.Text = ""; textBoxFname.Text = ""; textBoxLName.Text = ""; textBoxFname.Text = ""; textBoxMobile.Text = ""; if (radioButtonMale.Checked) { radioButtonMale.Checked = false; } else if (radioButtonFemale.Checked) { radioButtonFemale.Checked = false; } dateTimePickerDateOfBirth.Text = ""; dateTimePickerDateJoined.Text = ""; textBoxMail.Text = ""; textBoxCity.Text = ""; textBoxSalary.Text = ""; textBoxPost.Text = ""; } //Clear button click event. private void button1_Click_1(object sender, EventArgs e) { clearAllFields(); } //Delete button click event. private void buttonDelete_Click(object sender, EventArgs e) { if (MessageBox.Show("Do You Want To Remove This Data", "Remove Data", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes) { if (String.IsNullOrEmpty(textBoxSearch.Text)) { MessageBox.Show("Text Box Search Is Empty"); } else { SqlConnection connection = databaseConnection.getConn(); SqlCommand command = null; string sql = "DELETE FROM Newstaff WHERE SID = " + textBoxSearch.Text; try { connection.Open(); command = new SqlCommand(sql, connection); if (command.ExecuteNonQuery() > 0) { MessageBox.Show("DELETED"); } } catch (Exception ex) { MessageBox.Show("Error ! " + ex.Message); } finally { //Clear All Fields. clearAllFields(); command.Dispose(); connection.Close(); } } } else { MessageBox.Show("Data Not Removed", "Remove Data", MessageBoxButtons.OK, MessageBoxIcon.Information); } } //Insert button click event. private void buttonInsertIntoMSSQLDb_Click(object sender, EventArgs e) { if (String.IsNullOrEmpty(textBoxFname.Text)) { MessageBox.Show("There is one or more empty input field"); } else { SqlConnection connection = databaseConnection.getConn(); SqlCommand command = null; string sql = "INSERT INTO newstaff(Fname, lname, gender, dob, mobile, email, joindate, city, basicSalary, post) VALUES (@Fname, @lname, @gender, @dob, @mobile, @email, @joindate, @city, @basicSalary, @post )"; try { string gend = ""; if (radioButtonMale.Checked == true) { gend = "Male"; } else if (radioButtonFemale.Checked == true) { gend = "Female"; } connection.Open(); command = new SqlCommand(sql, connection); command.Parameters.Add("@Fname", SqlDbType.NVarChar).Value = textBoxFname.Text; command.Parameters.Add("@lname", SqlDbType.NVarChar).Value = textBoxLName.Text; command.Parameters.Add("@gender", SqlDbType.NVarChar).Value = gend; command.Parameters.Add("@dob", SqlDbType.NVarChar).Value = dateTimePickerDateOfBirth.Text; command.Parameters.Add("@mobile", SqlDbType.NVarChar).Value = textBoxMobile.Text; command.Parameters.Add("@email", SqlDbType.NVarChar).Value = textBoxMail.Text; command.Parameters.Add("@joindate", SqlDbType.NVarChar).Value = dateTimePickerDateJoined.Text; command.Parameters.Add("@city", SqlDbType.NVarChar).Value = textBoxCity.Text; command.Parameters.Add("@basicSalary", SqlDbType.NVarChar).Value = textBoxSalary.Text; command.Parameters.Add("@post", SqlDbType.NVarChar).Value = textBoxPost.Text; if (command.ExecuteNonQuery() > 0) { MessageBox.Show("Data Inserted Into Microsoft SQL Server Database Successfully"); } } catch (Exception ex) { MessageBox.Show("Error ! " + ex.Message); } finally { command.Dispose(); connection.Close(); } } } } }
Program.cs
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using System.Windows.Forms; namespace InsertUpdateDeleteDisplayStaffWindowsFormsApplication { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); Application.Run(new Form1()); } } }
Form1.Designer.cs
namespace InsertUpdateDeleteDisplayStaffWindowsFormsApplication { partial class Form1 { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.buttonSearch = new System.Windows.Forms.Button(); this.treeView1 = new System.Windows.Forms.TreeView(); this.label1 = new System.Windows.Forms.Label(); this.label2 = new System.Windows.Forms.Label(); this.textBoxFname = new System.Windows.Forms.TextBox(); this.textBoxLName = new System.Windows.Forms.TextBox(); this.label3 = new System.Windows.Forms.Label(); this.textBoxMobile = new System.Windows.Forms.TextBox(); this.label4 = new System.Windows.Forms.Label(); this.textBoxMail = new System.Windows.Forms.TextBox(); this.label5 = new System.Windows.Forms.Label(); this.textBoxSearch = new System.Windows.Forms.TextBox(); this.textBoxCity = new System.Windows.Forms.TextBox(); this.label7 = new System.Windows.Forms.Label(); this.textBoxPost = new System.Windows.Forms.TextBox(); this.label8 = new System.Windows.Forms.Label(); this.textBoxSalary = new System.Windows.Forms.TextBox(); this.label9 = new System.Windows.Forms.Label(); this.radioButtonMale = new System.Windows.Forms.RadioButton(); this.radioButtonFemale = new System.Windows.Forms.RadioButton(); this.label10 = new System.Windows.Forms.Label(); this.dateTimePickerDateOfBirth = new System.Windows.Forms.DateTimePicker(); this.label11 = new System.Windows.Forms.Label(); this.label12 = new System.Windows.Forms.Label(); this.dateTimePickerDateJoined = new System.Windows.Forms.DateTimePicker(); this.buttonUpdate = new System.Windows.Forms.Button(); this.buttonDelete = new System.Windows.Forms.Button(); this.button1 = new System.Windows.Forms.Button(); this.buttonInsertIntoMSSQLDb = new System.Windows.Forms.Button(); this.SuspendLayout(); // // buttonSearch // this.buttonSearch.Location = new System.Drawing.Point(13, 577); this.buttonSearch.Name = "buttonSearch"; this.buttonSearch.Size = new System.Drawing.Size(134, 53); this.buttonSearch.TabIndex = 0; this.buttonSearch.Text = "Search"; this.buttonSearch.UseVisualStyleBackColor = true; this.buttonSearch.Click += new System.EventHandler(this.button1_Click); // // treeView1 // this.treeView1.Location = new System.Drawing.Point(13, 13); this.treeView1.Name = "treeView1"; this.treeView1.Size = new System.Drawing.Size(362, 372); this.treeView1.TabIndex = 1; this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect); this.treeView1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.treeView1_MouseClick); // // label1 // this.label1.AutoSize = true; this.label1.Location = new System.Drawing.Point(13, 421); this.label1.Name = "label1"; this.label1.Size = new System.Drawing.Size(108, 13); this.label1.TabIndex = 2; this.label1.Text = "Enter SID For Search"; // // label2 // this.label2.AutoSize = true; this.label2.Location = new System.Drawing.Point(398, 16); this.label2.Name = "label2"; this.label2.Size = new System.Drawing.Size(41, 13); this.label2.TabIndex = 3; this.label2.Text = "FName"; // // textBoxFname // this.textBoxFname.Location = new System.Drawing.Point(466, 16); this.textBoxFname.Name = "textBoxFname"; this.textBoxFname.Size = new System.Drawing.Size(177, 20); this.textBoxFname.TabIndex = 4; // // textBoxLName // this.textBoxLName.Location = new System.Drawing.Point(466, 66); this.textBoxLName.Name = "textBoxLName"; this.textBoxLName.Size = new System.Drawing.Size(177, 20); this.textBoxLName.TabIndex = 6; // // label3 // this.label3.AutoSize = true; this.label3.Location = new System.Drawing.Point(398, 66); this.label3.Name = "label3"; this.label3.Size = new System.Drawing.Size(41, 13); this.label3.TabIndex = 5; this.label3.Text = "LName"; // // textBoxMobile // this.textBoxMobile.Location = new System.Drawing.Point(466, 107); this.textBoxMobile.Name = "textBoxMobile"; this.textBoxMobile.Size = new System.Drawing.Size(177, 20); this.textBoxMobile.TabIndex = 8; // // label4 // this.label4.AutoSize = true; this.label4.Location = new System.Drawing.Point(398, 107); this.label4.Name = "label4"; this.label4.Size = new System.Drawing.Size(38, 13); this.label4.TabIndex = 7; this.label4.Text = "Mobile"; // // textBoxMail // this.textBoxMail.Location = new System.Drawing.Point(466, 154); this.textBoxMail.Name = "textBoxMail"; this.textBoxMail.Size = new System.Drawing.Size(177, 20); this.textBoxMail.TabIndex = 10; // // label5 // this.label5.AutoSize = true; this.label5.Location = new System.Drawing.Point(398, 154); this.label5.Name = "label5"; this.label5.Size = new System.Drawing.Size(26, 13); this.label5.TabIndex = 9; this.label5.Text = "Mail"; // // textBoxSearch // this.textBoxSearch.Location = new System.Drawing.Point(148, 418); this.textBoxSearch.Name = "textBoxSearch"; this.textBoxSearch.Size = new System.Drawing.Size(216, 20); this.textBoxSearch.TabIndex = 11; // // textBoxCity // this.textBoxCity.Location = new System.Drawing.Point(845, 21); this.textBoxCity.Name = "textBoxCity"; this.textBoxCity.Size = new System.Drawing.Size(177, 20); this.textBoxCity.TabIndex = 15; // // label7 // this.label7.AutoSize = true; this.label7.Location = new System.Drawing.Point(777, 21); this.label7.Name = "label7"; this.label7.Size = new System.Drawing.Size(24, 13); this.label7.TabIndex = 14; this.label7.Text = "City"; // // textBoxPost // this.textBoxPost.Location = new System.Drawing.Point(845, 244); this.textBoxPost.Name = "textBoxPost"; this.textBoxPost.Size = new System.Drawing.Size(177, 20); this.textBoxPost.TabIndex = 19; // // label8 // this.label8.AutoSize = true; this.label8.Location = new System.Drawing.Point(777, 244); this.label8.Name = "label8"; this.label8.Size = new System.Drawing.Size(28, 13); this.label8.TabIndex = 18; this.label8.Text = "Post"; // // textBoxSalary // this.textBoxSalary.Location = new System.Drawing.Point(845, 194); this.textBoxSalary.Name = "textBoxSalary"; this.textBoxSalary.Size = new System.Drawing.Size(177, 20); this.textBoxSalary.TabIndex = 17; // // label9 // this.label9.AutoSize = true; this.label9.Location = new System.Drawing.Point(777, 194); this.label9.Name = "label9"; this.label9.Size = new System.Drawing.Size(36, 13); this.label9.TabIndex = 16; this.label9.Text = "Salary"; // // radioButtonMale // this.radioButtonMale.AutoSize = true; this.radioButtonMale.Location = new System.Drawing.Point(845, 109); this.radioButtonMale.Name = "radioButtonMale"; this.radioButtonMale.Size = new System.Drawing.Size(48, 17); this.radioButtonMale.TabIndex = 20; this.radioButtonMale.TabStop = true; this.radioButtonMale.Text = "Male"; this.radioButtonMale.UseVisualStyleBackColor = true; // // radioButtonFemale // this.radioButtonFemale.AutoSize = true; this.radioButtonFemale.Location = new System.Drawing.Point(949, 111); this.radioButtonFemale.Name = "radioButtonFemale"; this.radioButtonFemale.Size = new System.Drawing.Size(59, 17); this.radioButtonFemale.TabIndex = 21; this.radioButtonFemale.TabStop = true; this.radioButtonFemale.Text = "Female"; this.radioButtonFemale.UseVisualStyleBackColor = true; // // label10 // this.label10.AutoSize = true; this.label10.Location = new System.Drawing.Point(777, 113); this.label10.Name = "label10"; this.label10.Size = new System.Drawing.Size(42, 13); this.label10.TabIndex = 22; this.label10.Text = "Gender"; // // dateTimePickerDateOfBirth // this.dateTimePickerDateOfBirth.CustomFormat = "yyyy-MM-dd"; this.dateTimePickerDateOfBirth.Format = System.Windows.Forms.DateTimePickerFormat.Custom; this.dateTimePickerDateOfBirth.Location = new System.Drawing.Point(845, 293); this.dateTimePickerDateOfBirth.Name = "dateTimePickerDateOfBirth"; this.dateTimePickerDateOfBirth.Size = new System.Drawing.Size(200, 20); this.dateTimePickerDateOfBirth.TabIndex = 23; // // label11 // this.label11.AutoSize = true; this.label11.Location = new System.Drawing.Point(777, 299); this.label11.Name = "label11"; this.label11.Size = new System.Drawing.Size(30, 13); this.label11.TabIndex = 24; this.label11.Text = "DOB"; // // label12 // this.label12.AutoSize = true; this.label12.Location = new System.Drawing.Point(741, 357); this.label12.Name = "label12"; this.label12.Size = new System.Drawing.Size(64, 13); this.label12.TabIndex = 25; this.label12.Text = "Date Joined"; // // dateTimePickerDateJoined // this.dateTimePickerDateJoined.CustomFormat = "yyyy-MM-dd"; this.dateTimePickerDateJoined.Format = System.Windows.Forms.DateTimePickerFormat.Custom; this.dateTimePickerDateJoined.Location = new System.Drawing.Point(845, 351); this.dateTimePickerDateJoined.Name = "dateTimePickerDateJoined"; this.dateTimePickerDateJoined.Size = new System.Drawing.Size(200, 20); this.dateTimePickerDateJoined.TabIndex = 26; // // buttonUpdate // this.buttonUpdate.Location = new System.Drawing.Point(186, 577); this.buttonUpdate.Name = "buttonUpdate"; this.buttonUpdate.Size = new System.Drawing.Size(134, 53); this.buttonUpdate.TabIndex = 27; this.buttonUpdate.Text = "Update"; this.buttonUpdate.UseVisualStyleBackColor = true; this.buttonUpdate.Click += new System.EventHandler(this.button2_Click); // // buttonDelete // this.buttonDelete.Location = new System.Drawing.Point(348, 577); this.buttonDelete.Name = "buttonDelete"; this.buttonDelete.Size = new System.Drawing.Size(134, 53); this.buttonDelete.TabIndex = 28; this.buttonDelete.Text = "Delete"; this.buttonDelete.UseVisualStyleBackColor = true; this.buttonDelete.Click += new System.EventHandler(this.buttonDelete_Click); // // button1 // this.button1.Location = new System.Drawing.Point(537, 577); this.button1.Name = "button1"; this.button1.Size = new System.Drawing.Size(134, 53); this.button1.TabIndex = 29; this.button1.Text = "CLEAR"; this.button1.UseVisualStyleBackColor = true; this.button1.Click += new System.EventHandler(this.button1_Click_1); // // buttonInsertIntoMSSQLDb // this.buttonInsertIntoMSSQLDb.Location = new System.Drawing.Point(703, 577); this.buttonInsertIntoMSSQLDb.Name = "buttonInsertIntoMSSQLDb"; this.buttonInsertIntoMSSQLDb.Size = new System.Drawing.Size(134, 53); this.buttonInsertIntoMSSQLDb.TabIndex = 30; this.buttonInsertIntoMSSQLDb.Text = "INSERT"; this.buttonInsertIntoMSSQLDb.UseVisualStyleBackColor = true; this.buttonInsertIntoMSSQLDb.Click += new System.EventHandler(this.buttonInsertIntoMSSQLDb_Click); // // Form1 // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.ClientSize = new System.Drawing.Size(1281, 669); this.Controls.Add(this.buttonInsertIntoMSSQLDb); this.Controls.Add(this.button1); this.Controls.Add(this.buttonDelete); this.Controls.Add(this.buttonUpdate); this.Controls.Add(this.dateTimePickerDateJoined); this.Controls.Add(this.label12); this.Controls.Add(this.label11); this.Controls.Add(this.dateTimePickerDateOfBirth); this.Controls.Add(this.label10); this.Controls.Add(this.radioButtonFemale); this.Controls.Add(this.radioButtonMale); this.Controls.Add(this.textBoxPost); this.Controls.Add(this.label8); this.Controls.Add(this.textBoxSalary); this.Controls.Add(this.label9); this.Controls.Add(this.textBoxCity); this.Controls.Add(this.label7); this.Controls.Add(this.textBoxSearch); this.Controls.Add(this.textBoxMail); this.Controls.Add(this.label5); this.Controls.Add(this.textBoxMobile); this.Controls.Add(this.label4); this.Controls.Add(this.textBoxLName); this.Controls.Add(this.label3); this.Controls.Add(this.textBoxFname); this.Controls.Add(this.label2); this.Controls.Add(this.label1); this.Controls.Add(this.treeView1); this.Controls.Add(this.buttonSearch); this.Name = "Form1"; this.Text = "Form1"; this.Load += new System.EventHandler(this.Form1_Load); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.Button buttonSearch; private System.Windows.Forms.TreeView treeView1; private System.Windows.Forms.Label label1; private System.Windows.Forms.Label label2; private System.Windows.Forms.TextBox textBoxFname; private System.Windows.Forms.TextBox textBoxLName; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox textBoxMobile; private System.Windows.Forms.Label label4; private System.Windows.Forms.TextBox textBoxMail; private System.Windows.Forms.Label label5; private System.Windows.Forms.TextBox textBoxSearch; private System.Windows.Forms.TextBox textBoxCity; private System.Windows.Forms.Label label7; private System.Windows.Forms.TextBox textBoxPost; private System.Windows.Forms.Label label8; private System.Windows.Forms.TextBox textBoxSalary; private System.Windows.Forms.Label label9; private System.Windows.Forms.RadioButton radioButtonMale; private System.Windows.Forms.RadioButton radioButtonFemale; private System.Windows.Forms.Label label10; private System.Windows.Forms.DateTimePicker dateTimePickerDateOfBirth; private System.Windows.Forms.Label label11; private System.Windows.Forms.Label label12; private System.Windows.Forms.DateTimePicker dateTimePickerDateJoined; private System.Windows.Forms.Button buttonUpdate; private System.Windows.Forms.Button buttonDelete; private System.Windows.Forms.Button button1; private System.Windows.Forms.Button buttonInsertIntoMSSQLDb; } }
OTHER CODE SNIPPETS
InsertUpdateDeleteDisplayStaffWindowsFormsApplication.sln
Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 VisualStudioVersion = 14.0.24720.0 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "InsertUpdateDeleteDisplayStaffWindowsFormsApplication", "InsertUpdateDeleteDisplayStaffWindowsFormsApplication\InsertUpdateDeleteDisplayStaffWindowsFormsApplication.csproj", "{F48AD5DD-AB62-424F-A1AE-071E56DCE231}" EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU Release|Any CPU = Release|Any CPU EndGlobalSection GlobalSection(ProjectConfigurationPlatforms) = postSolution {F48AD5DD-AB62-424F-A1AE-071E56DCE231}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {F48AD5DD-AB62-424F-A1AE-071E56DCE231}.Debug|Any CPU.Build.0 = Debug|Any CPU {F48AD5DD-AB62-424F-A1AE-071E56DCE231}.Release|Any CPU.ActiveCfg = Release|Any CPU {F48AD5DD-AB62-424F-A1AE-071E56DCE231}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE EndGlobalSection EndGlobal
Form1.resx
<?xml version="1.0" encoding="utf-8"?> <root> <!-- Microsoft ResX Schema Version 2.0 The primary goals of this format is to allow a simple XML format that is mostly human readable. The generation and parsing of the various data types are done through the TypeConverter classes associated with the data types. Example: ... ado.net/XML headers & schema ... <resheader name="resmimetype">text/microsoft-resx</resheader> <resheader name="version">2.0</resheader> <resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader> <resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader> <data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data> <data name="Color1" type="System.Drawing.Color, System.Drawing">Blue</data> <data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64"> <value>[base64 mime encoded serialized .NET Framework object]</value> </data> <data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64"> <value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value> <comment>This is a comment</comment> </data> There are any number of "resheader" rows that contain simple name/value pairs. Each data row contains a name, and value. The row also contains a type or mimetype. Type corresponds to a .NET class that support text/value conversion through the TypeConverter architecture. Classes that don't support this are serialized and stored with the mimetype set. The mimetype is used for serialized objects, and tells the ResXResourceReader how to depersist the object. This is currently not extensible. For a given mimetype the value must be set accordingly: Note - application/x-microsoft.net.object.binary.base64 is the format that the ResXResourceWriter will generate, however the reader can read any of the formats listed below. mimetype: application/x-microsoft.net.object.binary.base64 value : The object must be serialized with : System.Runtime.Serialization.Formatters.Binary.BinaryFormatter : and then encoded with base64 encoding. mimetype: application/x-microsoft.net.object.soap.base64 value : The object must be serialized with : System.Runtime.Serialization.Formatters.Soap.SoapFormatter : and then encoded with base64 encoding. mimetype: application/x-microsoft.net.object.bytearray.base64 value : The object must be serialized into a byte array : using a System.ComponentModel.TypeConverter : and then encoded with base64 encoding. --> <xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata"> <xsd:import namespace="http://www.w3.org/XML/1998/namespace" /> <xsd:element name="root" msdata:IsDataSet="true"> <xsd:complexType> <xsd:choice maxOccurs="unbounded"> <xsd:element name="metadata"> <xsd:complexType> <xsd:sequence> <xsd:element name="value" type="xsd:string" minOccurs="0" /> </xsd:sequence> <xsd:attribute name="name" use="required" type="xsd:string" /> <xsd:attribute name="type" type="xsd:string" /> <xsd:attribute name="mimetype" type="xsd:string" /> <xsd:attribute ref="xml:space" /> </xsd:complexType> </xsd:element> <xsd:element name="assembly"> <xsd:complexType> <xsd:attribute name="alias" type="xsd:string" /> <xsd:attribute name="name" type="xsd:string" /> </xsd:complexType> </xsd:element> <xsd:element name="data"> <xsd:complexType> <xsd:sequence> <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> <xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" /> </xsd:sequence> <xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" /> <xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" /> <xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" /> <xsd:attribute ref="xml:space" /> </xsd:complexType> </xsd:element> <xsd:element name="resheader"> <xsd:complexType> <xsd:sequence> <xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" /> </xsd:sequence> <xsd:attribute name="name" type="xsd:string" use="required" /> </xsd:complexType> </xsd:element> </xsd:choice> </xsd:complexType> </xsd:element> </xsd:schema> <resheader name="resmimetype"> <value>text/microsoft-resx</value> </resheader> <resheader name="version"> <value>2.0</value> </resheader> <resheader name="reader"> <value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> <resheader name="writer"> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> </resheader> </root>
SELECTING DATA FROM SQL SERVER QUERY(100 ROWS)
/****** Script for SelectTopNRows command from SSMS ******/ SELECT TOP (1000) [SID] ,[Fname] ,[lname] ,[gender] ,[dob] ,[mobile] ,[email] ,[joindate] ,[city] ,[basicSalary] ,[post] FROM [Staff].[dbo].[Newstaff]
InsertUpdateDeleteDisplayStaffWindowsFormsApplication.csproj
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" /> <PropertyGroup> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <ProjectGuid>{F48AD5DD-AB62-424F-A1AE-071E56DCE231}</ProjectGuid> <OutputType>WinExe</OutputType> <AppDesignerFolder>Properties</AppDesignerFolder> <RootNamespace>InsertUpdateDeleteDisplayStaffWindowsFormsApplication</RootNamespace> <AssemblyName>InsertUpdateDeleteDisplayStaffWindowsFormsApplication</AssemblyName> <TargetFrameworkVersion>v4.5.2</TargetFrameworkVersion> <FileAlignment>512</FileAlignment> <AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> <DebugSymbols>true</DebugSymbols> <DebugType>full</DebugType> <Optimize>false</Optimize> <OutputPath>bin\Debug\</OutputPath> <DefineConstants>DEBUG;TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PlatformTarget>AnyCPU</PlatformTarget> <DebugType>pdbonly</DebugType> <Optimize>true</Optimize> <OutputPath>bin\Release\</OutputPath> <DefineConstants>TRACE</DefineConstants> <ErrorReport>prompt</ErrorReport> <WarningLevel>4</WarningLevel> </PropertyGroup> <ItemGroup> <Reference Include="System" /> <Reference Include="System.Core" /> <Reference Include="System.Xml.Linq" /> <Reference Include="System.Data.DataSetExtensions" /> <Reference Include="Microsoft.CSharp" /> <Reference Include="System.Data" /> <Reference Include="System.Deployment" /> <Reference Include="System.Drawing" /> <Reference Include="System.Net.Http" /> <Reference Include="System.Windows.Forms" /> <Reference Include="System.Xml" /> </ItemGroup> <ItemGroup> <Compile Include="DatabaseConnection.cs" /> <Compile Include="Form1.cs"> <SubType>Form</SubType> </Compile> <Compile Include="Form1.Designer.cs"> <DependentUpon>Form1.cs</DependentUpon> </Compile> <Compile Include="Program.cs" /> <EmbeddedResource Include="Form1.resx"> <DependentUpon>Form1.cs</DependentUpon> </EmbeddedResource> <EmbeddedResource Include="Properties\Resources.resx"> <Generator>ResXFileCodeGenerator</Generator> <LastGenOutput>Resources.Designer.cs</LastGenOutput> <SubType>Designer</SubType> </EmbeddedResource> <Compile Include="Properties\Resources.Designer.cs"> <AutoGen>True</AutoGen> <DependentUpon>Resources.resx</DependentUpon> <DesignTime>True</DesignTime> </Compile> <None Include="Properties\Settings.settings"> <Generator>SettingsSingleFileGenerator</Generator> <LastGenOutput>Settings.Designer.cs</LastGenOutput> </None> <Compile Include="Properties\Settings.Designer.cs"> <AutoGen>True</AutoGen> <DependentUpon>Settings.settings</DependentUpon> <DesignTimeSharedInput>True</DesignTimeSharedInput> </Compile> </ItemGroup> <ItemGroup> <None Include="App.config" /> </ItemGroup> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <!-- To modify your build process, add your task inside one of the targets below and uncomment it. Other similar extension points exist, see Microsoft.Common.targets. <Target Name="BeforeBuild"> </Target> <Target Name="AfterBuild"> </Target> --> </Project>
SCREENSHOTS
