NULL In GridView EVAL Calling ServerSide Method In ItemTemplate

This example explains how to handle NULL In GridView EVAL Calling Serverside Method In ItemTemplate

In this example i am going to describe how to handle NULL values from DataBase in Eval method of GridView ItemTemplate or How to call Server side method written in code behind in ItemTemplate of GridView.

Handle NULL In GridView

My Table in database look like as shown in image below, some columns contains NULL values and i'll be showing "No Records Found" instead of NULL values in GridView.

To achieve this i've written a Method in code behind and will be calling this method in ItemTemplate of GridView.




Html source of GridView
<asp:GridView ID="GridView1" runat="server" 
              DataSourceID="SqlDataSource1" 
              AutoGenerateColumns="false">
<Columns>
<asp:BoundField ShowHeader="true" DataField="ID" 
                              HeaderText="ID" />
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" 
           Text='<%# CheckNull(Eval("Name")) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Location">
<ItemTemplate>
<asp:Label ID="Label1" runat="server" 
           Text='<%# CheckNull(Eval("Location")) %>'>
</asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT ID, Name, Location FROM Details">
</asp:SqlDataSource>

In above code i am calling server side method CheckNull written in code behind from ItemTemplate of GridView which check the NULL values and change it as we want.
C# code for the CheckNull method
protected string CheckNull(object objGrid)
    {
        if (object.ReferenceEquals(objGrid, DBNull.Value))
        {
            return "No Record Found";
        }
        else
        {
            return objGrid.ToString();
        }
     }
VB.NET code behind
Protected Function CheckNull(ByVal objGrid As Object) As String
    If Object.ReferenceEquals(objGrid, DBNull.Value) Then
        Return "No Record Found"
    Else
        Return objGrid.ToString()
    End If
End Function


And this is how gridview will render


Hope this helps


If you like this post than join us or share

8 comments:

Anonymous said...

Cute, but I would recommend that instead of doing this on the control, you should improve you query with the appropiate function (Coalesce(),Isnull(),etc.) seems to me it would improve the perfomance.

Greetings


Sarwa said...

It is really helped me lot. Thank you. You saved my one hour time.


Unknown said...

Hi there!
I have been asked to modify an asp.net project which was developed long time back. Now i dont have an .aspx.cs file and the proj works fine, since it is a code behind model. Is there anyway, i can get the .aspx.cs file? (i have all the dll files in place)


Ryan said...

Anonymous: In my case, I want to render a   character on the page instead of a blank space when the bound value is null. IMHO, this is better done in the page's code behind rather than in the db query.

Therefore, doing a check for null value in the query doesn't make sense, in my case.


Anonymous said...

Excellent solution ... Thanks for sharing ...
It helped me a lot ...
Qadir
IT Height


Anonymous said...

Thank u very much


Unknown said...

why don't you just use ISNULL in your sql statement? of course if you have access to it.

SELECT ISNULL(nullvaluefield, replacement) FROM table


Anonymous said...

it's helpful


Find More Articles