Click here to Skip to main content
12,394,573 members (73,265 online)
Rate this:
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") %>'>

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 22: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 25-May-12 5:14am
now it is showing another error...that column active does not allow line

Technoses 25-May-12 5:27am
you should usegv.EmptyDataText = "No Result Found";in case of no result
abhishekagrwl25 25-May-12 5:31am
but i have to show footer row when no data is present in to do it..?
Technoses 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 25-May-12 5:35am
i have tried it. but still having same problem.
Technoses 25-May-12 6:57am
is your problem has been solved

if yes please mark it as solution
Technoses 25-May-12 5:40am
what is error now???
Copy and Paste it with stack trace
Technoses 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 25-May-12 7:51am
ok , i have done it...solved...:)

Technoses 25-May-12 9:09am
mark it as solution plz...
Technoses 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
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web02 | 2.8.160721.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2016
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