Click here to Skip to main content
Rate this: bad
good
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" />
 
    <Columns>
    <asp:TemplateField HeaderText="S.No.">
    <ItemTemplate>
 
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Edit/Remove">
    <ItemTemplate>
    <asp:LinkButton runat="server" ID="btnDelete" CommandName="Delete" Text="Delete"></asp:LinkButton>
    </ItemTemplate>
    <FooterTemplate>
    Insert new kit
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Product Code">
    <ItemTemplate>
    <asp:Label runat="server" ID="lblProductCode" Text='<%#Eval("product_code") %>'></asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:Button runat="server" ID="btnInsert" Text="Insert" CommandName="Insert" />
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label runat="server" ID="lblName" Text='<%#Eval("name") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox runat="server" ID="txtName1" Text='<%#Eval("name") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox runat="server" ID="txtName"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="MRP">
    <ItemTemplate>
    <asp:Label runat="server" ID="lblMRP" Text='<%#Eval("mrp") %>'></asp:Label>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:TextBox runat="server" ID="txtMRP1" Text='<%#Eval("mrp") %>'></asp:TextBox>
    </EditItemTemplate>
    <FooterTemplate>
    <asp:TextBox runat="server" ID="txtMRP"></asp:TextBox>
    </FooterTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Status">
    <ItemTemplate>
      <%#Convert.ToBoolean(DataBinder.Eval(Container, "DataItem.active")) ? "<img src='../images/active.gif'>" : "<img src='../images/deactive.gif'>"%>
    </ItemTemplate>
    <EditItemTemplate>
    <asp:CheckBox runat="server" ID="chkStatus" Text="Active" />
    </EditItemTemplate>
    <FooterTemplate>
    <asp:CheckBox runat="server" ID="chkStatus" />
    </FooterTemplate>
    </asp:TemplateField>
    </Columns>
    </asp:GridView>
 

 
code for .cs page:-
 
private void ShowNoResultFound(DataTable source, GridView gv)
  {
      source.Rows.Add(source.NewRow());
      gv.DataSource = source;
      gv.DataBind();
      int columnsCount = gv.Columns.Count;
      gv.Rows[0].Cells.Clear();
      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
good
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
  Permalink  
Comments
abhishekagrwl25 at 25-May-12 5:14am
   
now it is showing another error...that column active does not allow null....in line
 
"source.Rows.Add(source.NewRow());"
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 gridview....how 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
 
ShowNoResultFound
 
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...:)
 
thanx..:)
Technoses at 25-May-12 9:09am
   
ok!
mark it as solution plz...
Technoses at 25-May-12 9:10am
   
you can vote and bookmark it also..
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

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

Solution 3

http://www.codeproject.com/Tips/424661/Access-Controls-from-ASP-NET-GridView-EmptyDataTem
This will solve your problem.
 
its working for me please try this
  Permalink  
v2

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

  Print Answers RSS
0 OriginalGriff 304
1 Maciej Los 285
2 Sergey Alexandrovich Kryukov 230
3 Shweta N Mishra 186
4 Aajmot Sk 153
0 OriginalGriff 7,660
1 Sergey Alexandrovich Kryukov 7,072
2 DamithSL 5,604
3 Manas Bhardwaj 4,986
4 Maciej Los 4,760


Advertise | Privacy | Mobile
Web04 | 2.8.1411023.1 | Last Updated 23 Apr 2013
Copyright © CodeProject, 1999-2014
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