Click here to Skip to main content
13,737,743 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Here i am binding the datatable values with the grid but when the datatable has null values it returns an error

Additional information: Unable to cast object of type 'System.DBNull' to type 'System.Byte[]'.


What I have tried:

<asp:GridView ID="grdDoctorConfirmation" CssClass="table table-striped table-bordered table-hover" runat="server" AutoGenerateColumns="False">
            <Columns>
                <asp:TemplateField HeaderText="Select">
                    <HeaderTemplate>
                        <asp:CheckBox ID="chkAll" runat="server" AutoPostBack="True" />
                    </HeaderTemplate>
                    <ItemTemplate>
                        <asp:CheckBox ID="chkUpdate" runat="server" AutoPostBack="True"  />
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SNo" >
                    <ItemTemplate>
                <%# Container.DataItemIndex + 1 %>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="AppointmentID" HeaderText="ID" HeaderStyle-CssClass="hiddencolumn"  ItemStyle-CssClass="hiddencolumn">
                <HeaderStyle CssClass="hiddencolumn" />
                <ItemStyle CssClass="hiddencolumn" />
                </asp:BoundField>
                <asp:BoundField DataField="PatientID" HeaderText="Patient" />
                <%--<asp:BoundField DataField="DoctorID" HeaderText="Doctor" />--%>
                <asp:BoundField DataField="DoctorName" HeaderText="Doctor" />
                <%--<asp:BoundField DataField="Treatment" HeaderText="Treatment" />--%>
                <asp:BoundField DataField="TreatmentName" HeaderText="Treatment" />
                <asp:BoundField DataField="ADate" HeaderText="Date" DataFormatString="{0:dd/MM/yyyy}"/>
                <asp:BoundField DataField="TreatmentDate" HeaderText="Date" DataFormatString="{0:dd/MM/yyyy}"/>
                <asp:BoundField DataField="ASlotID" HeaderText="Slot" />
                <asp:BoundField DataField="Remarks" HeaderText="Remarks" />
                <asp:TemplateField HeaderText="Photo">
                    <ItemTemplate>
                                    <asp:Image ID="imgPhoto" runat="server" ImageUrl='<%# "data:Image/png;base64,"+Convert.ToBase64String((byte[])Eval("Photo")) %>' Height="50px" Width="50px" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>



public void FillRequestAppointment()
        {
            //DateTime today = DateTime.Today;
            //string todaydate = today.ToString("MM/dd/yyyy");
            DataTable dt = dabll.GetDoctorsConfirmation(uCls.UserName);
            obj.FillGrid(grdDoctorConfirmation, dt);
        }
Posted 30-Dec-17 22:57pm
Updated 31-Dec-17 7:14am

1 solution

Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

try

DataTable dt = dabll.GetDoctorsConfirmation(uCls.UserName);
          dt.AsEnumerable().ToList().ForEach(row =>   row["Photo"] = row["Photo"] == DBNull.Value ? null : row["Photo"] ); // handles dbnull value
          obj.FillGrid(grdDoctorConfirmation, dt);
  Permalink  

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01-2016 | 2.8.180920.1 | Last Updated 31 Dec 2017
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100