you may try this template
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 WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void groupBox1_Enter(object sender, EventArgs e)
{
}
private void addbutton_Click(object sender, EventArgs e)
{
panel2.Hide();
panel1.Show();
}
private void showbutton_Click(object sender, EventArgs e)
{
panel1.Hide();
panel2.Hide();
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FASTRACK\Documents\vb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string select = "select * from customer";
SqlCommand cmd = new SqlCommand(select, cn);
cn.Open();
DataTable dt = new DataTable();
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
dataGridView1.DataSource = dt;
cn.Close();
}
private void button1_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FASTRACK\Documents\vb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string add = "insert into customer(cname,cadd,cmobile) values(@cname,@cadd,@cmobile)";
SqlCommand cmd = new SqlCommand(add, cn);
cn.Open();
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
errorProvider1.SetError(button1, "No text box should be emty");
}
else
{
cmd.Parameters.AddWithValue("cname", textBox1.Text);
cmd.Parameters.AddWithValue("cadd", textBox2.Text);
cmd.Parameters.AddWithValue("cmobile", textBox3.Text);
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Record Added successfully", "Record Added", MessageBoxButtons.OK);
panel1.Hide();
}
}
private void deletebutton_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FASTRACK\Documents\vb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string delete = "delete from customer where cname=@cname";
SqlCommand cmd = new SqlCommand(delete, cn);
cn.Open();
cn.Close();
}
private void updatebutton_Click(object sender, EventArgs e)
{
panel1.Hide();
panel2.Show();
}
private void button2_Click(object sender, EventArgs e)
{
SqlConnection cn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Users\FASTRACK\Documents\vb.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
string update = "insert into customer(cname,cadd,cmobile) values(@cname,@cadd,@cmobile)";
SqlCommand cmd = new SqlCommand(update, cn);
cn.Open();
if (textBox4.Text == "" || textBox5.Text == "" || textBox6.Text == "")
{
errorProvider1.SetError(button1, "No text box should be emty");
}
else
{
cmd.Parameters.AddWithValue("cname", textBox4.Text);
cmd.Parameters.AddWithValue("cadd", textBox5.Text);
cmd.Parameters.AddWithValue("cmobile", textBox6.Text);
cmd.ExecuteNonQuery();
cn.Close();
MessageBox.Show("Record Added successfully", "Record update", MessageBoxButtons.OK);
panel2.Hide();
}
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}