0% found this document useful (0 votes)
50 views2 pages

Namespace Public Partial Class New: Form2

This C# code defines a form class that connects to a SQL database and allows adding, displaying, and updating author data. The form initializes a SQL connection and command. It displays author data from the database in a datagrid on load. Buttons allow adding new authors which inserts data into the database table, and clicking a row retrieves that record's data.

Uploaded by

Muhammad Pratama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
50 views2 pages

Namespace Public Partial Class New: Form2

This C# code defines a form class that connects to a SQL database and allows adding, displaying, and updating author data. The form initializes a SQL connection and command. It displays author data from the database in a datagrid on load. Buttons allow adding new authors which inserts data into the database table, and clicking a row retrieves that record's data.

Uploaded by

Muhammad Pratama
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 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