0

I have a dataset with one of it's fields named "Reviewed".

This dataset is popualted and below picture show it's data: enter image description here

Now Im trying to show this dataset inside a DataGrid :

<ASP:TemplateColumn HeaderText="Reviewed" HeaderStyle-HorizontalAlign="center" HeaderStyle-Wrap="True">
                                    <ItemStyle Wrap="false" HorizontalAlign="center" />
                                            <ItemTemplate>
                                                <ASP:Checkbox  Checked='<%# DataBinder.Eval(Container.DataItem, "Reviewed") %>' runat="server" ID="Label22" />
                                            </ItemTemplate>
                                            <EditItemTemplate>
                                                <Asp:Checkbox id="Textbox5" width="40" Checked='<%# DataBinder.Eval(Container.DataItem, "Reviewed") %>' runat="server" />
                                            </EditItemTemplate>
        </ASP:TemplateColumn>

I get the following Error:

Exception Details: System.InvalidCastException: Cast from type 'DBNull' to type 'Boolean' is not valid.

What I am doing wrong? Question is where DBNull comes from?

================================

Update:

Thanks for the nice answers. My main point of confusion was "STUPID" XML visualizer was wrongly showing Reviewed field as checked(as you see in the picture above). I checked the values in the dataset itself and realized all are indeed DBNUll.

1
  • as the error message says - you can't convert dbNull to a boolean value. you may have a look to this blog which presents a solution to this or have a look at my answer Commented Jul 18, 2013 at 14:08

2 Answers 2

1

It means that some of your data coming from database is null. You need to check for that before assigning.You might want to assign a default value Like.

iif((DataBinder.Eval(Container.DataItem, "Reviewed") is DbNull.Value),false, DataBinder.Eval(Container.DataItem, "Reviewed")))

You can also create a method that checks for both DBNULL and null values and returns you the appropriate value. like (it may have syntax error that you can correct)

<Asp:Checkbox id="Textbox5" width="40" Checked='<%# YourMethod(Eval("Reviewed")) %>' runat="server" />
Sign up to request clarification or add additional context in comments.

2 Comments

See the picture I posted. All of data are populated.
Execute your query in the database and you will find out that some of the row is containing the null value which is causing error
1

Try to change your markup code to this code.

<asp:Checkbox 
  Checked='<%# (Eval("reviewed")==DBNull.Value ? false : Eval("reviewed"))%>' 
  runat="server" ID="Label22" />

So instead of binding a nullValue to boolean property you assign false if there is DBNull. Credits go to limno

1 Comment

ah - noted it right now - but as @ehsan ullah provided a working example I will not adapt my example so others have a working example in c# too

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.