You are on page 1of 2

namespace WindowsFormsApp5

{
public partial class Form2 : Form
{
SqlConnection con = new SqlConnection("Data Source = ASUS-X555; Initial
Catalog=db_exercisebatch4;Integrated Security=True");
SqlCommand cmd;
SqlDataAdapter adapt;
public Form2()
{
InitializeComponent();
DisplayData();
}

private void textBox3_TextChanged(object sender, EventArgs e)


{

private void label2_Click(object sender, EventArgs e)


{

private void btn_add_Click(object sender, EventArgs e)


{
if (txt_id.Text != "" && txt_authorname.Text != "" && txt_gender.Text!="")
{
cmd = new SqlCommand("insert into tb_author values(@id,@name,@gender)",
con);
con.Open();
cmd.Parameters.AddWithValue("@id", txt_id.Text);
cmd.Parameters.AddWithValue("@name", txt_authorname.Text);
cmd.Parameters.AddWithValue("@gender", txt_gender.Text);

cmd.ExecuteNonQuery();
con.Close();
MessageBox.Show("Record Inserted Successfully");
DisplayData();
ClearData();
}
else
{
MessageBox.Show("Please Provide Details!");
}
}
private void DisplayData()
{
con.Open();
DataTable dt = new DataTable();
adapt = new SqlDataAdapter("select * from tb_author", con);
adapt.Fill(dt);
dataGridView1.DataSource = dt;
con.Close();
}
//Clear Data
private void ClearData()
{
txt_id.Text = "";
txt_authorname.Text = "";
txt_gender.Text = "";
}
private void dataGridView1_RowHeaderMouseClick(object sender,
DataGridViewCellMouseEventArgs e)
{
txt_id.Text = dataGridView1.Rows[e.RowIndex].Cells[0].Value.ToString();
txt_authorname.Text =
dataGridView1.Rows[e.RowIndex].Cells[1].Value.ToString();
txt_gender.Text = dataGridView1.Rows[e.RowIndex].Cells[2].Value.ToString();

You might also like