public Boolean Getdata(Entity.EntityMahasiswa EM, string nim) { using (SqlConnection con = new SqlConnection("Data Source=IGIHCKSN\\SQLEXPRESS;Initial Catalog=Fp_PRESENSI;Integrated Security=True ")) { con.Open(); using (SqlCommand cmd = new SqlCommand("SELECT NIM,NAMA FROM MAHASISWA where NIM = '" + nim + "'", con)) { using (SqlDataReader reader = cmd.ExecuteReader()) { if (reader.HasRows) { while (reader.Read()) { EM.Nim = reader["NIM"].ToString(); EM.Nama = reader["NAMA"].ToString(); } status = true; } else { status = false; } reader.Close(); } // reader closed and disposed up here } // command disposed here con.Close(); } nim = ""; return(status); }
private void txtID_KeyDown(object sender, KeyEventArgs e) { mhs = new Entity.EntityMahasiswa(); if (e.KeyCode == Keys.Enter) { if (imhs.Getdata(mhs, txtID.Text) == true) { string[] row = new string[] { mhs.Nim, mhs.Nama }; dgvPresensi.Rows.Add(row); txtID.Text = ""; txtID.Select(); } else { MessageBox.Show("NIM TIDAK ADA"); txtID.Text = ""; txtID.Select(); } } }