Satya
Satya

Reputation: 8881

if operator in vb.net giving error

I have a line notes = If(IsDBNull(notes),"","") in my code . When I try running it on localhost,

I am getting the error on Line 51:

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30201: Expression expected.

Source Error:



Line 49:       End Try  
Line 50:       
**Line 51:     notes = If(IsDBNull(notes),"","")** 
Line 52:       'Context.Response.Output.Write("AAAA"+notes+"MMM")
Line 53:        if notes.Trim().Equals("TIMEUP") then  


Version Information: Microsoft .NET Framework Version:2.0.50727.5466; ASP.NET Version:2.0.50727.5459 

However, when the same code is ported to production it is running without any glitch, any idea why ?

Upvotes: 2

Views: 1729

Answers (3)

Tim Schmelter
Tim Schmelter

Reputation: 460128

The problem is that the If-operator was introduced with VS 2008 but you're compiling to .NET Framework Version:2.0.

This will compile on .NET 2:

Dim notesObj As Object = IIf(IsDBNull(notes),"","")

The difference is that IIF returns an object whereas If is strongly typed(conditional operator (?) in C#), so you can write:

Dim notesStr = If(IsDBNull(notes),"","") ' a string '

Upvotes: 3

AakashM
AakashM

Reputation: 63338

VB.NET 2008 introduced the If operator, which short circuits - which is to say, only ONE out of the if-true and if-false expressions is evaluated.

Prior to this, all that was available was the IIf function, which, being a function, always evaluates both the if-true and if-false expressions.

It looks like your production box is using VB.NET 2008 or later, whereas your local machine is using VB.NET 2005 or earlier.

Upvotes: 2

Darren
Darren

Reputation: 70728

Do you mean:

IIf(IsDBNull(notes),"","")

Note the extra I.

If statements are declared as the one you see on line 53:

If <condition> Then

IIF statements are declared as:

IIf(Expression, True, False)

http://msdn.microsoft.com/en-gb/library/27ydhh0d(v=vs.71).aspx

Upvotes: 3

Related Questions