who-aditya-nawandar
who-aditya-nawandar

Reputation: 1342

vb.net to c# conversion for IsDBNull not working

The following conversion doesn't work.

Error:"Only assignment, call, increment, decrement & new object can be used as a statement"

VB.net
objUser.Email = IIf(IsDBNull(drow("Email")), "", drow("Email"))
C#
objUser.Email == (Information.IsDBNull(drow("Email")) ? "" : drow("Email"));

I need it in C#. Any ideas??

Upvotes: 0

Views: 2695

Answers (4)

Nikhil Agrawal
Nikhil Agrawal

Reputation: 48550

In C# = is asignment operator and == is comparison operator

Remove == and replace with =.

Assuming that drow is DataRow

objUser.Email = (drow.IsNull("Email") ? String.Empty : drow["Email"].ToString());

?: is ternary operator which always returns a value. In your case that value is being assigned to objUser.Email.

Upvotes: 3

Jens Kloster
Jens Kloster

Reputation: 11287

Try this:

objUser.Email = (DBNull.Value == drow("Email")) ? "" : drow("Email"));

See the Documentation for DbNull - where you will find examples:

From MSDN

private string AddFieldValue(string label, DataRow row, 
                             string fieldName) 
{                                
   if (! DBNull.Value.Equals(row[fieldName])) 
      return (string) row[fieldName] + " ";
   else
      return String.Empty;
}

Upvotes: 1

viclim
viclim

Reputation: 959

You're using equal operator instead of assignment operator in the C# variant. Change the == to = since what you want is assignment.

objUser.Email = (Information.IsDBNull(drow("Email")) ? "" : drow("Email"));

Upvotes: 1

Parimal Raj
Parimal Raj

Reputation: 20585

You have accidentally, used comparison operator instead of assignment operator.

objUser.Email == (Information.IsDBNull(drow("Email")) ? "" : drow("Email"));

should be, as you are not doing comparision, its an assignment.

objUser.Email = (Information.IsDBNull(drow("Email")) ? "" : drow("Email"));

Upvotes: 1

Related Questions