Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

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
0
speedbeeqs
Asked:
speedbeeqs
  • 3
  • 2
  • 2
  • +1
1 Solution
 
TheAvengerCommented:
if (container.DataItem["IUSER_LOCKED"].GetType().ToString() == "System.DBNull")
            oCheckBox.Checked = false;
else
            oCheckBox.Checked = (bool)container.DataItem["IUSER_LOCKED"];
0
 
Chester_M_RagelCommented:
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"]);
}
0
 
TheAvengerCommented:
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"];
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Chester_M_RagelCommented:
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...
0
 
mbprogrammerCommented:
this might be better :
  oCheckBox.Checked = (container.DataItem["IUSER_LOCKED"] != System.DBNull.Value && 
                                    Convert.ToBoolean(container.DataItem["IUSER_LOCKED"]));
0
 
speedbeeqsAuthor Commented:
to Chester_M_Ragel and TheAvenger:
after compile :
Cannot apply indexing with [] to an expression of type 'object'
0
 
Chester_M_RagelCommented:
What is actually container.DataItem is? Cast it to that one...

eg: If its a DataRowView,

if (((DataRowView)container.DataItem)["IUSER_LOCKED"] == System.DBNull.Value)
{
   oCheckBox.Checked = false;
}
else
{
   oCheckBox.Checked = Convert.ToBoolean(((DataRowView)container.DataItem)["IUSER_LOCKED"]);
}
0
 
speedbeeqsAuthor Commented:
ok, great!! Thanks Chester_M_Ragel!
thanks all experts for you helps!
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now