helper function gives case error when called during datagrid binding

Discussion in 'ASP .Net' started by bhavin30@gmail.com, Jan 12, 2006.

  1. Guest

    Hi Guys,
    I have a quick question. I think code will explain it the best.
    This is what I am doing for one of the columns during databinding on my
    grid:

    <asp:TemplateColumn>
    <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    <ItemStyle Font-Size="11px" Font-Names="Verdana"
    HorizontalAlign="Center"></ItemStyle>
    <HeaderTemplate>
    <IMG src="images/detail_lineitems_invoice_no.gif" width="67"
    height="18">
    </HeaderTemplate>
    <ItemTemplate>
    <asp:HyperLink Font-Size="11px" ForeColor="Blue" Font-Underline=True
    Font-Name="Verdana" Target="Invoice" Runat="server" NavigateUrl='<%#
    CheckAppFlagDuringBinding(Container.DataItem("app_flag"),
    Container.DataItem("invoice_id"))%>'>
    <%# DataBinder.Eval(Container.DataItem, "invoice_no") %>
    </asp:HyperLink>
    </ItemTemplate>
    </asp:TemplateColumn>


    I basically check the flag in CheckAppFlagDuringBinding function, and
    return a string value.
    Here's the function:

    Public Function CheckAppFlagDuringBinding(ByVal sAppFlag As String,
    ByVal gInvoiceID As Guid) As String
    Dim sPath As String

    Try
    If sAppFlag = "APW" Then
    sPath = "http://nytimes.com"
    ElseIf sAppFlag = "JE" Then
    sPath = "http://cnet.com"
    Else
    sPath = "http://boston.com"
    End If

    Return sPath

    Catch ex As Exception
    HandleError(ex)
    End Try

    End Function

    It is a very simple function. But when i run this code i get a cast
    error: Cast from type 'DBNull' to type string. When i debug the code,
    i can see that a string is passed from the function fine (and
    correctly), but doesn't seem to 'stick' to the NavigationUrl property
    of the column. The function gets called only one time (maybe because
    there is only row in the dataset)

    Do you know why this might be happening? Any help is really
    appreciated. Thanks!!

    >bhavin
     
    , Jan 12, 2006
    #1
    1. Advertising

  2. It seems like one of your values is null.

    you need to change ur method slightly:

    public function blah (appflag as object, gInvoiceID as Guid) as string
    if appflag is nothing orelse appflag = DBNull.Value then
    'what to do if it's a null?
    else
    dim sAppFlag as string = cstr(appFlag)
    'rest of your code
    end if
    end function

    Karl
    --
    MY ASP.Net tutorials
    http://www.openmymind.net/


    <> wrote in message
    news:...
    > Hi Guys,
    > I have a quick question. I think code will explain it the best.
    > This is what I am doing for one of the columns during databinding on my
    > grid:
    >
    > <asp:TemplateColumn>
    > <HeaderStyle HorizontalAlign="Center"></HeaderStyle>
    > <ItemStyle Font-Size="11px" Font-Names="Verdana"
    > HorizontalAlign="Center"></ItemStyle>
    > <HeaderTemplate>
    > <IMG src="images/detail_lineitems_invoice_no.gif" width="67"
    > height="18">
    > </HeaderTemplate>
    > <ItemTemplate>
    > <asp:HyperLink Font-Size="11px" ForeColor="Blue" Font-Underline=True
    > Font-Name="Verdana" Target="Invoice" Runat="server" NavigateUrl='<%#
    > CheckAppFlagDuringBinding(Container.DataItem("app_flag"),
    > Container.DataItem("invoice_id"))%>'>
    > <%# DataBinder.Eval(Container.DataItem, "invoice_no") %>
    > </asp:HyperLink>
    > </ItemTemplate>
    > </asp:TemplateColumn>
    >
    >
    > I basically check the flag in CheckAppFlagDuringBinding function, and
    > return a string value.
    > Here's the function:
    >
    > Public Function CheckAppFlagDuringBinding(ByVal sAppFlag As String,
    > ByVal gInvoiceID As Guid) As String
    > Dim sPath As String
    >
    > Try
    > If sAppFlag = "APW" Then
    > sPath = "http://nytimes.com"
    > ElseIf sAppFlag = "JE" Then
    > sPath = "http://cnet.com"
    > Else
    > sPath = "http://boston.com"
    > End If
    >
    > Return sPath
    >
    > Catch ex As Exception
    > HandleError(ex)
    > End Try
    >
    > End Function
    >
    > It is a very simple function. But when i run this code i get a cast
    > error: Cast from type 'DBNull' to type string. When i debug the code,
    > i can see that a string is passed from the function fine (and
    > correctly), but doesn't seem to 'stick' to the NavigationUrl property
    > of the column. The function gets called only one time (maybe because
    > there is only row in the dataset)
    >
    > Do you know why this might be happening? Any help is really
    > appreciated. Thanks!!
    >
    >>bhavin

    >
     
    Karl Seguin [MVP], Jan 12, 2006
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chumley the Walrus

    Subroutine /helper function error

    Chumley the Walrus, Jun 10, 2004, in forum: ASP .Net
    Replies:
    2
    Views:
    338
    bruce barker
    Jun 10, 2004
  2. Alan Silver
    Replies:
    4
    Views:
    3,104
    Alan Silver
    Jul 11, 2005
  3. traveller
    Replies:
    0
    Views:
    1,234
    traveller
    Jan 8, 2008
  4. Dmitry Korolyov

    Datagrid not updated during delete, but updated during insert and update

    Dmitry Korolyov, Sep 22, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    0
    Views:
    429
    Dmitry Korolyov
    Sep 22, 2003
  5. kevin
    Replies:
    1
    Views:
    387
    Kairi Zikpin
    Jul 21, 2006
Loading...

Share This Page