3

I am just wondering if someone could help me with the following. I am using VB. NET and SQL Server and want to include some error handling in my code.

I am doing a SQL SUM command which returns fine with no problems providing there is a match in the database. But when there is no match to the String I have specified I get a "Conversion from type 'DBNull' to type 'Integer' is not valid."

I understand this error occurs because I have no value in the database to which I have specified.

Below is the code that I am using which contains no error handling:

Dim dbCount1 As Integer
    SQLConnectLog()
    strSQLog = "SELECT SUM ([TotalTime]) FROM [Line1Log] WHERE ([State] = 'Test')"
    dbCommandLog = New SqlCommand(strSQLog, dbConnectionLog)
    dbCount1 = dbCommandLog.ExecuteScalar()
    SQLDisconnectLog()
    lblTest.Text = dbCount1

Now I have tried several different ways to use isDBNull but to no avail, such as:

Dim dbCount1 As Integer
    SQLConnectLog()
    strSQLog = "SELECT SUM ([TotalTime]) FROM [Line1Log] WHERE ([State] = 'Test')"
    dbCommandLog = New SqlCommand(strSQLog, dbConnectionLog)
    If IsDBNull(dbCount1 = dbCommandLog.ExecuteScalar()) Then
        SQLDisconnectLog()
        lblTest.Text = dbCount1
    Else
        lblTest.Text = "0"
    End If

The method I used above still does not work.

I am sure I need to use isDBNull function but no sure where to place it.

If anyone could give me some insight on how this can be done I would greatly appreciate it.

Thanks in advance :)

5 Answers 5

4

A pure SQL solution can rely in coalesce or isnull for this simple query just returning a single aggregate column.

SELECT COALESCE(SUM ([TotalTime]), 0) FROM [Line1Log] WHERE ([State] = 'Test')
Sign up to request clarification or add additional context in comments.

1 Comment

Hi there, thank you for this response, this is my chosen answer as it works perfectly, exactly what I wanted. Thanks again.
3

Try to change dbCount1 to nullable integer

Dim dbCount1 As Nullable(Of Integer)

Or

Dim dbCount1 As Integer?

1 Comment

Thank you for your response, it helped me out a great deal
1

Define dbCount1 as a Nullable integer and call dbCount.HasValue to check whether the value is a Null or a valid integer:

....
Dim dbCount1 as Integer? = dbCommandLog.ExecuteScalar()
if (dbCount1.HasValue) Then
    lblTest.Text = dbCount1
...

An even better option is to use the If operator, to return a default value if the first argument is null:

Dim dbCount1 as Integer? = dbCommandLog.ExecuteScalar()
lblTest.Text=If(dbCount1,0)

7 Comments

Thank you so much for your reply. I have tried the two examples that you have given me and the method makes a lot of sense thank you. However when using the examples above I get a "Specified cast is not valid." error.
With Integer, Nothing will set the value as = 0. For example: Dim dbCount1 = If(Not IsDBNull(row.Item(3)), CInt(row.Item(3)), Nothing)
@JoshYates1980 no, the implicit conversion from Null (that's what Nothing is) to int will - mixing up actual 0s with nulls. Such implicit conversions are undesirable as they lead to undetectable bugs
My comment was related to the question, but untested. My code that was tested: Public Class Person with Public Property Age As Integer? This was set to 0: Person.Age = If(Not IsDBNull(row.Item(3)), CInt(row.Item(3)), Nothing)
@JoshYates1980 that's worse. Integer? means the property should accept Nulls. Your code replaces them with 0 instead. That's a bug.
|
1

try below if condition instead of yours,

If(dbCommandLog.ExecuteScalar() = DBNull.Value, False, True)

Comments

0
dbCount1 =CInt(dbCommandLog.ExecuteScalar())

Try this Code

1 Comment

Hi there, thank you for your response however the above code gives me the same error.

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.