Tri Setia

Tri Setia

  • 1.3k
  • 464
  • 24.4k

How to sum number from database using DataReader in ASP.NET Web Form

Jun 7 2021 3:29 AM

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

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();
        }
    }
}

 


Answers (7)