private void button2_Click(object sender, EventArgs e) { //[CONNECTION STRINGS] var connectionString = ConfigurationManager.ConnectionStrings["EmployeeManagement"].ConnectionString; SqlConnection con = new SqlConnection(connectionString); //when the button is pressed go through all data from the Employees table and then display con.Open(); SqlCommand cmd = con.CreateCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "select * from Employees"; cmd.ExecuteNonQuery(); DataTable dt = new DataTable(); SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); ListOfUsers.DataSource = dt; ListOfUsers.Refresh(); con.Close(); panel2.Visible = true; Dashboard.Visible = false; panel3.Visible = false; panel4.Visible = false; holidayPanel.Visible = false; }
private void SearchUser_Click(object sender, EventArgs e) { //opens the connection var connectionString = ConfigurationManager.ConnectionStrings["EmployeeManagement"].ConnectionString; SqlConnection con = new SqlConnection(connectionString); //checks to see if the search box is populated if not then show all employees if (textBox3.Text == "") { //opens connection con.Open(); //creating the sql command SqlCommand Tcmd = con.CreateCommand(); //setting the command type Tcmd.CommandType = CommandType.Text; //selecting all employees Tcmd.CommandText = "select * from Employees"; //executing the sql Tcmd.ExecuteNonQuery(); //creating the new datatable to fill DataTable dta = new DataTable(); SqlDataAdapter adapt = new SqlDataAdapter(Tcmd); //filling the adapter with the correct data from the dt adapt.Fill(dta); //filling the dgv with the data ListOfUsers.DataSource = dta; con.Close(); //refreshing the dgv ListOfUsers.Refresh(); } else { //opens connection con.Open(); //selected everything from the dgv where the first name is equal to whats in the txtbox SqlDataAdapter search = new SqlDataAdapter("select * from Employees where FirstName like '" + textBox3.Text + "%'", con); DataTable dt = new DataTable(); search.Fill(dt); ListOfUsers.DataSource = dt; con.Close(); textBox3.Clear(); } }