Step 9

Insert

Double-click on the Insert button. Then, write a code under the function to create query variable and in-store the SQL insert command. Then we define SQL CommandText property equal to query, open the connection, and call to ExecuteNonQuery function, then close the connection. Here we make another function Clear data for after submitting details to clear the text boxes.

private void btninsert_Click(object sender, EventArgs e) {
    string query = $ "insert into teacher values('{txtid.Text.ToString()}','{txtname.Text}','{txtaddress.Text}','{txtsalary.Text.ToString()}')";
    cmd.CommandText = query;
    conn.Open();
    cmd.ExecuteNonQuery();
    cleardata();
    conn.Close();
    displaydata();
}
private void cleardata() {
    txtid.Clear();
    txtname.Clear();
    txtaddress.Clear();
    txtsalary.Clear();
}

Comments

Post a comment