Before using the following steps ensure that you have crated a table. Let the table be Sampletable with attributes Id, Name and State. The attribute Id can be set to auto increment so that the values will be incremented automatically and will serve as a primary key of your table.Enter few rows into your table.
Now, open visual studio and open a new project and then select Windows Forms application from the list. Set the name and location of your project as required. See the figure below for reference.
Reference: http://www.c-sharpcorner.com |
Create a form (sampleform)and add controls to it. Drag and drop textbox, labels, buttons and DataGridView controls from the toolbox. Name these controls as applicable.
Two textboxes can be used because there are two attributes present in the table.( Id is ignored because it is set to auto-increment). Two labels can be used against these textboxes to increase readability. Three buttons each for insert, delete and update can be used. See the figure below.
Reference : www.c-sharpcorner.com |
Now, right click on the form and go to the code area. Add the following namespaces at the beginning of the code.(if not added previously)
using System.Data ;
using System.Data.SqlClient;
See the following code, This code is derived from www.c-sharpcorner.com. for more details go through the website.
CODE:
- using System;
- using System.Data;
- using System.Windows.Forms;
- using System.Data.SqlClient;
- namespace InsertUpdateDeleteDemo
- {
- public partial class sampleform : Form
- {
- SqlConnection con= new SqlConnection("Data Source=siyaram.;Initial Catalog=Sample;Integrated Security=true;");
- SqlCommand cmd;
- SqlDataAdapter adapt;
- //ID variable used in Updating and Deleting Record
- int ID = 0;
- public sampleform()
- {
- InitializeComponent();
- DisplayData();
- }
- //Insert Data
- private void btn_Insert_Click(object sender, EventArgs e)
- {
- if (txt_Name.Text != "" && txt_State.Text != "")
- {
- cmd = new SqlCommand("insert into sampletable
- (Name,State) values(@name,@state)", con);
- con.Open();
- cmd.Parameters.AddWithValue("@name", txt_Name.Text);
- cmd.Parameters.AddWithValue("@state", txt_State.Text);
- cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("Record Inserted Successfully");
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Please fill Name and State");
- }
- }
- //Display Data in DataGridView
- private void DisplayData()
- {
- con.Open();
- DataTable dt=new DataTable();
- adapt=new SqlDataAdapter("select * from sampletable",con);
- adapt.Fill(dt);
- dataGridView1.DataSource = dt;
- con.Close();
- }
- //Clear Data
- private void ClearData()
- {
- txt_Name.Text = "";
- txt_State.Text = "";
- ID = 0;
- }
- //dataGridView1 RowHeaderMouseClick Event
- private void dataGridView1_RowHeaderMouseClick(object sender, DataGridViewCellMouseEventArgs e)
- {
- ID = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString());
- txt_Name.Text = dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
- txt_State.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();
- }
- //Update Record
- private void btn_Update_Click(object sender, EventArgs e)
- {
- if (txt_Name.Text != "" && txt_State.Text != "")
- {
- cmd = new SqlCommand("update sampletable set Name=@name,State=@state where ID=@id", con);
- con.Open();
- cmd.Parameters.AddWithValue("@id", ID);
- cmd.Parameters.AddWithValue("@name", txt_Name.Text);
- cmd.Parameters.AddWithValue("@state", txt_State.Text);
- cmd.ExecuteNonQuery();
- MessageBox.Show("Record Updated Successfully");
- con.Close();
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Please Select Record to Update");
- }
- }
- //Delete Record
- private void btn_Delete_Click(object sender, EventArgs e)
- {
- if(ID!=0)
- {
- cmd = new SqlCommand("delete sampletable where ID=@id",con);
- con.Open();
- cmd.Parameters.AddWithValue("@id",ID);
- cmd.ExecuteNonQuery();
- con.Close();
- MessageBox.Show("Record Deleted Successfully!");
- DisplayData();
- ClearData();
- }
- else
- {
- MessageBox.Show("Please Select Record to Delete");
- }
- }
- }
- }
Save your project and check the result. check all the options like insertion, deletion and updation.
Enjoy Programming.
No comments:
Post a Comment