Share via

Button Click System.InvalidCastException

Anonymous
2024-02-22T12:05:07.64+00:00

I encountered a

System.InvalidCastException: Unable to cast object of type 'WhereSelectListIterator`2[RateApp.Pages.Index+InQuestion,System.Decimal]' to type 'System.IConvertible'. at System.Convert.ToDecimal(Object value) at RateApp.Pages.Index.Submit()

@page "/"
@using System.Text;
@using System.Net.Http.Headers;
@using Microsoft.AspNetCore.Components;
@using Newtonsoft.Json;
@using RateApp.Models;
@exp
 
<table class="table">
    <thead>
        <tr>
           
            <th>DaysBegin</th>
            <th>DaysEnd</th>
            <th>Interest</th>
            <th>SrInterest</th>
        </tr>
    </thead>
<tbody>
@foreach (InQuestion inques in Questions)
{
    // Pass the Dto properties to the component
     <tr>
                <td>@inques.DaysBegin</td>
                <td>@inques.DaysEnd</td>
                <td>@inques.Interest</td>
                <td>@inques.SrInterest</td>
            </tr>}
</tbody>
</table>
 
 
<div class="row">
    <div class="col-md-3">
        <p>Deposit Amount</p>
    </div>
    <div class="col-md-4">
        <input placeholder="Deposit Amount" @bind="@amount" />
    </div>
</div>
<br />
<div class="row">
    <div class="col-md-3">
        <p>Months</p>
    </div>
    <div class="col-md-4">
        <input placeholder="Months" @bind="@months" />
    </div>
</div>
<div>
    <button type="submit"    @onclick="Submit" >Submit</button>
</div>
<div>
    <div class="col-md-3">
        <p>Interest</p>
    </div>
    <div>@interest</div>
</div>
  
 
@code {
    public class InQuestion
    { 
        public int No { get; set; }
        public int DaysBegin { get; set; }
        public int DaysEnd { get; set; }
        public decimal Interest { get; set; }
        public decimal SrInterest { get; set; }
    }
    public string exp;
    public string codex; public string  jsonstring ; public bool statcode;
    List<InQuestion> Questions = new List<InQuestion>();
    public int number;int months ;decimal interest; int totalquestions;     int     maxnumber;int amount;
    protected override async Task OnInitializedAsync()
    {
        string jwturl = "https://quizapijwt.azurewebsites.net/api/";
        using (var client1 = new HttpClient())
        {
            client1.BaseAddress = new Uri(jwturl);
            client1.DefaultRequestHeaders.Clear();
            client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client1.GetAsync("RateCards");
            if (response.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                jsonstring = await response.Content.ReadAsStringAsync();
                try
                {
                    Questions = JsonConvert.DeserializeObject<List<InQuestion>>(jsonstring);
                }
                catch (Exception ex)
                {   }
                // maxnumber=(from e in Questions select e.No).Max();
            }
        }
    }
    private void Submit()
    {
        try
        { 
        decimal days = months * 30;
        var queryint  = from l in Questions
                       where l.DaysBegin >= days
                          && l.DaysEnd <= days
                       select l.Interest;
        decimal finalint = Convert.ToDecimal(queryint);
        decimal amt = amount;
        decimal factor = 12;
        interest = Decimal.Multiply(amt, finalint) / factor;
        }
        catch (Exception ex)
        {
              exp=ex.ToString();
        }
    }
}
 
 
Developer technologies | .NET | Blazor
Developer technologies | .NET | .NET Multi-platform App UI

Answer accepted by question author

AgaveJoe 31,361 Reputation points
2024-02-22T13:00:11.9033333+00:00

Your code never executes the LINQ expression. Basically, the code tries convert a query to a decimal. Use the Visual Studio debugger so you can see what happening in the code. This is untested but try...

var queryint  = (from l in Questions
               where l.DaysBegin >= days
                  && l.DaysEnd <= days
               select l.Interest).FirstOrDefault();

In my opinion, you should stop using "var" for a bit and use the actual type. The "var" is hiding what your design is actually producing.

Reference documentation

Query expression basics

Was this answer helpful?

1 person found this answer helpful.
0 comments No comments

2 additional answers

