Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: C# ASP.NET
hello all,

I am filling my gridview when no data is present, but in a field named "active", is a bit datatype , showing error "
Object cannot be cast from DBNull to other types
code for .aspx:-
<asp:GridView CssClass="maingrid" runat="server" ID="Grid1"
        AutoGenerateColumns="False" ShowFooter="true" DataKeyNames="id"
        onrowdeleting="Grid1_RowDeleting" onrowcommand="Grid1_RowCommand" >
    <HeaderStyle CssClass="headergrid"/>
    <FooterStyle CssClass="footergrid" />
    <asp:TemplateField HeaderText="S.No.">
    <asp:TemplateField HeaderText="Edit/Remove">
    <asp:LinkButton runat="server" ID="btnDelete" CommandName="Delete" Text="Delete"></asp:LinkButton>
    Insert new kit
    <asp:TemplateField HeaderText="Product Code">
    <asp:Label runat="server" ID="lblProductCode" Text='<%#Eval("product_code") %>'></asp:Label>
    <asp:Button runat="server" ID="btnInsert" Text="Insert" CommandName="Insert" />
    <asp:TemplateField HeaderText="Name">
    <asp:Label runat="server" ID="lblName" Text='<%#Eval("name") %>'></asp:Label>
    <asp:TextBox runat="server" ID="txtName1" Text='<%#Eval("name") %>'></asp:TextBox>
    <asp:TextBox runat="server" ID="txtName"></asp:TextBox>
    <asp:TemplateField HeaderText="MRP">
    <asp:Label runat="server" ID="lblMRP" Text='<%#Eval("mrp") %>'></asp:Label>
    <asp:TextBox runat="server" ID="txtMRP1" Text='<%#Eval("mrp") %>'></asp:TextBox>
    <asp:TextBox runat="server" ID="txtMRP"></asp:TextBox>
    <asp:TemplateField HeaderText="Status">
      <%#Convert.ToBoolean(DataBinder.Eval(Container, "")) ? "<img src='../images/active.gif'>" : "<img src='../images/deactive.gif'>"%>
    <asp:CheckBox runat="server" ID="chkStatus" Text="Active" />
    <asp:CheckBox runat="server" ID="chkStatus" />

code for .cs page:-
private void ShowNoResultFound(DataTable source, GridView gv)
      gv.DataSource = source;
      int columnsCount = gv.Columns.Count;
      gv.Rows[0].Cells.Add(new TableCell());
      gv.Rows[0].Cells[0].ColumnSpan = columnsCount;
      gv.Rows[0].Cells[0].HorizontalAlign = HorizontalAlign.Center;
      gv.Rows[0].Cells[0].ForeColor = System.Drawing.Color.Red;
      gv.Rows[0].Cells[0].Text = "NO RESULT FOUND!";
error occur at gv.DataBind() event..
Posted 24-May-12 23:44pm
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

when you retrive database then you can use
IsNull(active,0) as active
in your sql statement

if this is your solution , please mark as solution
abhishekagrwl25 at 25-May-12 5:14am
now it is showing another error...that column active does not allow line
Technoses at 25-May-12 5:27am
you should usegv.EmptyDataText = "No Result Found";in case of no result
abhishekagrwl25 at 25-May-12 5:31am
but i have to show footer row when no data is present in to do it..?
Technoses at 25-May-12 5:32am
have you try this.
first you should try this.
i think header, item, footer row not shows when database is blank
abhishekagrwl25 at 25-May-12 5:35am
i have tried it. but still having same problem.
Technoses at 25-May-12 6:57am
is your problem has been solved
if yes please mark it as solution
Technoses at 25-May-12 5:40am
what is error now???
Copy and Paste it with stack trace
Technoses at 25-May-12 6:59am
remove your code of
it is not required to set
only in case of bind
you should add below line
gv.EmptyDataText = "No Result Found";
before bind grid


abhishekagrwl25 at 25-May-12 7:51am
ok , i have done it...solved...:)
Technoses at 25-May-12 9:09am
mark it as solution plz...
Technoses at 25-May-12 9:10am
you can vote and bookmark it also..
Rate this: bad
Please Sign up or sign in to vote.

Solution 2

i have used emptydatatemplate in which i have created a table inside emptydatatemplate...
Rate this: bad
Please Sign up or sign in to vote.

Solution 3
This will solve your problem.
its working for me please try this

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

  Print Answers RSS
0 Zoltán Zörgő 265
1 /\jmot 205
2 CHill60 145
3 Peter Leow 145
4 Sergey Alexandrovich Kryukov 115
0 Sergey Alexandrovich Kryukov 9,353
1 OriginalGriff 6,881
2 Peter Leow 4,577
3 Zoltán Zörgő 4,129
4 Richard MacCutchan 2,882

Advertise | Privacy | Mobile
Web02 | 2.8.150129.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

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