I have the following code in ASP.NET but when run the code it returns me 0 value in label lblassest.Text. can anyone tell me where is the problem?
C# Code :
if (reader.Read())
{
decimal price;
decimal.TryParse(lblcurrentbtc.Text, out price);
decimal pr_charges = reader["WallBalan"] ==
DBNull.Value ? 0 : Convert.ToDecimal(reader["WallBalan"]);
lblamount.Text = pr_charges.ToString();
lblassest.Text = ((Convert.ToDecimal(reader["WallBalan"]) * price)).ToString();
reader.Close();
}
ASP (Frontend) :
<asp:Label class="text-dark float-right font-weight-medium" id="lblcurrentbtc" runat="server"></asp:Label>
<asp:Label class="text-dark mb-1 font-weight-medium" runat="server" ID="lblassest"></asp:Label>
Javascript :
var baseUrl = "https://api.coinranking.com/v2/coins"
var proxyUrl = "https://cors-anywhere.herokuapp.com/"
var apiKey = "coinranking"
fetch(`${proxyUrl}${baseUrl}`, {
method: "GET",
headers: {
'Content-Type': 'application/json',
'x-access-token': `${apiKey}`,
'Access-Control-Allow-Origin': '*'
}
}).then((response) => {
if (response.ok) {
response.json().then((json) => {
console.log(json.data.coins)
let coinsData = json.data.coins
if (coinsData.lenght > 0) {
var cryptoCoins = "";
}
coinsData.forEach((coin) => {
cryptoCoins += "<tr>"
cryptoCoins += `<td> ${coin.uuid} </td>`;
cryptoCoins += `<td> ${coin.btcPrice} </td>`;
cryptoCoins += `<td> ${coin.rank} </td>`;
cryptoCoins += `<td> ${coin.tier} </td>`;
cryptoCoins += `<td> ${coin.name} </td>`;
cryptoCoins += `<td> ${coin.price} </td>`;
cryptoCoins += `<td> ${coin.symbol} </td>`; "<tr>";
})
document.getElementById("data").innerHTML = cryptoCoins
document.getElementById('lblcurrentbtc').innerHTML = "";
var myTab = document.getElementById('table1');
var objCells = myTab.rows.item(1).cells;
lblcurrentbtc.innerHTML = lblcurrentbtc.innerHTML + ' ' +
objCells.item(5).innerHTML;
})
}
}).catch((error) => {
console.log(error)
})
Please note that the lblcurrentbtc.Text have value like 54897.56062971018 .
Thx for your helps
2
Answers
You are using
decimal.TryParse()
correctly. If the value of price is0
after calling it then either the contents of yourllblcurrentbtc.Text
is0
, something that isn’t a number or null/empty string.It’s also possible that the
TryParse
line is never reached.Try setting a breakpoint in your if statement to verify the string value (
lblcurrentbtc.Text
) is as you expect. Yourlblcurrentbtc.Text
may be null or empty string at the point this code runs – the code that assigns a value may happen later.If I write this code:
Then I run your code verbatim after it:
The value of
lblamount.Text
&lblassest.Text
are2
&109795.12125942036
, respectively.Your code is working fine as it is. Your issue is that
lblcurrentbtc.Text
doesn’t contain the value that you think it does.