If question IF no have value (NULL),then set o to textbox SQL

  • 623 Views
  • Last Post 15 October 2018
  • Topic Is Solved
Stylus STYLUS posted this 14 October 2018

My code working fine.

Question:

If is value NULL, need to put value 0

Some help?

SELECT SUM(isnull(cast(REPLACE(TRY_CONVERT(int,TRY_CONVERT(float, iznos_bpdv),1), '#,0.00','0')AS decimal(10,2)),0.00)) as UKUPNObpdv
from mp_racun_roba
where tip_robe = 'Usluge (Generalno)' and id_fakture=105
Order By: Standard | Newest | Votes
admin posted this 15 October 2018

You can write as shown below

select isnull('your column', 0) as value from [your table]
Stylus STYLUS posted this 15 October 2018

I now that, how to implement in my code?

Stylus STYLUS posted this 15 October 2018

I try this, but when is value from sql NULL, nothing hapend, need to be 0.00 when is NULL

private void ukupno_bez_pdv_usluge()
{
SqlConnection con2 = new SqlConnection(cs);

string sqlquery = ("SELECT SUM(isnull(cast(REPLACE(TRY_CONVERT(int,TRY_CONVERT(float,iznos_bpdv),1), '#,0.00','')AS decimal(10,2)),0.00)) as UKUPNObpdv," +
" SUM(isnull(cast(REPLACE(TRY_CONVERT(float, TRY_CONVERT(float, pdv), 1), '#,0.00', '')AS decimal(10, 2)), 0.00)) as UKUPNOpdv," +
" SUM(isnull(cast(REPLACE(TRY_CONVERT(float, TRY_CONVERT(float, mp_cijena), 1), '#,0.00', '')AS decimal(10, 2)), 0.00)) as UKUPNOsapdv" +
" from mp_racun_roba" +
" where tip_robe = 'Usluge (Generalno)' and id_fakture =" + id_fakture
);

SqlCommand command = new SqlCommand(sqlquery, con2);
con2.Open();
SqlDataReader sdr = command.ExecuteReader();

if (sdr.Read())
{

usluga_bez_pdvTextBox.Text = sdr["UKUPNObpdv"].ToString();
usluga_pdvTextBox.Text = sdr["UKUPNOpdv"].ToString();
usluga_sa_pdvTextBox.Text = sdr["UKUPNOsapdv"].ToString();

}
else
{

usluga_bez_pdvTextBox.Text = "0.00";
usluga_pdvTextBox.Text = "0.00";
usluga_sa_pdvTextBox.Text = "0.00";

}
con2.Close();
}
Close