Naidu AMMAN

Naidu AMMAN

  • NA
  • 37
  • 35.2k

how to get data in data gried view in this code

Jan 4 2017 11:47 PM
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace SAMPLE
{
public partial class EmpDeatils : Form
{
public EmpDeatils()
{
InitializeComponent();
}
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
DataSet ds;
SqlDataAdapter da;
SqlCommandBuilder bldr;
private void EmpDeatils_Load(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=3");
con.Open();
cmd = new SqlCommand("select * from table_name", con);
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
con.Close();
}
private void display()
{
da = new SqlDataAdapter("Select * from table_name", con);
ds = new DataSet();
da.MissingSchemaAction = MissingSchemaAction.AddWithKey;
da.Fill(ds, "table_name");
bldr = new SqlCommandBuilder(da);
dataGridView1.DataSource = ds.Tables["table_name"];
}
private void display_btn_Click(object sender, EventArgs e)
{
display();
}
private void button7_Click(object sender, EventArgs e)
{
this.Close();
}
private void btninsert_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=3");
con.Open();
int aa = Convert.ToInt32(TXTEMP_ID.Text);
string bb = TXTEMP_NAME.Text;
int cc = Convert.ToInt32(TXTSALARY.Text);
cmd = new SqlCommand("INSERT INTO table_name(Emp_ID, Emp_Name,Salary) VALUES ('" + aa + "','" + bb + "','" + cc + "')", con);
cmd.ExecuteNonQuery();
MessageBox.Show("one record inserted:");
con.Close();
display();
}
private void button3_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=3");
con.Open();
int aa = Convert.ToInt32(TXTEMP_ID.Text);
cmd = new SqlCommand("DELETE FROM table_name where Emp_ID='" + aa + "'", con);
cmd.ExecuteNonQuery();
MessageBox.Show("one record Delete:");
con.Close();
display();
}
private void btnupdate_Click(object sender, EventArgs e)
{
con = new SqlConnection("Data Source=(LocalDB)\v11.0;Integrated Security=True;Connect Timeout=3");
con.Open();
int aa = Convert.ToInt32(TXTEMP_ID.Text);
string bb = TXTEMP_NAME.Text;
int cc = Convert.ToInt32(TXTSALARY.Text);
string abc = "UPDATE table_name SET Emp_ID ='" + aa + "', Emp_Name ='" + bb + "',Salary ='" + cc + "' WHERE Emp_ID = '" + aa + "'";
SqlCommand cmd = new SqlCommand(abc, con);
cmd.ExecuteNonQuery();
MessageBox.Show("one record updated:");
con.Close();
display();
}
private void btnsearch_Click(object sender, EventArgs e)
{
con.Open();
int aa = Convert.ToInt32(TXTEMP_ID.Text);
string abc = "SELECT Emp_ID,Emp_Name,Salary FROM table_name where Emp_ID='" + aa + "'";
cmd = new SqlCommand(abc, con);
MessageBox.Show("one record search:");
dr = cmd.ExecuteReader();
DataTable dt = new DataTable();
dt.Load(dr);
dataGridView1.DataSource = dt;
con.Close();
}
private void btntotalrecord_Click(object sender, EventArgs e)
{
con.Open();
cmd = new SqlCommand("select Count(*) from Employee", con);
int a = (int)cmd.ExecuteScalar();
label4.Text = "Total Record:--> " + a.ToString();
con.Close();
}
}
}

Answers (1)