1

I get the Conversion from type 'DBNull' to type 'Integer' is not valid." error on the line "Dim avgObject As string = Cstr(avgCom.ExecuteScalar())

The command works when the where module_ID='" & moduleSelect & "' statement is removed and I do not know how to fix this, can anyone help?

    Dim moduleSelect As String = moduleRadio.SelectedValue
    Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
        Using avgCom As New SqlCommand("SELECT AVG(exam) FROM completed_module where module_ID='" & moduleSelect & "' ", _
                                        connection)
            connection.Open()
            Dim avgObject As Integer = CInt(avgCom.ExecuteScalar())
            Dim averageVar As String
            averageVar = avgObject.ToString
            avgLabel.Text = averageVar
        End Using
3
  • A google search for Conversion from type 'DBNull' to type 'Integer' is not valid. has nearly 8000 results. Commented Apr 8, 2013 at 14:47
  • Can you run the query SELECT AVG(exam).. where Module_ID = nnn? I think, it is returning null because there aren't records for the module_ID you passed in. Commented Apr 8, 2013 at 14:49
  • There were no values selected from the list when the page loaded thus giving the null, thanks everyone for their help. Commented Apr 8, 2013 at 15:28

5 Answers 5

2

I believe you are looking for something like this, first checking if it is dbnull:

Dim moduleSelect As String = moduleRadio.SelectedValue

Using connection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True")
    Using avgCom As New SqlCommand("SELECT AVG(exam) FROM completed_module where module_ID='" & moduleSelect & "' ", _
                                    connection)
        connection.Open()
        Dim result = avgCom.ExecuteScalar()
        If IsDBNull(result) then return
        Dim avgObject As Integer = CInt(result)
        Dim averageVar As String
        averageVar = avgObject.ToString
        avgLabel.Text = averageVar
    End Using
Sign up to request clarification or add additional context in comments.

Comments

2

DBNull means that the record in the database does not contain a value for the column. So basically you are trying to convert "nothing" into a number.

What do you want your program to do? Skip the row? Use a default value instead?

If the command really "works" if you remove a statement from the command, I suggest you simply remove it.

Comments

0

Use Convert.ToString instead. Directcast as string does not work for Null/Nothing

UPDATE Problem happens whenever you do not receive any results.

I tested, so CStr to Convert.ToString works for DBNUll, but CInt and Convert.ToIntXX still throws an eception. You can use

Dim scalarResult = avgCom.ExecuteScalar()
If Convert.IsDBNull(scalarResult) then
  avgObject = 0
Else
  avgObject = Convert.toInt32(scalarResult)
End If

Comments

0

Error :Conversion from type 'DBNull' to type 'Integer' is not valid.

This error Occurs because your query return a NULL value.. you can manage the NULL value by using the Below code..

Try like below it will help you...

connection.Open()
Dim result As String = avgCom.ExecuteScalar().ToString()
Dim avgObject As Integer = If(result = "", 0, CInt(result))

Comments

0

Probably this fails because there is a value missing. (i.e. NULL) But it might work if you default to 0 if a row with NULL was encountered:

SELECT AVG(ISNULL(exam,0)) FROM completed_module where module_ID=

Otherwise make sure your table does not include NULL-values for that column:

UPDATE completed_module SET exam = 0 WHERE exam IS NULL

(maybe constraint it so it may not have future NULL-Values also ;))

EDIT: this assumes that you can actually have an average value for every row, even those where the column you access is NULL, in that case i would assume NULL does not add anything to your average value (which the other rows that share that ID might) so I default it to 0

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.