Hi sir/mam,
I have tried above subject in MS VS 2010 but i couldn't succeed in that so i have attached my code. Please rectify and send to me,so that it will be very useful for me in learning this project completely.kindly rectify my mistakes in the code 2 get output.
i hav created id , name , age , phone , email in database , its enough .
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;
using System.Data;
using System.Configuration;
using CrystalDecisions.CrystalReports.Engine;
namespace createEmp
{
public partial class Form1 : Form
{
static string connectionString = "Data Source=DIVYA-PC;Initial Catalog=emp;Integrated Security=True";
SqlConnection con = new SqlConnection(connectionString);
public Form1()
{
InitializeComponent();
GetData();
}
private void Form1_Load(object sender, EventArgs e)
{
SqlConnection connection = new SqlConnection("Data Source=DIVYA-PC;Initial Catalog=emp;Integrated Security=True");
SqlCommand cmd = new SqlCommand("Select top 10 * From employee", connection);
SqlDataAdapter adapter = new SqlDataAdapter(cmd);
DataSet dataset = new DataSet();
adapter.Fill(dataset, "employee");
}
private void btninsert_Click(object sender, EventArgs e)
{
string sql = "Insert into employee(id,name,age,phone,email)";
sql += "values('" + txtid.Text + "','" + txtname.Text + "','" + txtage.Text + "','" + txtphone.Text + "','" + txtemail.Text + "' )";
if (con.State != System.Data.ConnectionState.Open)
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
MessageBox.Show("Record committed");
}
else
{
con.Close();
}
}
private void GetData()
{
string sql = "select * from employee ";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
dgvdataGridView1.DataSource = dt;
}
private void btnupdate_Click(object sender, EventArgs e)
{
string sql = "Update employee set name='" + txtname.Text + "'";
sql += "where id='" + txtid.Text + "',age='" + txtage.Text + "',phone='" + txtphone.Text + "',email='" + txtname.Text + "'";
if (con.State != System.Data.ConnectionState.Open)
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
MessageBox.Show("Record update Id No:- " + txtid.Text);
GetData();
}
else
{
con.Close();
}
}
private void button3_Click(object sender, EventArgs e)
{
string sql = "delete employee set name='" + txtname.Text + "'";
sql += "where id='" + txtid.Text + "',age='" + txtage.Text + "',phone='" + txtphone.Text + "',email='" + txtname.Text + "'";
if (con.State != System.Data.ConnectionState.Open)
{
con.Open();
SqlCommand cmd = new SqlCommand(sql, con);
cmd.ExecuteNonQuery();
MessageBox.Show("Record delete Id No:- " + txtid.Text);
GetData();
}
else
{
con.Close();
}
}
private void dgvdataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{
int i;
i = dgvdataGridView1.SelectedCells[0].RowIndex;
txtid.Text = dgvdataGridView1.Rows[i].Cells[0].Value.ToString();
txtname.Text = dgvdataGridView1.Rows[i].Cells[1].Value.ToString();
txtage.Text = dgvdataGridView1.Rows[i].Cells[2].Value.ToString();
txtphone.Text = dgvdataGridView1.Rows[i].Cells[3].Value.ToString();
txtemail.Text = dgvdataGridView1.Rows[i].Cells[4].Value.ToString();
txtid.Text = "";
txtname.Text = "";
txtage.Text = "";
txtphone.Text = "";
txtemail.Text = "";
}
}
}