0

Hi everyone I have a problem with the input string, I think I know the cause of this problem but I can't resolve it.

I have a database ,I'm using a request that return null ,because the table is empty. The null I return it as a double. So i want to make a condition that verify if it is null or not.

This is my method:

 public static double CREDIT()
    {
        double Total;
        try
        {
            CLSERVICES.CON.Open();
            string req = "select SUM(F.Reste)from RELATION as R , Facture as F where R.NRelation = F.Relation";
            SqlCommand cmd = new SqlCommand(req, CLSERVICES.CON);
            SqlDataReader dr;
            dr = cmd.ExecuteReader();
            if (dr.Read())
            {
                Total= double.Parse(dr.GetValue(0).ToString());
            }
            else
            {
                Total= 0;
            }
            dr.Close();
            CLSERVICES.CON.Close();
            return Total;
        }
        catch (SqlException E)
        {
            MessageBox.Show(E.Message, "<!!!>", MessageBoxButtons.OK, MessageBoxIcon.Error);
            return 0;
        }
    }
5
  • You really really need to put some using statements around your objects to ensure you don't get memory leaks Commented Oct 10, 2017 at 9:24
  • GetValue() will return object that may be null or DbNull. You need to test for these before calling ToString() and certainly before attempting a cast to double Commented Oct 10, 2017 at 9:25
  • 2
    Possible duplicate of SQL Data Reader - handling Null column values Commented Oct 10, 2017 at 9:25
  • Total= double.Parse(dr.GetValue(0).ToString()); => is this really returning numeric value? double.Parse will throw error if the parsed string is not numeric or decimal representation. Commented Oct 10, 2017 at 9:25
  • the dr.getvalue(0) is null for now , because the table is empty . Commented Oct 10, 2017 at 9:28

4 Answers 4

3

Here is a better way to write your method:

public static double CREDIT()
{
    try
    {
        string req = "select SUM(F.Reste) "+
                        "from RELATION as R "+
                        "inner join Facture as F "+
                        "ON R.NRelation = F.Relation";
        using (var con = new SqlConnection(ConnectionString))
        {
            using (var cmd = new SqlCommand(req, con))
            {
                con.Open();
                var value = cmd.ExecuteScalar();
                double total;
                if (value != null && value != DBNull.Value && double.TryParse(value.ToString(), out total))
                {
                    return total;
                }
            }
        }
    }
    catch (SqlException E)
    {
        MessageBox.Show(E.Message, "<!!!>", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    return 0;
}

Points of interest:

  • Using a local connection object, so that it can be closed and disposed as soon as possible.
  • Wrap all IDisposable instances in a using statement
  • Use ExecuteScalar() instead of ExecuteReader() since you only want to get a single scalar value
  • Use double.TryParse instead of double.Parse
  • Break sql to lines for better readability
  • Changed the implicit inner join to an explicit one
Sign up to request clarification or add additional context in comments.

Comments

0

You could:

change your double.Parse to double.TryParse (which is something you should always do when parsing a number)

and/or

test for null using https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.isdbnull(v=vs.110).aspx

This is how to use the isDBNull:

if (!dr.IsDBNull(0))
{
    Total = double.Parse(dr.GetValue(0).ToString());
}

and probably consider making your method return a double? instead of a double if it might be null

also, another thing, think about using ExecuteScalar instead of ExecuteReader

1 Comment

This would be better with an example of how to use that in the OPs code
-1

your problem comes from not checking for null in this line :

Total= double.Parse(dr.GetValue(0).ToString());

dr.GetValue(0) could be null or not even a double so you could do something like this:

var val = dr.GetValue(0);

if ( val != null )
{
    Double.TryParse(val.ToString(), out Total);
}

6 Comments

There's not point using TryParse and checking for null. Also what happens if TryParse fails here? You never check the result.
@T.Wassim I would strongly recommend that you look at this other answer it handles this in a much cleaner way and addresses many of the other issues in your code like the lack of usings.
@liam i m trying to write the code as zohar says , and for this answer i use an else to make total =0 and it solved there is no problem att my app now thanks for ur help :) :D
Ok @T.Wassim but your code suffers from memory leaks. This will cause problems.
@Liam .. how i can fix this memory leaks ? .. thiis is just a methode in a class that has a destructor
|
-1
Total = Convert.ToDouble(dr.GetValue(0));

use above line instead of Double.Parse() method and that will help you out.

2 Comments

it returns null...this will just throw an exception.
Hey, @Liam if you are using the double.parse() method and value is null then return the exception but for Convert.ToDouble () method if your value is null then always return the 0.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.