saeed garmsiri
saeed garmsiri

Reputation: 47

representation of boolean db column in asp grid view

I have a column in my database which accept true or false value. I want to display its value grid view column and using below code (my db column name is IsCancelled):

<asp:TemplateField HeaderText="کنسل شده" SortExpression="IsCancelled">
                                <ItemTemplate>%#Boolean.Parse(Eval("IsCancelled").ToString())) ? "Yes" : "No" %></ItemTemplate>
                            </asp:TemplateField>

but I'm getting following exception:

An exception of type 'System.NullReferenceException' occurred in App_Web_jcvfb4rq.dll but was not handled in user code

Additional information: Object reference not set to an instance of an object.

Upvotes: 1

Views: 525

Answers (2)

Amnesh Goel
Amnesh Goel

Reputation: 2655

Try below solution if you are using C#. Please note that IIF is not available in VB.

<asp:TemplateField HeaderText="کنسل شده" SortExpression="IsCancelled">
    <ItemTemplate>
        <%#IIf(Boolean.Parse(Eval("IsCancelled").ToString()), "Yes", "No")%>
    </ItemTemplate>
</asp:TemplateField>

In case you are using VB and looking for similar solution then use below code.

<asp:TemplateField HeaderText="کنسل شده" SortExpression="IsCancelled">
    <ItemTemplate><%# (Boolean.Parse(Eval("IsCancelled").ToString())) ? "Yes" : "No" %></ItemTemplate>
</asp:TemplateField>

However same can be done at server side, using below code.

Protected Sub GridName_RowDataBound(sender As Object, e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridName.RowDataBound
    If e.Row.RowType = DataControlRowType.DataRow Then
        If e.Row.Cells(13).Text = "True" Then
            e.Row.Cells(13).Text = "Yes"
        Else
            e.Row.Cells(13).Text = "No"
        End If
    End If
End Sub

Upvotes: 1

Koo SengSeng
Koo SengSeng

Reputation: 953

Did you face this problem if you debug your code without deploying? I suspect you get this error after deploy because the exception occur in "App_Web_XXX" which is compiled file.

So first we need to know if your local has such issue. If yes, then it's easier to debug to know if this is the exact line that having the issue.

Once you confirmed the exact line that has the issue. You can try the following:

1) Check and ensure that "IsCancelled" column is exists in that particular table that you called and there is no typo, as the name must match.

2) Check your select statement in the data source. Make sure your select command able to return that column.

3) Might not be relevant but you can try Convert.ToInt32, and check if the value is 1 or 0 instead.

Since the error is not about casting or conversion problem, it's probably complaining that it cannot find or read "IsCancelled".

Hope this helps.

Upvotes: 0

Related Questions