Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
Hi guys:

I want to assign value to label.text form database
but if the value is null i want to assign specific value to the label.text

in brief code like that:

C#
if (UserProfileInformation.Tables[0].Rows[0].IsNull("UserFirstName"))
        {
            UserFirstNameData.Text = "not found";
        }
        else
        {
            UserFirstNameData.Text = UserProfileInformation.Tables[0].Rows[0]["UserFirstName"].ToString();
        }


that code work good ,
but it somewhat long and i have many labels on the page ,
so i want gentle and simple code if it could possible

thanks........
Posted
Updated 19-Dec-10 8:01am
v2

1 solution

No too difficult

private static void CheckForNull(object obj, Label label, string text)
{
  string labelText = text;
  if(obj != DbNull.Value)
  {
     labelText = obj.ToString();
  }

  label.Text = labelText;  
}

CheckForNull(UserProfileInformation.Tables[0].Rows[0]["UserFirstName"],
UserFirstNameData, "not found");
 
Share this answer
 
Comments
MrLonely_2 19-Dec-10 18:10pm    
Thank you very much.......... very nice and gentle code

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900