Check for null on gridview template column

How can I check for NULL in the following templatefield of a gridview?  I am using a dynamic search and if nothing is returned I get error.

Here is the code

                <asp:TemplateField HeaderText="Username">
                                <ItemTemplate>
                    <asp:HyperLink ID="hlUrl" runat="server" NavigateUrl='<%# FormatUrl( (String) Eval("UserName") ) %>'
                        Text='<%# Eval("Username") %>' />                    
                    </a>
                </ItemTemplate>
mugseyAsked:
Who is Participating?
 
nisarkhanCommented:
try:

  if (!(Convert.IsDBNull(DataBinder.Eval(e.Row.DataItem, "UserName"))))
  {
    string _username = DataBinder.Eval(e.Row.DataItem, "UserName").ToString();
   }

0
 
mugseyAuthor Commented:
Oops here is the error

Unable to cast object of type 'System.DBNull' to type 'System.String'.
0
 
Bob LearnedCommented:
Try this instead:

     FormatUrl( Eval("UserName").ToString() )

Bob
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.