This is a migrated thread and some comments may be shown as answers.

Handling null values

4 Answers 251 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Clive Hoggar
Top achievements
Rank 1
Clive Hoggar asked on 01 Dec 2010, 01:36 PM
Hi

I guess I should have been able to figure this out but I get error messages for all the methods I have tried..

I have a grid template column whose content is the product of two db fields, and when CMVPU is null, there
is an exception error related to dbNull value. What I want to happen is - where there is a null db value it is treated
as zero.

Please suggest the simplest way to accomplish this.

Thanks for help!

Clive
<ItemTemplate>
           <asp:Label ID="CMVPULabel" runat="server" Text='<%# Eval("CMVPU") * Eval("Qty")  %>'
            style="text-align: right"></asp:Label>
  </ItemTemplate>

4 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Dec 2010, 07:17 AM
Hello Clive,

Give a try with the following approach to achieve your scenario.

ASPX:
<ItemTemplate>
     <asp:Label ID="CMVPULabel" runat="server" Style="text-align: right"
                    Text='<%#(Convert.IsDBNull(Eval("CMVPU")))?0:  (Convert.ToInt32(Eval("CMVPU")) * Convert.ToInt32(Eval("Qty")))  %>'>
       </
asp:Label>
</ItemTemplate>

Thanks,
Princy.
0
Clive Hoggar
Top achievements
Rank 1
answered on 02 Dec 2010, 02:09 PM
Thanks Prinzy,

I tried this but when I run it in Visual Studio I get an error message : "The '?' character cannot be used here"


Clive
0
Princy
Top achievements
Rank 2
answered on 03 Dec 2010, 06:11 AM
Hello Clive,

 The above aspx code is in C#. ?: is the C# ternary conditional operator. Make the following modification in the above code and check if it works now.

ASPX:
<ItemTemplate>
  <asp:Label ID="CMVPULabel" runat="server" Style="text-align: right"
    Text='<%#If((Convert.IsDBNull(Eval("CMVPU"))), 0, (Convert.ToInt32(Eval("CMVPU")) * Convert.ToInt32(Eval("Qty"))))  %>'>
  </asp:Label>
</ItemTemplate>

Thanks,
Princy.
0
Clive Hoggar
Top achievements
Rank 1
answered on 03 Dec 2010, 01:13 PM
Perfect!

Thanks

Clive
Tags
Grid
Asked by
Clive Hoggar
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Clive Hoggar
Top achievements
Rank 1
Share this question
or