Insert Update and Delete in Asp.Net C#

Hi Friends,
In this tutorial i wanna going to show you " How To Insert, Update and Delete Record in Asp.Net C# ".


For Insert, Update and Delete Record We need to Design Form same as Abow.

After Design the form We Need to Create Database.

Than Write following Code on Particular Events.

CODE IN C#  

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.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Student.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd;
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            cn.Open();
            cmd = new SqlCommand("Insert into Student_Info values(" + textBox1.Text + ",'" + textBox2.Text + "','" + textBox3.Text + "')", cn);
          
            cmd.ExecuteNonQuery();
            cn.Close();
            textBox1.Text = " ";
            textBox2.Text = " ";
            textBox3.Text = " ";
            MessageBox.Show("Insert successfully.");
            DataDisplay();
        }

        private void button2_Click(object sender, EventArgs e)
        {
            cn.Open();
            cmd = new SqlCommand("Delete from Student_Info where RollNo=" + textBox1.Text + "", cn);
            cmd.ExecuteNonQuery();
            cn.Close();
            textBox1.Text = " ";
            textBox2.Text = " ";
            textBox3.Text = " ";
            MessageBox.Show("Delete Succcessfully.");
            DataDisplay();
        }

        private void button3_Click(object sender, EventArgs e)
        {
            cn.Open();
            cmd = new SqlCommand("Update Student_Info set Name='" + textBox2.Text + "', Address='" + textBox3.Text + "' where RollNo=" + textBox1.Text + "", cn);
            cmd.ExecuteNonQuery();
            cn.Close();
            textBox1.Text = " ";
            textBox2.Text = " ";
            textBox3.Text = " ";
            MessageBox.Show("Update Succcessfully.");
            DataDisplay();
        }
        public void DataDisplay()
        {
            DataTable dt = new DataTable();
            cn.Open();
            cmd = new SqlCommand("Select * from Student_Info", cn);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            da.Fill(dt);
            cn.Close();
            dataGridView1.DataSource = dt;
        }

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

    }
}

CODE IN VB.NET

Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'Database1DataSet1.customer' table. You can move, or remove it, as needed.
        Me.StudentTableAdapter.Fill(Me.StudentDataSet1.Student)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Me.StudentBindingSource.AddNew()
        TextBox1.Enabled = True
        TextBox2.Enabled = True
        TextBox3.Enabled = True
        TextBox1.Text = ""
        TextBox2.Text = ""
        TextBox3.Text = ""
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Enabled = True
        TextBox2.Enabled = True
        TextBox3.Enabled = True
    End Sub

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        Me.BindingContext(Me.StudentDataSet1).RemoveAt(DataGridView1.SelectedRows(0).Index)
        Me.Validate()
        Me.StudentBindingSource.EndEdit()
        Me.StudentTableAdapter.Update(Me.StudentDataSet1.Student)
        MsgBox("Record Deleted Successfully!!!")
    End Sub
 End Class

 

Enjoy.
I Hope You Like This Tutorials. In our Next Tutorial we will Discuss on how to write query with parameter.   . 
Previous
Next Post »

If you have any kind of question about any post, Feel free to ask.You can simply drop a comment below post. Your feedback and suggestions will be highly appreciated. ConversionConversion EmoticonEmoticon