简体   繁体   中英

C# What type should I cast here?

I have a portion of code which calculates a new balance by reading from a database all prior charges and payments and then adding the new amount that's going to be charged to the balance. It was working fine with values that I'd hand planted into the database, but then I started getting errors when I wiped all the test values and started from scratch.

This is the code:

        //sum all prior charges
        try
        {
            connection.Open();
            command.Connection = connection;

            command.CommandText = "SELECT SUM(Amount) FROM Charges WHERE TransactionDate<='" + DateTime.Now + "';";
            chargesSoFar = (double)command.ExecuteScalar();
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }

        // sum all prior payments
        try
        {
            connection.Open();
            command.Connection = connection;

            command.CommandText = "SELECT SUM(Amount) FROM Payments WHERE TransactionDate<='" + DateTime.Now + "';";
            paymentsSoFar = (double)command.ExecuteScalar();
            connection.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Error: " + ex.Message);
        }

        // calculate balance
        tempCharge.Balance = (decimal)(chargesSoFar + int.Parse(textBoxAmount.Text) - paymentsSoFar);

        DataWriter.WriteCharge(tempCharge);

The errors I'm getting are type conversion errors where I cast the ExecuteScalar result. The error is happening because there are no prior charges or payments, so null is being returned, which fails when casting to double.

Is there another type I can cast to, which I can then check if it's null? If not, how do I get around this?

Any help is appreciated. Please ask any questions if I haven't been clear in what I'm trying to do here.

try nullable datatype:

double? chargesSoFar = null;
// other codes here
chargesSoFar = (double?)command.ExecuteScalar();

As a sidenote, a parameterized query is most likely the best practice to prevent sql injection. To do this,

try
{
    connection.Open();
    command.Connection = connection;

    command.CommandText = "SELECT SUM(Amount) FROM Payments WHERE TransactionDate <= @TransData;";
    command.Parameter.AddWithValue("TransData", DateTime.Now);
    chargesSoFar = (double?)command.ExecuteScalar();
    connection.Close();
}
catch (Exception ex)
{
    MessageBox.Show("Error: " + ex.Message);
}

According to this question: What is the best data type to use for money in c#?

The best data type to use is decimal.

decimal? chargesSoFar = null;
// other codes here
chargesSoFar = (decimal?)command.ExecuteScalar();

You can cast to a nullable type by using a question mark suffix, ie (double?), then you can check for null.

     paymentsSoFar = (double?)command.ExecuteScalar()
     if (paymentSoFar.HasValue){

Please also read up on SQL Injection: https://www.acunetix.com/websitesecurity/sql-injection/ as your code is currently vulnerable to this type of attack

Maybe you can cast to Nullable<double> ie double?

double? paymentsSoFar = (double?)command.ExecuteScalar();

if(paymentsSoFar.HasValue)
{
    // Then its safe to calculate 

    double myActualValue = paymentsSoFar.Value;
}

Nullable Types (C# Programming Guide)

Nullable types have the following characteristics:

  • Nullable types represent value-type variables that can be assigned the value of null . You cannot create a nullable type based on a reference type.

  • The syntax T? is shorthand for Nullable<T> , where T is a value type. The two forms are interchangeable.

  • Assign a value to a nullable type just as you would for an ordinary value type, for example int? x = 10; int? x = 10; or double? d = 4.108 double? d = 4.108 . A nullable type can also be assigned the value null: int? x = null int? x = null .

  • Use the Nullable<T>.GetValueOrDefault method to return either the assigned value, or the default value for the underlying type if the value is null, for example int j = x.GetValueOrDefault();

  • Use the HasValue and Value read-only properties to test for null and retrieve the value, as shown in the following example: if(x.HasValue) j = x.Value ;

  • The HasValue property returns true if the variable contains a value, or false if it is null .

  • The Value property returns a value if one is assigned. Otherwise, a System.InvalidOperationException is thrown.

  • The default value for HasValue is false . The Value property has no default value.

  • You can also use the == and != operators with a nullable type, as shown in the following example: if (x != null) y = x;

  • Use the ?? operator to assign a default value that will be applied when a nullable type whose current value is null is assigned to a non-nullable type, for example int? x = null; int y = x ?? -1; int? x = null; int y = x ?? -1;

  • Nested nullable types are not allowed. The following line will not compile: Nullable<Nullable<int>> n;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM