• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1484
  • Last Modified:

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>
0
mugsey
Asked:
mugsey
1 Solution
 
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
 
nisarkhanCommented:
try:

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

0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now