Link to home
Start Free TrialLog in
Avatar of speedbeeqs
speedbeeqs

asked on

Convert to C#! Urgent! Help me!

Please help me! Convert this code into C#!!!

Here:  
        If container.DataItem("IUSER_LOCKED").GetType.ToString = "System.DBNull" Then
            oCheckBox.Checked = False
        Else
            oCheckBox.Checked = CBool(container.DataItem("IUSER_LOCKED"))
        End If
Avatar of TheAvenger
TheAvenger
Flag of Switzerland image

if (container.DataItem["IUSER_LOCKED"].GetType().ToString() == "System.DBNull")
            oCheckBox.Checked = false;
else
            oCheckBox.Checked = (bool)container.DataItem["IUSER_LOCKED"];
Avatar of Chester_M_Ragel
Chester_M_Ragel

I think this may be better than that,

if (container.DataItem["IUSER_LOCKED"] == System.DBNull.Value)
{
        oCheckBox.Checked = false;
}
else
{
       oCheckBox.Checked = Convert.ToBoolean(container.DataItem["IUSER_LOCKED"]);
}
Well, the best and simplest would actually be:

if (container.DataItem["IUSER_LOCKED"] != DBNull.Value)
            oCheckBox.Checked = false;
else
            oCheckBox.Checked = (bool)container.DataItem["IUSER_LOCKED"];
If its a data from database, it depends on the database... If you try to cast like that, some time it may throw an exception... Better to go for Convert.ToBoolean.. I think its better to always use Convert.ToXXX than anything else...
this might be better :
  oCheckBox.Checked = (container.DataItem["IUSER_LOCKED"] != System.DBNull.Value && 
                                    Convert.ToBoolean(container.DataItem["IUSER_LOCKED"]));
Avatar of speedbeeqs

ASKER

to Chester_M_Ragel and TheAvenger:
after compile :
Cannot apply indexing with [] to an expression of type 'object'
ASKER CERTIFIED SOLUTION
Avatar of Chester_M_Ragel
Chester_M_Ragel

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ok, great!! Thanks Chester_M_Ragel!
thanks all experts for you helps!