I'm very new to programming and was wondering why I am receiving a warning.
"Function Checkbox doesnt return a value on all code paths. A null reference exception could occur at runtime when the result is used"


I then want to execute this function through button.click event: CheckBox.ExecuteNonQuery()

Function CheckBox() As SqlCommand
        Dim ConnString As String = "Data Source=08.4.4.56;Initial Catalog=" + txtPortal.Text + ";Persist Security Info=True;User ID=USER1;Password=" + TextBox1.Text
        Using Con As New SqlConnection(ConnString)
            Con.Open()

            If cb18g.Checked Then
                sql = "UPDATE ItemType SET IsActive = 1 WHERE ItemTypeCode = @ItemTypeCode"
                Dim cmd As New SqlCommand(sql, Con)
                cmd.Parameters.Add("@ItemTypeCode", SqlDbType.VarChar, 3)
                cmd.Parameters("@ItemTypeCode").Value = "18G"
                cmd.ExecuteNonQuery()
            ElseIf cb35g.Checked Then
                sql = "UPDATE ItemType SET IsActive = 1 WHERE ItemTypeCode = @ItemTypeCode"
                Dim cmd As New SqlCommand(sql, Con)
                cmd.Parameters.Add("@ItemTypeCode", SqlDbType.VarChar, 3)
                cmd.Parameters("@ItemTypeCode").Value = "35G"
                cmd.ExecuteNonQuery()
            Else
                MsgBox("DEFUALT ITEM ACTIVE")
            End If

        End Using

    End Function

You have declared this as a function that returns a SqlCommand. No where in the code do you actually do this, so the compiler is complaining that it is possible to run this code and not get a SqlCommand back.

I'm not sure if you actually want to return anything. If you don't, make it a subroutine instead of a function.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.