Hi Guys,
I'm trying to use multiple command for save and update data in diffrence table.
- on tbl_pembayaran command for save data
- on tbl_pinjaman command for update data
in tbl_pembayaran command for save data working properly, but on tbl_pinjaman command for update data not working, I'm get error
Error converting data type nvarchar to numeric.
when update data on table 2. Anny help could be appriciate.
Store Prosedure for Save Data on tbl_pembayaran
CREATE PROCEDURE [dbo].[spAddPembayaran]
@id_pembayaran varchar(50),
@tanggal_pembayaran datetime,
@tanggal_jatuh_tempo datetime,
@id_pinjaman varchar(50),
@cicilan_ke int,
@denda numeric(18, 0),
@jumlah_pembayaran numeric(18, 0),
@jumlah_hari_terlambat_bayar int,
@keterangan text,
@id_user varchar(50),
@id_perusahaan varchar(50)
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Insert Into [dbo].[tbl_pembayaran]
([id_pembayaran],
[tanggal_pembayaran],
[tanggal_jatuh_tempo],
[id_pinjaman],
[cicilan_ke],
[denda],
[jumlah_pembayaran],
[jumlah_hari_terlambat_bayar],
[keterangan],
[id_user],
[id_perusahaan])
VALUES
(@id_pembayaran,
@tanggal_pembayaran,
@tanggal_jatuh_tempo,
@id_pinjaman,
@cicilan_ke,
@denda,
@jumlah_pembayaran,
@jumlah_hari_terlambat_bayar,
@keterangan,
@id_user,
@id_perusahaan)
END
Store Prosedure for Update Data on tbl_pinjaman
CREATE PROCEDURE [dbo].[spUpdatePinjaman]
@id_pinjaman varchar(50),
@sisa_pinjaman numeric(18, 0),
@status_pinjaman varchar(50),
@tanggal_jatuh_tempo datetime
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
Update dbo.tbl_pinjaman
Set sisa_pinjaman=@sisa_pinjaman,
status_pinjaman=@status_pinjaman,
tanggal_jatuh_tempo = @tanggal_jatuh_tempo
Where id_pinjaman=@id_pinjaman
END
Code Bihind
//this is command for update data on tbl_pinjaman
//this command not working when running get error Error converting data type nvarchar to numeric.
private void UpdateTglJatuhTempoToPinjamanAndUpdateJumlahSisaPinjaman()
{
using (SqlConnection con = new SqlConnection(koneksi))
{
using (SqlCommand cmd = new SqlCommand("spUpdatePinjaman"))
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id_pinjaman",txtid_pinjaman.Text.Trim());
cmd.Parameters.AddWithValue("@sisa_pinjaman", txtjumlah_sisa_pinjaman_sekarang.Text.Trim());
cmd.Parameters.AddWithValue("@status_pinjaman", txt_status_pinjaman.Text.Trim());
cmd.Parameters.Add("@tanggal_jatuh_tempo",System.Data.SqlDbType.DateTime);
cmd.Parameters["@tanggal_jatuh_tempo"].Value = txtSettgl_jatuh_tempo.Text;
cmd.ExecuteNonQuery();
}
}
}
protected void btnAddDataPembayaran_Click(object sender, EventArgs e)
{
ClientScript.RegisterStartupScript(this.GetType(), "PopUp", "MessageSucces();", true);
AddDataPembayaran();
UpdateTglJatuhTempoToPinjamanAndUpdateJumlahSisaPinjaman();
}
//this is command for save data on tbl_pembayaran
//this command working properly
private void AddDataPembayaran()
{
using (SqlConnection con = new SqlConnection(koneksi))
{
using (SqlCommand cmd = new SqlCommand("spAddPembayaran"))
{
try
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@id_pembayaran", txtid_pembayaran.Text.Trim());
cmd.Parameters.Add("@tanggal_pembayaran", System.Data.SqlDbType.DateTime);
cmd.Parameters["@tanggal_pembayaran"].Value = txttanggal_pembayaran.Text.Trim();
cmd.Parameters.Add("@tanggal_jatuh_tempo", System.Data.SqlDbType.DateTime);
cmd.Parameters["@tanggal_jatuh_tempo"].Value = txtSettgl_jatuh_tempo.Text.Trim();
cmd.Parameters.AddWithValue("@id_pinjaman", txtid_pinjaman.Text.Trim());
cmd.Parameters.AddWithValue("@cicilan_ke", txtcicilan_ke.Text.Trim());
cmd.Parameters.Add("@denda", System.Data.SqlDbType.Decimal);
cmd.Parameters["@denda"].Value = txtjumlah_denda_terlambat_pembayaran.Text.Trim();
cmd.Parameters.Add("@jumlah_pembayaran", System.Data.SqlDbType.Decimal);
cmd.Parameters["@jumlah_pembayaran"].Value = txtjumlah_pembayaran.Text.Trim();
cmd.Parameters.AddWithValue("@jumlah_hari_terlambat_bayar", txtterlambat_pembayaran.Text.Trim());
cmd.Parameters.AddWithValue("@keterangan", txtketerangan.Text.Trim());
cmd.Parameters.AddWithValue("@id_user", txtid_user.Text.Trim());
cmd.Parameters.AddWithValue("@id_perusahaan", txtid_perusahaan.Text.Trim());
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception ex)
{
throw ex;
}
}
}
}