public JsonResult Delete(MSatuan obj, MUser user) { JsonResult jsonResult = new JsonResult(); if (user != null && user.id >= 1) { using (NpgsqlConnection con = new NpgsqlConnection(StrKon)) { using (NpgsqlCommand com = new NpgsqlCommand("", con)) { using (NpgsqlDataAdapter oDA = new NpgsqlDataAdapter(com)) { try { con.Open(); com.CommandTimeout = con.ConnectionTimeout; com.Transaction = com.Connection.BeginTransaction(); com.CommandText = "update msatuan set aktif=0 where id=@id"; com.Parameters.Clear(); com.Parameters.AddWithValue("@id", obj.id); com.ExecuteNonQuery(); jsonResult.JSONMessage = "Data satuan id=" + obj.id + " dihapus"; jsonResult.JSONResult = true; jsonResult.JSONRows = 1; jsonResult.JSONValue = obj; Repository.RepLogData.insertLog(com, Newtonsoft.Json.JsonConvert.SerializeObject(user, formatting), Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult, formatting)); jsonResult.JSONMessage = "Data berhasil dihapus"; jsonResult.JSONResult = true; jsonResult.JSONRows = 0; jsonResult.JSONValue = null; com.Transaction.Commit(); } catch (Exception e) { jsonResult.JSONMessage = "ERR : " + e.Message; jsonResult.JSONResult = false; jsonResult.JSONRows = 0; jsonResult.JSONValue = null; } } } } } else { jsonResult.JSONMessage = "User untuk hapus data harus diisi"; jsonResult.JSONResult = false; jsonResult.JSONRows = 0; jsonResult.JSONValue = null; } return(jsonResult); }
public JsonResult Update(MSatuan obj, MUser user) { JsonResult jsonResult = new JsonResult(); bool validasi = true; if (user != null && user.id >= 1) { using (NpgsqlConnection con = new NpgsqlConnection(StrKon)) { using (NpgsqlCommand com = new NpgsqlCommand("", con)) { using (NpgsqlDataAdapter oDA = new NpgsqlDataAdapter(com)) { try { con.Open(); com.CommandTimeout = con.ConnectionTimeout; com.Transaction = com.Connection.BeginTransaction(); if (validasi) { if (obj.kode.Length <= 0) { jsonResult.JSONMessage = "Data kode yang anda masukkan harus terisi."; validasi = false; } } if (validasi) { if (obj.nama.Length <= 0) { jsonResult.JSONMessage = "Data nama yang anda masukkan harus terisi."; validasi = false; } } if (validasi) { if (obj.konversi <= 0) { jsonResult.JSONMessage = "Data konversi yang anda masukkan harus terisi."; validasi = false; } } if (validasi) { com.CommandText = "select count(id) from msatuan where id<>@id and upper(kode)=@kode or upper(nama)=@nama"; com.Parameters.Clear(); com.Parameters.AddWithValue("@id", obj.id); com.Parameters.AddWithValue("@kode", obj.kode.ToUpper()); com.Parameters.AddWithValue("@nama", obj.nama.ToUpper()); if ((long)com.ExecuteScalar() >= 1) { jsonResult.JSONMessage = "Data kode atau nama yang anda masukkan sudah terdaftar."; validasi = false; } } //Validasi done com.CommandText = "update msatuan set kode=@kode, nama=@nama, aktif=@aktif, konversi=@konversi where id=@id"; com.Parameters.Clear(); com.Parameters.AddWithValue("@id", obj.id); com.Parameters.AddWithValue("@kode", obj.kode); com.Parameters.AddWithValue("@nama", obj.nama); com.Parameters.AddWithValue("@aktif", obj.aktif); com.Parameters.AddWithValue("@konversi", obj.konversi); com.ExecuteNonQuery(); jsonResult.JSONMessage = "Data satuan id=" + obj.id + " diupdate"; jsonResult.JSONResult = true; jsonResult.JSONRows = 1; jsonResult.JSONValue = obj; Repository.RepLogData.insertLog(com, Newtonsoft.Json.JsonConvert.SerializeObject(user, formatting), Newtonsoft.Json.JsonConvert.SerializeObject(jsonResult, formatting)); jsonResult.JSONMessage = "Data berhasil diupdate"; jsonResult.JSONResult = true; jsonResult.JSONRows = 0; jsonResult.JSONValue = null; com.Transaction.Commit(); } catch (Exception e) { jsonResult.JSONMessage = "ERR : " + e.Message; jsonResult.JSONResult = false; jsonResult.JSONRows = 0; jsonResult.JSONValue = null; } } } } } else { jsonResult.JSONMessage = "User untuk update data harus diisi"; jsonResult.JSONResult = false; jsonResult.JSONRows = 0; jsonResult.JSONValue = null; } return(jsonResult); }