Hi Guys,, I'm try to sum number from database using DataReader and then the value will showing to textbox. If from database the value row is zero will retrun value 1 in textbox, If from database the value row is 1 will retrun value 2 and so on in textbox by automatic read exist value from database. How to solve this problem. Anny help could be appriciate.
this is is the code bihind
C#
private void CariCicilanKe()
{
using (SqlConnection con = new SqlConnection(koneksi))
{
using (SqlCommand cmd = new SqlCommand())
{
con.Open();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "Select Count(*) from tbl_pembayaran where id_pinjaman = '" + txtid_pinjaman.Text.Trim() + "'";
using (SqlDataReader rdr = cmd.ExecuteReader())
{
rdr.Read();
if(rdr.HasRows)
{
//If from database the value row is zero will retrun value 1 in textbox
txtcicilan_ke.Text = Convert.ToString(1);
}
else
{
//If from database the value row is 1will retrun value 2 and so on in textbox by automatic read exist value from database
//How to solve this problem, this code not working.
txtcicilan_ke.Text = rdr["cicilan_ke"].ToString() + 1;
}
}
cmd.Connection.Close();
con.Close();
}
}
}

Jignesh KumarRank #91 · 22.9K reputationPosted Jun 7, 2021, 12:14 AM EDT
Sachin SinghRank #66 · 26.2K reputationPosted Jun 7, 2021, 5:13 AM EDT
Nitin SontakkeRank #264 · 9.6K reputationPosted Jun 7, 2021, 12:25 AM EDT
Tri SetiaRank #4,571 · 337 reputationPosted Jun 7, 2021, 1:40 AM EDT
@Jignesh Kumar
exactly is return value of rows sir,, I want the textbox fill automatic number, with this condition If from database the value row is zero will retrun value 1 in textbox and If from database the value row is 1 will return value 2 and so on in textbox by automatic read exist value from database.
Tri SetiaRank #4,571 · 337 reputationPosted Jun 7, 2021, 2:11 AM EDT
@Nitin Sontakke
I has been try the code sir, but directly retruning value 2 in textbox when code running.
when I'm entry data 0 on field cicilan_ke it should show value is 1 on textbox and then save the data to database. after that code read again the value on field cicilan_ke and will show 2 on textbox.
CREATE TABLE [dbo].[tbl_pembayaran]( [id_pembayaran] [varchar](50) NOT NULL, [tanggal_pembayaran] [datetime] NULL, [id_pinjaman] [varchar](50) NULL, [cicilan_ke] [int] NULL, [denda] [numeric](18, 0) NULL, [jumlah_pembayaran] [numeric](18, 0) NULL, [keterangan] [text] NULL, [id_user] [varchar](50) NULL, [id_perusahaan] [varchar](50) NULL, CONSTRAINT [PK_tbl_pembayaran] PRIMARY KEY CLUSTEREDprivate void CariCicilanKe() { using (SqlConnection con = new SqlConnection(koneksi)) { using (SqlCommand cmd = new SqlCommand()) { con.Open(); cmd.Connection = con; cmd.CommandType = CommandType.Text; cmd.CommandText = "Select Count(*) from tbl_pembayaran where id_pinjaman = '" + txtid_pinjaman.Text.Trim() + "'"; Object _Object = cmd.ExecuteScalar(); Int32 value = Int32.Parse(_Object.ToString()); int hasil = value + 1; txtcicilan_ke.Text = Convert.ToString(hasil); cmd.Connection.Close(); con.Close(); } } }Tri SetiaRank #4,571 · 337 reputationPosted Jun 7, 2021, 7:34 AM EDT
Tri SetiaRank #4,571 · 337 reputationPosted Jun 13, 2021, 1:48 AM EDT