2

Anyone knows why tihs code doesn't work? Program says "Invalid use of null". In access thise code is working, but not hire. UPDATE cas SET cas.skupaj = CDbl([Odhod]-[Prihod])*24;

try
{
    OleDbConnection conn = GetConnection();
    conn.Open();

    String MyString = @"UPDATE cas SET skupaj = CDbl(Odhod-Prihod)*24 " ;  

    OleDbCommand command = new OleDbCommand(MyString, conn);
    command.ExecuteNonQuery();
    conn.Close();
    MessageBox.Show("Uspešno dodano v PB!");
}
catch (Exception ex)
{
    MessageBox.Show(ex.Message);
}
3
  • 1
    This is really confusing. The error message Invalid use of null is an Access error message. How are you getting that in C# code? When you say this works in access, that means you can run that exact SQL statement above in Access without any errors? Commented Apr 11, 2013 at 20:07
  • Might be due to NULL value for any one of column (Odhod-Prihod), AS CDate cannot handle Null values. Commented Apr 11, 2013 at 20:09
  • Code that HansUp posted it is working. In querry must be where is Odhod Not Null and Prihod Not Null. Commented Apr 11, 2013 at 20:26

1 Answer 1

5

In an Access Immediate window session, these statements throw error 94, "Invalid use of Null".

? CDbl(Null)
? CDbl(10 - Null)
? CDbl(Null - 10)
? CDbl(Null - Null)

Revise your query to make sure you don't give CDbl() Null values.

UPDATE cas
SET skupaj = CDbl(Odhod-Prihod)*24
WHERE
        Odhod Is Not Null
    AND Prihod Is Not Null

OTOH, it's not clear why you need to store that calculated value to skupaj. You could use a SELECT query to return it whenever you need, and that way you wouldn't have to run the UPDATE again whenever Odhod or Prihod values change.

SELECT CDbl(Odhod-Prihod)*24 AS skupaj
FROM cas
WHERE
        Odhod Is Not Null
    AND Prihod Is Not Null
Sign up to request clarification or add additional context in comments.

Comments

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.