0

while editing the status column grid view im getting this error - Object cannot be cast from DBNull to other types.i have newly added one column status(bit,null) in table so status column having null value. while binding status column with NULL value getting error .

<asp:TemplateField HeaderText="status">
                        <ItemTemplate>
                        <asp:Label ID="lblstatus" runat="server" Text='<%#DataBinder.Eval(Container.DataItem, "status") %>'></asp:Label>
                        </ItemTemplate>
                        <EditItemTemplate>
                        <asp:CheckBox ID="chkEditStatus" runat="server" Checked='<%# Convert.ToBoolean(Eval("status")) %>' />
                        </EditItemTemplate>
                        <FooterTemplate>
                        <asp:CheckBox ID="chkAddStatus" runat="server" Checked='<%# Convert.ToBoolean(Eval("status")) %>' />
                        </FooterTemplate>
                    </asp:TemplateField>

how to edit edititem template so that this error will not come while editing grid ?

1 Answer 1

1

That's because you are trying to convert a DBNull to Boolean, you need to check this while retrieving the data and calculate accordingly while fetching it from DB:-

bool status = reader["status"] is DBNull ? false : Convert.ToBoolean(reader["status"]);
Sign up to request clarification or add additional context in comments.

1 Comment

@krishnamohan - Hey were you able to solve it? By reader I meant while fetching your data by SqlDataReader.

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.