0

I'm a newbie to aspx programming. I'm facing the problem where the data I fetch contain null value. my code :

<asp:TemplateField HeaderText="NCRE" ItemStyle-Width="150px" >
    <ItemTemplate>
        <asp:Hyperlink ID="Hyperlink1" runat="server"  Text='<%# Eval("formid") %>' NavigateUrl='<%# Eval("formid","~/sapphire/ncrr" + Mid(Eval("formid"), 4, 1) + ".aspx?formid={0}") %>' />
    </ItemTemplate>
</asp:TemplateField>

And my code behind:

Protected Function geturl(value As Object) As String
    Dim NCRE As String = Convert.ToString("formid")
    If Mid(NCRE, 4, 1) = 2 Then
        Return ("~/sapphire/ncrr2.aspx")
    Else
        Return ("~/sapphire/ncrr1.aspx")
    End If
End Function
Max
  • 12,622
  • 16
  • 73
  • 101
Joe Cheong
  • 15
  • 1
  • 7

3 Answers3

1

To help you getting the right code from the post I've linked:

Copy this part into your code file (ending on aspx.cs):

Public Function ProcessMyDataItem(myValue As Object) As String
    If myValue Is Nothing OrElse myValue = DbNull.Value Then
        Return "(no value)"
    End If

    Return myValue.ToString()
End Function

And in your .aspx:

<asp:TemplateField HeaderText="NCRE" ItemStyle-Width="150px" >
    <ItemTemplate>
        <asp:Hyperlink ID="Hyperlink1" runat="server"  Text='<%# ProcessMyDataItem(Eval("formid")) %>' NavigateUrl='<%# Eval("formid","~/sapphire/ncrr" + Mid(ProcessMyDataItem(Eval("formid")), 4, 1) + ".aspx?formid={0}") %>' />
    </ItemTemplate>
</asp:TemplateField>
Community
  • 1
  • 1
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • 1
    @PatrickHofman Original answer was in the wrong language. Removed based upon the update. – duffn Aug 18 '15 at 01:13
0

You can use IIf (IIf(IsDBNull(Eval("fieldName")) = True, *do what You want when record is empty*, *do what You want when record isn't empty)).

There are examples :

Text = '<%# IIf(IsDBNull(Eval("formid")) = True, "no data", Eval("formid))%>'

Same code You can use for NavigateUrl :

NavigateUrl = '<%# IIf(IsDBNull(Eval("formid")) = True, "#", Eval("formid","~/sapphire/ncrr" + Mid(Eval("formid"), 4, 1) + ".aspx?formid={0}"))'

And, then, in Your function geturl (code behind) first check is value, for example # (do nothing, or just return empty), otherwise execute code.

nelek
  • 4,074
  • 3
  • 22
  • 35
0

You may only add an empty string to avoid the error:

Dim NCRE As String = Convert.ToString("formid") & ""

Or, to avoid error at the 4th position:

Dim NCRE As String = Convert.ToString("formid") & " "

David BS
  • 1,822
  • 1
  • 19
  • 35