Click here to Skip to main content
15,908,274 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace WindowsFormsApplication7
{
    public partial class Form1 : Form
    {
        string strCon = "Data Source=CIODEV03\\SQLEXPRESS;Initial Catalog=EmployeeDB;Integrated Security=True";
        
        string strSQL;

        SqlConnection conn;
        SqlCommand cmd;
        public Form1()
        {
            InitializeComponent();


        }
        private void Form1_Load(object sender, EventArgs e)
        {
           string strSQL = "select * from gridview";


            SqlDataAdapter dataAdapter = new SqlDataAdapter(strSQL, strCon);

            SqlCommandBuilder commandBuilder = new SqlCommandBuilder(dataAdapter);
            DataTable table = new DataTable();

            table.Locale = System.Globalization.CultureInfo.InvariantCulture;

            dataAdapter.Fill(table);

            bindingSource1.DataSource = table;
            dataGridView1.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.AllCellsExceptHeader);
            dataGridView1.ReadOnly = true;
            dataGridView1.DataSource = bindingSource1;
        }


        private void btninsert_Click(object sender, EventArgs e)
        {
            strSQL = "insert into gridview values('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "')";
            conn = new SqlConnection(strCon);
            cmd = new SqlCommand(strSQL, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
           
            dataGridView1.DataSource = bindingSource1;
             conn.Close();
        }

        private void btnupdate_Click(object sender, EventArgs e)
        {
            strSQL = "update gridview set ID='" + textBox1.Text + "',Name='" + textBox2.Text + "',Address='" + textBox3.Text +  "'";
            conn = new SqlConnection(strCon);
            cmd = new SqlCommand(strSQL, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            dataGridView1.DataSource = bindingSource1;
           

        }

        private void btndelete_Click(object sender, EventArgs e)
        {
            strSQL = "delete from gridview";
            conn = new SqlConnection(strCon);
            cmd = new SqlCommand(strSQL, conn);
            conn.Open();
            cmd.ExecuteNonQuery();
            dataGridView1.DataSource = bindingSource1;
        }
    }
}
Posted
Updated 6-Mar-12 0:12am
v2

1 solution

 
Share this answer
 
Comments
Adersh Ram 6-Mar-12 6:19am    
what the error ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900