Reputation: 351
I have an application which has been migrated from VB to VB.NET. In VB code Trim function has been used on the values from database,in lots of places.As VB Trim can propagate NULL no error is thrown. Now when the code is migrated to .NET same Trim functions are are throwing errors as it doesn't support NULL propagation in .NET.How I can address this issue on changing my code in a single place? It will be a tedious job if I have to use Convert.ToString for all places. Any out of the box suggession? Databse is DB2
Sample:
VB:
res = Trim(rs("Name").Value)
num = Len(rs("Name").Value)
Converted VB.NET Code
rs("Name").Trim()
rs("Name").Length
Upvotes: 1
Views: 340
Reputation: 590
Create a module and create a public function ad follows:
Public function IfNull(ByVal oObj as Object, byval oRetVal as Object) as Object
If oObj = Nothing orelse oObj = dbvalue.null then
Return oRetval
End if
Try
Return oObj.ToString.Trim()
Catch ex as exception
End try
End function
I am with mobile,when I have my laptop I will try to make it better
You know what you need but for me to cast everything to String is not the best practice...
Upvotes: 0
Reputation: 39777
DBNull.ToString
returns an empty string. So do a Global Find/Replace for
.Trim(
to
.ToString().Trim(
Upvotes: 4