SQL Server How to check data from two different database for data validation

xqk2d5yq  于 2023-05-21  发布在  其他
关注(0)|答案(1)|浏览(185)

I am currently working on a parking application in Visual Studio using C# language. In the payment form, there are 2 textboxes named tbMembership (which holds the membership ID) and tbRate (which holds the parking fee based on membership and vehicle type), and 1 combobox named cbType (which holds the vehicle type ID).

I have an SQL database for the Payment form called ParkingData, which contains the following fields: id, license_plate, vehicle_id, employee_id, hourly_rates_id, datetime_in, datetime_out, amount_to_pay.

Based on the client's request, the parking fee per hour will vary depending on whether the vehicle belongs to a member or not. Additionally, the calculation is based on the rounded number of hours the vehicle is parked.

I tried to make code like this but it doesn't work

void tarif()
{
    var harga = Global.db.HourlyRates.FirstOrDefault(x => x.value == Convert.ToDecimal(tbRate.Text));
    harga.vehicle_type_id = Convert.ToInt32(cbType.SelectedValue);
    harga.Membership.name = tbOwner.Text;

    if (harga != null)
    {
        tbRate.Text = harga.value.ToString();
    }
}

There is an error code in the first line of that code ie recognize the method 'System.Decimal ToDecimal(System.String)' method, and this method cannot be translated into a store expression.'

void tarif()
    //in the first line
    var harga = Global.db.HourlyRates.FirstOrDefault(x => x.value == Convert.ToDecimal(tbRate.Text));
    harga.vehicle_type_id = Convert.ToInt32(cbType.SelectedValue);
    harga.Membership.name = tbOwner.Text;

    if (harga != null)
    {
        tbRate.Text = harga.value.ToString();
    }
}
xxe27gdn

xxe27gdn1#

Convert.ToDecimal, cannot be translated in this way.

To resolve this issue, you can try retrieving the values separately

and performing the conversion in C# rather than within the LINQ query. Here's an updated version of your code:

void tarif()
{
    decimal rate = Convert.ToDecimal(tbRate.Text); // Retrieve and convert the rate separately
    int vehicleTypeId = Convert.ToInt32(cbType.SelectedValue);
    string ownerName = tbOwner.Text;

    var harga = Global.db.HourlyRates.FirstOrDefault(x => x.value == rate && x.vehicle_type_id == vehicleTypeId);
    
    if (harga != null)
    {
        tbRate.Text = harga.value.ToString();
    }
}

相关问题