private void button1_Click(object sender, EventArgs e) { try { if (ValidateData() == 0) { Koneksi kon = new Koneksi(); SqlConnection sqlcon = kon.getConnection(); string msg = string.Empty; using (sqlcon) { sqlcon.Open(); string sql = "insert into nasabah (id, nama, nohp, alamat, usia) values( @id, @nama, @nohp, @alamat, @usia)"; SqlCommand sqlcom = new SqlCommand(sql, sqlcon); using (sqlcom) { sqlcom.Parameters.AddWithValue("@id", tb_id.Text.ToString()); sqlcom.Parameters.AddWithValue("@nama", tb_nama.Text); sqlcom.Parameters.AddWithValue("@nohp", tb_nohp.Text); sqlcom.Parameters.AddWithValue("@alamat", tb_alamat.Text); sqlcom.Parameters.AddWithValue("@usia", tb_usia.Text.ToString()); int res = sqlcom.ExecuteNonQuery(); msg = (res != 0 ? "Data has been saved." : "Oops, something went wrong."); } sqlcon.Close(); } MessageBox.Show(msg); loadTable(); ClearForm(); tb_id.Text = generateID(); //AutoGenerate(); } else { MessageBox.Show("Please Fix The Error"); } } catch (Exception exc) { MessageBox.Show(exc.ToString()); } }
private void loadTable() { try { Koneksi kon = new Koneksi(); SqlConnection sqlcon = kon.getConnection(); string sql = "select * from nasabah;"; SqlCommand sqlcom = new SqlCommand(sql, sqlcon); SqlDataAdapter sqlda = new SqlDataAdapter(); sqlda.SelectCommand = sqlcom; DataTable dTable = new DataTable(); sqlda.Fill(dTable); dataGridView1.DataSource = dTable; // here i have assign dTable object to the dataGridView1 object to display data. sqlcon.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
private void button3_Click(object sender, EventArgs e) { try { Koneksi kon = new Koneksi(); SqlConnection sqlcon = kon.getConnection(); //string sql = "select * from nasabah where id = '" + tb_search.Text + "';"; string sql = "select * from nasabah where nama like '%" + tb_search.Text + "%';"; SqlCommand sqlcom = new SqlCommand(sql, sqlcon); SqlDataAdapter sqlda = new SqlDataAdapter(); sqlda.SelectCommand = sqlcom; DataTable dTable = new DataTable(); sqlda.Fill(dTable); dataGridView1.DataSource = dTable; // here i have assign dTable object to the dataGridView1 object to display data. sqlcon.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
protected string generateID() { string sID = null; int ID = 0; Koneksi kon = new Koneksi(); SqlConnection sqlconn = kon.getConnection(); sqlconn.Open(); DataTable dt = new DataTable(); SqlDataReader myReader = null; SqlCommand myCommand = new SqlCommand("select TOP 1 id from nasabah order by id DESC", sqlconn); myReader = myCommand.ExecuteReader(); if (myReader.Read()) { sID = (myReader["id"].ToString()); ID = Convert.ToInt32(sID.Substring(1, 3)); ID += 1; if (ID <= 9) { sID = "N00" + ID; } else if (ID <= 90) { sID = "N0" + ID; } else if (ID <= 900) { sID = "N" + ID; } } else { sID = "N001"; } sqlconn.Close(); return(sID); }
private void button1_Click(object sender, EventArgs e) { try { if (ValidateData() == 0) { Koneksi kon = new Koneksi(); SqlConnection sqlcon = kon.getConnection(); string msg = string.Empty; using (sqlcon) { sqlcon.Open(); string sql = "insert into gajinasabah (nama, gaji) values( @nama, @gaji)"; SqlCommand sqlcom = new SqlCommand(sql, sqlcon); using (sqlcom) { sqlcom.Parameters.AddWithValue("@nama", tb_nama.Text); sqlcom.Parameters.AddWithValue("@gaji", cb_gaji.Text.ToString()); int res = sqlcom.ExecuteNonQuery(); msg = (res != 0 ? "Data has been saved." : "Oops, something went wrong."); } sqlcon.Close(); } MessageBox.Show(msg); //AutoGenerate(); } else { MessageBox.Show("Please Fix The Error"); } } catch (Exception exc) { MessageBox.Show(exc.ToString()); } }
private void button2_Click(object sender, EventArgs e) { Koneksi kon = new Koneksi(); SqlConnection sqlcon = kon.getConnection(); string msg = string.Empty; using (sqlcon) { sqlcon.Open(); string sql = "delete from nasabah where id = @id"; SqlCommand sqlcom = new SqlCommand(sql, sqlcon); using (sqlcon) { sqlcom.Parameters.AddWithValue("@id", tb_id.Text); int res = sqlcom.ExecuteNonQuery(); msg = (res != 0 ? "Data has been deleted " : "Oops, something went wrong"); } sqlcon.Close(); } MessageBox.Show(msg); ClearForm(); loadTable(); tb_id.Text = generateID(); }