datagrid can't get value to do insert

Ask a Question related to ASP.NET Data Grid Control, Design and Development.

  1. #1

    Default datagrid can't get value to do insert

    I am trying to get the value out of a datagrid please help
    Getting error
    Object reference not set to an instance of an object.
    at line
    newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;


    data binds and displays fine
    I enter value in textbox for comment
    click the insert button
    it goes to item command fine starts the foreach and errors

    here is code below is grid html

    private void dgComments_ItemCommand(object source, private void
    dgComments_ItemCommand(object source, DataGridCommandEventArgs e)
    {
    if (e.CommandName == "Insert")
    {
    SearchDAL dal= new SearchDAL();
    foreach(DataGridItem item in dgComments.Items)
    {
    if(!item.ItemType.ToString().Equals("EditItem"))
    {
    newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
    dal.InsertComment(newComment);
    }
    dgComments.DataBind();
    }
    }
    }


    datagrid html
    <asp:DataGrid id="dgComments" runat="server" ShowFooter="True"
    AutoGenerateColumns="False">
    <Columns>
    <asp:TemplateColumn HeaderText="CommentID">
    <ItemTemplate>
    <asp:Label id=Label3 runat="server" Text='<%# DataBinder.Eval(Container,
    "DataItem.CommentID") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:LinkButton id="LinkButton1" runat="server"
    CommandName="Insert">Insert</asp:LinkButton>
    </FooterTemplate>
    <EditItemTemplate>
    <asp:Label id=lblCommentID runat="server" Text='<%#
    DataBinder.Eval(Container, "DataItem.CommentID") %>'>
    </asp:Label>
    </EditItemTemplate>
    </asp:TemplateColumn>
    <asp:TemplateColumn HeaderText="Comment">
    <ItemTemplate>
    <asp:Label id=Label2 runat="server" Text='<%# DataBinder.Eval(Container,
    "DataItem.comment") %>'>
    </asp:Label>
    </ItemTemplate>
    <FooterTemplate>
    <asp:TextBox id="txtComment" runat="server"></asp:TextBox>
    </FooterTemplate>
    <EditItemTemplate>
    <asp:TextBox id="txtEditComment" runat="server"></asp:TextBox>
    </EditItemTemplate>
    </asp:TemplateColumn>
    </Columns>
    </asp:DataGrid>
    --
    cindy
    cindy Guest

  2. Similar Questions and Discussions

    1. Insert progress bar into datagrid?
      Hi, I have problem in displaying each progress bar value on it. I create the progress bar by using table. When i run, table just like...
    2. How to insert a row in a DataGrid in code?
      I thought I could insert a new row in a DataGrid in code by calling CurrencyManager.AddNew; but it does not work. I want to open a new row that...
    3. Insert row in DataGrid
      Hi I would like to know how to insert a row into a datagrid? i allready know how to edit, delete and update a row...
    4. Extending datagrid with new/insert button
      Hi, I try to develop a server control which inherits from DataGrid and adds a new/insert button to this grid. This is no problem, but I can't get...
    5. Add (INSERT) a New Row Using the ASP.Net Datagrid Control...
      I found some good information On "Add (INSERT) a New Row Using the ASP.Net Datagrid Control..." at this site...
  3. #2

    Default RE: datagrid can't get value to do insert

    Hi Cindy,

    Welcome to ASPNET newsgroup.
    Regarding on the problem you mentioned, from the code you provided, I think
    the cause is the DataGrid.Items collection which dosn't contains the Footer
    Item. The DataGrid.Items collection ony contains the
    Item/AlternateItem/Edit.. rows.
    For your scenario, you'd like to retrieve the control instance from the
    Footer template, you can make use of the
    DAtaGridCommandArgs paramter passed in the ItemCommand event and use its
    "Item" property to get the FooterRow.
    For example, the following code just retrieve a textbox instance from
    footer template in itemcommand event:

    =====================
    private void dgFooter_ItemCommand(object source,
    System.Web.UI.WebControls.DataGridCommandEventArgs e)
    {
    if(e.CommandName != "Insert")
    return;
    if(e.Item.ItemType == ListItemType.Footer)
    {
    TextBox txt = e.Item.FindControl("txtComment") as TextBox;

    .....
    }
    }
    ===========================

    Hope helps. Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]

    --------------------
    | Thread-Topic: datagrid can't get value to do insert
    | thread-index: AcXOvn/4cY+arEbkRUuWvyIvOAQ9TQ==
    | X-WBNR-Posting-Host: 71.136.160.120
    | From: "=?Utf-8?B?Y2luZHk=?=" <cmello@nospam.nospam>
    | Subject: datagrid can't get value to do insert
    | Date: Tue, 11 Oct 2005 16:50:03 -0700
    | Lines: 70
    | Message-ID: <CE83848E-303D-448E-BA5D-235F58D55065@microsoft.com>
    | MIME-Version: 1.0
    | Content-Type: text/plain;
    | charset="Utf-8"
    | Content-Transfer-Encoding: 7bit
    | X-Newsreader: Microsoft CDO for Windows 2000
    | Content-Class: urn:content-classes:message
    | Importance: normal
    | Priority: normal
    | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSF TNGXA03.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.datagridc ontrol:14477
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    |
    | I am trying to get the value out of a datagrid please help
    | Getting error
    | Object reference not set to an instance of an object.
    | at line
    | newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
    |
    |
    | data binds and displays fine
    | I enter value in textbox for comment
    | click the insert button
    | it goes to item command fine starts the foreach and errors
    |
    | here is code below is grid html
    |
    | private void dgComments_ItemCommand(object source, private void
    | dgComments_ItemCommand(object source, DataGridCommandEventArgs e)
    | {
    | if (e.CommandName == "Insert")
    | {
    | SearchDAL dal= new SearchDAL();
    | foreach(DataGridItem item in dgComments.Items)
    | {
    | if(!item.ItemType.ToString().Equals("EditItem"))
    | {
    | newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
    | dal.InsertComment(newComment);
    | }
    | dgComments.DataBind();
    | }
    | }
    | }
    |
    |
    | datagrid html
    | <asp:DataGrid id="dgComments" runat="server" ShowFooter="True"
    | AutoGenerateColumns="False">
    | <Columns>
    | <asp:TemplateColumn HeaderText="CommentID">
    | <ItemTemplate>
    | <asp:Label id=Label3 runat="server" Text='<%#
    DataBinder.Eval(Container,
    | "DataItem.CommentID") %>'>
    | </asp:Label>
    | </ItemTemplate>
    | <FooterTemplate>
    | <asp:LinkButton id="LinkButton1" runat="server"
    | CommandName="Insert">Insert</asp:LinkButton>
    | </FooterTemplate>
    | <EditItemTemplate>
    | <asp:Label id=lblCommentID runat="server" Text='<%#
    | DataBinder.Eval(Container, "DataItem.CommentID") %>'>
    | </asp:Label>
    | </EditItemTemplate>
    | </asp:TemplateColumn>
    | <asp:TemplateColumn HeaderText="Comment">
    | <ItemTemplate>
    | <asp:Label id=Label2 runat="server" Text='<%#
    DataBinder.Eval(Container,
    | "DataItem.comment") %>'>
    | </asp:Label>
    | </ItemTemplate>
    | <FooterTemplate>
    | <asp:TextBox id="txtComment" runat="server"></asp:TextBox>
    | </FooterTemplate>
    | <EditItemTemplate>
    | <asp:TextBox id="txtEditComment" runat="server"></asp:TextBox>
    | </EditItemTemplate>
    | </asp:TemplateColumn>
    | </Columns>
    | </asp:DataGrid>
    | --
    | cindy
    |

    Steven Cheng[MSFT] Guest

  4. #3

    Default RE: datagrid can't get value to do insert

    Hi Cindy,

    How are you doing on this issue, does the suggestions in my last reply
    helps a little? If there're anything else we can help, please feel free to
    post here.

    Thanks,

    Steven Cheng
    Microsoft Online Support

    Get Secure! [url]www.microsoft.com/security[/url]
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
    --------------------
    | X-Tomcat-ID: 65148547
    | References: <CE83848E-303D-448E-BA5D-235F58D55065@microsoft.com>
    | MIME-Version: 1.0
    | Content-Type: text/plain
    | Content-Transfer-Encoding: 7bit
    | From: [email]stcheng@online.microsoft.com[/email] (Steven Cheng[MSFT])
    | Organization: Microsoft
    | Date: Wed, 12 Oct 2005 03:23:46 GMT
    | Subject: RE: datagrid can't get value to do insert
    | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | Message-ID: <LPvqlxtzFHA.3712@TK2MSFTNGXA02.phx.gbl>
    | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | Lines: 109
    | Path: TK2MSFTNGXA02.phx.gbl
    | Xref: TK2MSFTNGXA02.phx.gbl
    microsoft.public.dotnet.framework.aspnet.datagridc ontrol:14478
    | NNTP-Posting-Host: TOMCATIMPORT1 10.201.218.122
    |
    | Hi Cindy,
    |
    | Welcome to ASPNET newsgroup.
    | Regarding on the problem you mentioned, from the code you provided, I
    think
    | the cause is the DataGrid.Items collection which dosn't contains the
    Footer
    | Item. The DataGrid.Items collection ony contains the
    | Item/AlternateItem/Edit.. rows.
    | For your scenario, you'd like to retrieve the control instance from the
    | Footer template, you can make use of the
    | DAtaGridCommandArgs paramter passed in the ItemCommand event and use its
    | "Item" property to get the FooterRow.
    | For example, the following code just retrieve a textbox instance from
    | footer template in itemcommand event:
    |
    | =====================
    | private void dgFooter_ItemCommand(object source,
    | System.Web.UI.WebControls.DataGridCommandEventArgs e)
    | {
    | if(e.CommandName != "Insert")
    | return;
    | if(e.Item.ItemType == ListItemType.Footer)
    | {
    | TextBox txt = e.Item.FindControl("txtComment") as TextBox;
    |
    | .....
    | }
    | }
    | ===========================
    |
    | Hope helps. Thanks,
    |
    | Steven Cheng
    | Microsoft Online Support
    |
    | Get Secure! [url]www.microsoft.com/security[/url]
    |
    | --------------------
    | | Thread-Topic: datagrid can't get value to do insert
    | | thread-index: AcXOvn/4cY+arEbkRUuWvyIvOAQ9TQ==
    | | X-WBNR-Posting-Host: 71.136.160.120
    | | From: "=?Utf-8?B?Y2luZHk=?=" <cmello@nospam.nospam>
    | | Subject: datagrid can't get value to do insert
    | | Date: Tue, 11 Oct 2005 16:50:03 -0700
    | | Lines: 70
    | | Message-ID: <CE83848E-303D-448E-BA5D-235F58D55065@microsoft.com>
    | | MIME-Version: 1.0
    | | Content-Type: text/plain;
    | | charset="Utf-8"
    | | Content-Transfer-Encoding: 7bit
    | | X-Newsreader: Microsoft CDO for Windows 2000
    | | Content-Class: urn:content-classes:message
    | | Importance: normal
    | | Priority: normal
    | | X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3790.0
    | | Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
    | | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSF TNGXA03.phx.gbl
    | | Xref: TK2MSFTNGXA02.phx.gbl
    | microsoft.public.dotnet.framework.aspnet.datagridc ontrol:14477
    | | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridc ontrol
    | |
    | | I am trying to get the value out of a datagrid please help
    | | Getting error
    | | Object reference not set to an instance of an object.
    | | at line
    | | newComment = ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
    | |
    | |
    | | data binds and displays fine
    | | I enter value in textbox for comment
    | | click the insert button
    | | it goes to item command fine starts the foreach and errors
    | |
    | | here is code below is grid html
    | |
    | | private void dgComments_ItemCommand(object source, private void
    | | dgComments_ItemCommand(object source, DataGridCommandEventArgs e)
    | | {
    | | if (e.CommandName == "Insert")
    | | {
    | | SearchDAL dal= new SearchDAL();
    | | foreach(DataGridItem item in dgComments.Items)
    | | {
    | | if(!item.ItemType.ToString().Equals("EditItem"))
    | | {
    | | newComment =
    ((TextBox)item.Cells[1].FindControl("txtComment")).Text;
    | | dal.InsertComment(newComment);
    | | }
    | | dgComments.DataBind();
    | | }
    | | }
    | | }
    | |
    | |
    | | datagrid html
    | | <asp:DataGrid id="dgComments" runat="server" ShowFooter="True"
    | | AutoGenerateColumns="False">
    | | <Columns>
    | | <asp:TemplateColumn HeaderText="CommentID">
    | | <ItemTemplate>
    | | <asp:Label id=Label3 runat="server" Text='<%#
    | DataBinder.Eval(Container,
    | | "DataItem.CommentID") %>'>
    | | </asp:Label>
    | | </ItemTemplate>
    | | <FooterTemplate>
    | | <asp:LinkButton id="LinkButton1" runat="server"
    | | CommandName="Insert">Insert</asp:LinkButton>
    | | </FooterTemplate>
    | | <EditItemTemplate>
    | | <asp:Label id=lblCommentID runat="server" Text='<%#
    | | DataBinder.Eval(Container, "DataItem.CommentID") %>'>
    | | </asp:Label>
    | | </EditItemTemplate>
    | | </asp:TemplateColumn>
    | | <asp:TemplateColumn HeaderText="Comment">
    | | <ItemTemplate>
    | | <asp:Label id=Label2 runat="server" Text='<%#
    | DataBinder.Eval(Container,
    | | "DataItem.comment") %>'>
    | | </asp:Label>
    | | </ItemTemplate>
    | | <FooterTemplate>
    | | <asp:TextBox id="txtComment" runat="server"></asp:TextBox>
    | | </FooterTemplate>
    | | <EditItemTemplate>
    | | <asp:TextBox id="txtEditComment" runat="server"></asp:TextBox>
    | | </EditItemTemplate>
    | | </asp:TemplateColumn>
    | | </Columns>
    | | </asp:DataGrid>
    | | --
    | | cindy
    | |
    |
    |

    Steven Cheng[MSFT] Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139