Results 1 to 10 of 10

Thread: VB6 to .NET

  1. #1
    Lounger
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I hope this is clear and not too long. I am converting VB6 code to .NET. I am getting an error that I am not sure I understand and not sure how to handle it.

    There is a section of code that runs through a table resultset and does some checking on the values there. Based on those checks, it does one thing or the other.

    After converting I ran across the following Upgrade Warning and resulting code:

    'UPGRADE_WARNING: Use of Null/IsNull() detected.
    If (IsDBNull(datSPRS.Fields(12).Value) Or datSPRS.Fields(12).Value = " ") And (datSPRS.Fields(9).Value <> 0 And datSPRS.Fields(11).Value <> 0) And datSPRS.Fields(11).Value < 1000 And datSPRS.Fields(7).Value < sTrkStrDt Then
    DO THIS
    ELSE
    DO THIS

    The ISDBNULL conversion works great. The new problem introduced is now when I run the code I get the error:
    Operator '<>' is not defined for type 'DBNull' and type 'Integer'.

    This is resulting from datSPRS.Fields(9).Value. And likewise any operator such as "<", ">" produces the same error.

    Now datSPRS.Fields(9).Value in my resultset comes from a database table that has it listed as a Float and allows Nulls. The first data point where this is failing has a Null. The best I could figure from research is that datSPRS.Fields(9).Value is an INT and can contain Nulls, but the Integer Type in the .NET Framework is not nullable. So what it is trying to do is now impossible in its current form.

    I need to evaluate the datSPRS.Fields(9).Value for zero to run the first code chunk, otherwise run the ELSE. The same rule would have to apply for NULL. I almost want Null to default to zero.

    I think I need to create a Nullable type and, if that is true, I am looking for guidance on how to implement that for this scenario.

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    I don't have or use .Net so I can't test the following.

    You could introduce variables like this:

    Code:
    Dim lngField9 As Long ' use the appropriate type here
    If IsDBNull(datSPRS.Fields(9).Value) Then
      lngField9 = 0
    Else
      lngField9 = datSPRS.Fields(9).Value
    End If
    You can now use lngField9 instead of datSPRS.Fields(9).Value in the rest of the code without having to worry about Null values.

  3. #3
    Plutonium Lounger
    Join Date
    Dec 2000
    Location
    Sacramento, California, USA
    Posts
    16,775
    Thanks
    0
    Thanked 1 Time in 1 Post
    Quote Originally Posted by newb View Post
    I hope this is clear and not too long. I am converting VB6 code to .NET. I am getting an error that I am not sure I understand and not sure how to handle it.

    There is a section of code that runs through a table resultset and does some checking on the values there. Based on those checks, it does one thing or the other.

    After converting I ran across the following Upgrade Warning and resulting code:

    'UPGRADE_WARNING: Use of Null/IsNull() detected.
    If (IsDBNull(datSPRS.Fields(12).Value) Or datSPRS.Fields(12).Value = " ") And (datSPRS.Fields(9).Value <> 0 And datSPRS.Fields(11).Value <> 0) And datSPRS.Fields(11).Value < 1000 And datSPRS.Fields(7).Value < sTrkStrDt Then
    DO THIS
    ELSE
    DO THIS

    The ISDBNULL conversion works great. The new problem introduced is now when I run the code I get the error:
    Operator '<>' is not defined for type 'DBNull' and type 'Integer'.

    This is resulting from datSPRS.Fields(9).Value. And likewise any operator such as "<", ">" produces the same error.

    Now datSPRS.Fields(9).Value in my resultset comes from a database table that has it listed as a Float and allows Nulls. The first data point where this is failing has a Null. The best I could figure from research is that datSPRS.Fields(9).Value is an INT and can contain Nulls, but the Integer Type in the .NET Framework is not nullable. So what it is trying to do is now impossible in its current form.

    I need to evaluate the datSPRS.Fields(9).Value for zero to run the first code chunk, otherwise run the ELSE. The same rule would have to apply for NULL. I almost want Null to default to zero.

    I think I need to create a Nullable type and, if that is true, I am looking for guidance on how to implement that for this scenario.
    I'm not sure what you're trying to do, but you can't compare a null to anything, which is why the <> operator won't work. What you must do is check for a null value in the field and if it is NOT null, compare it to the integer, and you have to test each field before you use it. Trying to compare a null to a value is always going to throw an exception. You should be able to use an Nz function much as in VB to substitute a zero for a null value.
    Charlotte

  4. #4
    Lounger
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Hans, this worked great thanks! I had trouble getting back in to say thanks. I also created a re-usable sub to execute this.

  5. #5
    Lounger
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    I don't have or use .Net so I can't test the following.

    You could introduce variables like this:

    Code:
    Dim lngField9 As Long ' use the appropriate type here
    If IsDBNull(datSPRS.Fields(9).Value) Then
      lngField9 = 0
    Else
      lngField9 = datSPRS.Fields(9).Value
    End If
    You can now use lngField9 instead of datSPRS.Fields(9).Value in the rest of the code without having to worry about Null values.
    Hans,

    You wont believe this but after all my changes, my hard drive crashed and I lost everything about the conversion. I think I have everything back the way it was including a new Function to run various fileds through (I am tetsting one field, field11, below):

    Main module:
    If (NullInt(datSPRS.Fields(11).Value) <> 0) Then

    Other Module:
    Public newField As Object

    Public Function NullInt(ByRef strvalTest As Object) As Object
    If IsDBNull(strvalTest) Then
    newField = 0
    Else
    newField = strvalTest
    End If

    Return newField

    End Function

    However now I am getting this new message I have never seen before. I just wanted to make sure it was not code related. Any thoughts? I think I have everything back the way it was. Error: Current Recordset does not support updating. This may be a limitation of the provider, or of the selected locktype.

    I did upgrade my SQL Programmer to the latest release...

    Sorry- last thing. I get back data in the Field and when it tries to go off thi sline after the Function is called I get that error. So I am not even updating a recordset, just checking it.

  6. #6
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    As I wrote, I don't have .NET so I can't really help. Is datSPRS a control? If so, check its data properties. Or is it a recordset? If so, check the way you open it.

    I hope that someone who knows .NET will have more relevant suggestions.


  7. #7
    Lounger
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    As I wrote, I don't have .NET so I can't really help. Is datSPRS a control? If so, check its data properties. Or is it a recordset? If so, check the way you open it.

    I hope that someone who knows .NET will have more relevant suggestions.
    Thanks- I know I am making a mess of this. Here is what I have implemented based on your suggestion:

    Do Until eof
    Dim lngField9 As Long
    If (IsDBNull(datSPRS.Fields(9).Value)) Then
    lngField9 = 0
    Else
    lngField9 = datSPRS.Fields(9).Value
    End If
    If lngField9 <> 0 Then

    Do Other Stuff

    What I had done was take your peice and extract it to its own function so that I could pass in different fields. That is the part I had working, lost, and now fail at. Something is very different and I cannot figure out what it was.

    I changed your code to
    If (NullInt(datSPRS.Fields(9).Value)) <> 0 Then

    and called the Function

    Public Function NullInt(ByRef strvalTest As Object) As Object
    If IsDBNull(strvalTest) Then
    strvalTest = 0
    Else
    strvalTest = strvalTest
    End If

    Return strvalTest

    End Function

    I cannot figure out what I have messed up here even though I know it looks different than what I had. Now I get that weird error.

  8. #8
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts
    Code:
    Public Function NullInt(fld As Object) As Double
      Dim dblRetVal As Double
      If IsDBNull(fld.Value) Then
    	dblRetVal = 0
      Else
        dblRetVal = CDbl(fld.Value)
      End If
      Return dblRetVal
    End Function
    Use like this:

    If NullInt(datSPRS.Fields(9)) <> 0 Then

  9. #9
    Lounger
    Join Date
    Apr 2008
    Posts
    41
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by HansV View Post
    Code:
    Public Function NullInt(fld As Object) As Double
      Dim dblRetVal As Double
      If IsDBNull(fld.Value) Then
    	dblRetVal = 0
      Else
        dblRetVal = CDbl(fld.Value
      End If
      Return dblRetVal
    End Function
    Use like this:

    If NullInt(datSPRS.Fields(9)) <> 0 Then
    Thats IT!!!!!! Im sending you a big fat kiss. Thank You!

  10. #10
    New Lounger
    Join Date
    Apr 2007
    Location
    Salem, Oregon, USA
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It looks like you're way past this, but the update error you were getting was likely the result of passing the paramenter ByRef instead of ByVal:

    Public Function NullInt(ByRef strvalTest As Object) As Object
    If IsDBNull(strvalTest) Then
    strvalTest = 0
    Else
    strvalTest = strvalTest
    End If

    Return strvalTest

    End Function

    Whether strvalTest passes the IsDBNull test or not, this code will attempt to update the original value (to either 0 or whatever it was in the first place) because you passed a reference to that value rather than the value itself. I'm a bit confused, though, because in your original code referring to this update error you wrote the function slightly differently, using newField instead of changing the value of strvalTest directly:

    Public Function NullInt(ByRef strvalTest As Object) As Object
    If IsDBNull(strvalTest) Then
    newField = 0
    Else
    newField = strvalTest
    End If

    Return newField

    End Function


    The way I read it, that should have worked, unless you were attempting to assign the return value of NullInt to strvalTest in some other module.

    While you probably no longer care, my point is that using ByRef always entails the risk of modifying the original, while using ByVal requires a separate, explicit step to do so.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •