0

PolicyPersonID or PolicyMemberID will be a parameter in GetInsuranceCompanyName(). But PolicyPersonID or PolicyMemberID depends on the list of object bound to the grid view. If it's a list of policy members, PolicyMemberID will be there and PolicyPersonID won't be there, and vice versa.

I've tried an if statement but since it's either PolicyPersonID or PolicyMemberID, the if statement doesn't really work.

<div class="row">
    <div class="col-md-12">
        <asp:GridView ID="gvCustomerView" runat="server" AllowPaging="True" AllowSorting="true" AutoGenerateColumns="False" CssClass="table table-striped table-bordered table-hover"
            OnPageIndexChanging="gvCustomerView_PageIndexChanging" PageSize="10">
            <Columns>
                <asp:TemplateField HeaderText="Customer Name">
                    <ItemTemplate>
                        <asp:Label ID="lblCustomerName" runat="server" Text='<%# GetCustomerName(Eval("InvoiceMasterID")) %>' ForeColor="Black"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField DataField="MembershipNo" HeaderText="Membership Number" />

                <asp:TemplateField HeaderText="Insurance Company">
                    <ItemTemplate>
                        <asp:Label ID="lblInsuranceCompany" runat="server" Text='<%# try { GetInsuranceCompany(Eval("PolicyPersonID")); } catch (Exception) { GetInsuranceCompany(Eval("PolicyMemberID")); } %>' ForeColor="Black"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>

                <asp:BoundField DataField="PolicyNumber" HeaderText="Policy Number" />

                <asp:TemplateField HeaderText="Currency Type And Rate">
                    <ItemTemplate>
                        <asp:Label ID="lblCurrencyTypeAndRate" runat="server" Text='<%# GetCurrencyTypeAndRate(Eval("InvoiceMasterID")) %>' ForeColor="Black"></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>
    </div>
</div>

If someone can explain what does the error mean with this:

Text='<%# try { GetInsuranceCompany(Eval("PolicyPersonID")); } catch (Exception) { GetInsuranceCompany(Eval("PolicyMemberID")); } %>'
GSerg
  • 76,472
  • 17
  • 159
  • 346
Lakshitha
  • 11
  • 1
  • 4
  • Is there a reason why you are not putting this code in a code behind? – logixologist Jan 16 '19 at 14:51
  • 1
    I don’t think you can have try inside databinding, can you please try moving into your function GetInsuranceCompany.. – estinamir Jan 16 '19 at 14:51
  • @bestinamir I didnt really evaluate all the code but I dont think try/catch would work like the way the OP is doing it,. – logixologist Jan 16 '19 at 14:53
  • the code behind has the logic for the method GetInsuranceCompanyName(). The insurance company is set depending on the PolicyMemberID or PolicyPersonID – Lakshitha Jan 16 '19 at 14:57

1 Answers1

3

The <%# token evaluates a single expression. It can't process try/catch blocks or other complex statements.

I would instead create a GetInsuranceCompanyOrDefault method (the name is up to you) in the code-behind that does this logic and call that method from the markup.

As a side note, throwing exceptions is an expensive way to do branching - if there is a way to know if GetInsuranceCompany will fail before calling the method that will be a cheaper way to determine which input to use.

D Stanley
  • 149,601
  • 11
  • 178
  • 240
  • Good point @DStanley. I did a post on this actually about the try catch vs if. https://stackoverflow.com/questions/17335217/try-catch-or-if-statement/17335269#17335269 – logixologist Jan 16 '19 at 14:54