Sort by: Most helpful
  1. Anonymous
    2024-02-22T13:21:15.74+00:00
    @page "/" @using System.Text; @using System.Net.Http.Headers; @using Microsoft.AspNetCore.Components; @using Newtonsoft.Json; @using RateApp.Models; @imint @exp
    <table class="table"> <thead> <tr>
            <th>DaysBegin</th>
            <th>DaysEnd</th>
            <th>Interest</th>
            <th>SrInterest</th>
        </tr>
    </thead>
    <tbody> @foreach (InQuestion inques in Questions) { // Pass the Dto properties to the component <tr> <td>@inques.DaysBegin</td> <td>@inques.DaysEnd</td> <td>@inques.Interest</td> <td>@inques.SrInterest</td> </tr>} </tbody> </table>
    <div class="row"> <div class="col-md-3"> <p>Deposit Amount</p> </div> <div class="col-md-4"> <input placeholder="Deposit Amount" @bind="@amount" /> </div> </div> <br /> <div class="row"> <div class="col-md-3"> <p>Months</p> </div> <div class="col-md-4"> <input placeholder="Months" @bind="@months" /> </div> </div> <div> <button type="submit"    @onclick="Submit" >Submit</button> </div> <div> <div class="col-md-3"> <p>Interest</p> </div> <div>@interest</div> </div>
    @code { public class InQuestion { public int No { get; set; } public int DaysBegin { get; set; } public int DaysEnd { get; set; } public decimal Interest { get; set; } public decimal SrInterest { get; set; } }
    public string exp;
    public string codex; public string  jsonstring ; public bool statcode;
    List<InQuestion> Questions = new List<InQuestion>();
    public int number;int months ;decimal interest; int totalquestions;     int     maxnumber;int amount; decimal imint;
    protected override async Task OnInitializedAsync()
    {
        string jwturl = "https://quizapijwt.azurewebsites.net/api/";
        using (var client1 = new HttpClient())
        {
            client1.BaseAddress = new Uri(jwturl);
            client1.DefaultRequestHeaders.Clear();
            client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
            HttpResponseMessage response = await client1.GetAsync("RateCards");
            if (response.IsSuccessStatusCode)
            {
                //Storing the response details recieved from web api
                jsonstring = await response.Content.ReadAsStringAsync();
                try
                {
                    Questions = JsonConvert.DeserializeObject<List<InQuestion>>(jsonstring);
                }
                catch (Exception ex)
                {   }
                // maxnumber=(from e in Questions select e.No).Max();
            }
        }
    }
    private void Submit()
    {
        try
        { 
             
       int  days = (months * 30);
      
    var    imint  =  (from l in Questions
                       where l.DaysBegin <= days
                          && l.DaysEnd>= days
                       select l.Interest).FirstOrDefault() ; 
                      
      
        decimal amt = amount;
      
       decimal  finint= Decimal.Multiply(amt, imint) /1200 ;   
        interest=Decimal.Multiply( finint,months);
        
        }
        catch (Exception ex)
        {
              exp=ex.ToString();
        }
    }
    }
    

    Was this answer helpful?

    0 comments No comments

  2. Anonymous
    2024-02-22T12:52:51.2233333+00:00
    @page "/"
    @using System.Text;
    @using System.Net.Http.Headers;
    @using Microsoft.AspNetCore.Components;
    @using Newtonsoft.Json;
    @using RateApp.Models;
    @exp
     
     
    <table class="table">
        <thead>
            <tr>
               
                <th>DaysBegin</th>
                <th>DaysEnd</th>
                <th>Interest</th>
                <th>SrInterest</th>
            </tr>
        </thead>
    <tbody>
    @foreach (InQuestion inques in Questions)
    {
        // Pass the Dto properties to the component
         <tr>
                    <td>@inques.DaysBegin</td>
                    <td>@inques.DaysEnd</td>
                    <td>@inques.Interest</td>
                    <td>@inques.SrInterest</td>
                </tr>}
    </tbody>
    </table>
     
     
    <div class="row">
        <div class="col-md-3">
            <p>Deposit Amount</p>
        </div>
        <div class="col-md-4">
            <input placeholder="Deposit Amount" @bind="@amount" />
        </div>
    </div>
    <br />
    <div class="row">
        <div class="col-md-3">
            <p>Months</p>
        </div>
        <div class="col-md-4">
            <input placeholder="Months" @bind="@months" />
        </div>
    </div>
    <div>
        <button type="submit"    @onclick="Submit" >Submit</button>
    </div>
    <div>
        <div class="col-md-3">
            <p>Interest</p>
        </div>
        <div>@interest</div>
    </div>
      
     
    @code {
        public class InQuestion
        { 
            public int No { get; set; }
            public int DaysBegin { get; set; }
            public int DaysEnd { get; set; }
            public decimal Interest { get; set; }
            public decimal SrInterest { get; set; }
        }
     
        public string exp;
        public string codex; public string  jsonstring ; public bool statcode;
        List<InQuestion> Questions = new List<InQuestion>();
        public int number;int months ;decimal interest; int totalquestions;     int     maxnumber;int amount;
        protected override async Task OnInitializedAsync()
        {
            string jwturl = "https://quizapijwt.azurewebsites.net/api/";
            using (var client1 = new HttpClient())
            {
                client1.BaseAddress = new Uri(jwturl);
                client1.DefaultRequestHeaders.Clear();
                client1.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                HttpResponseMessage response = await client1.GetAsync("RateCards");
                if (response.IsSuccessStatusCode)
                {
                    //Storing the response details recieved from web api
                    jsonstring = await response.Content.ReadAsStringAsync();
                    try
                    {
                        Questions = JsonConvert.DeserializeObject<List<InQuestion>>(jsonstring);
                    }
                    catch (Exception ex)
                    {   }
                    // maxnumber=(from e in Questions select e.No).Max();
                }
            }
        }
        private void Submit()
        {
            try
            { 
            decimal days = months * 30;
            var queryint  = from l in Questions
                           where l.DaysBegin <= days
                              && l.DaysEnd >= days
                           select l.Interest;
            decimal finalint = Convert.ToDecimal(queryint);
            decimal amt = amount;
            decimal factor = 12;
            interest = Decimal.Multiply(amt, finalint) / factor;
            }
            catch (Exception ex)
            {
                  exp=ex.ToString();
            }
        }
    }
     
     
    
    
    

    I changed the code and I am getting System.InvalidCastException

    Was this answer helpful?


Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.