datagrid can't get value to do insert

C

cindy

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>
 
S

Steven Cheng[MSFT]

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! www.microsoft.com/security

--------------------
| 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=?=" <[email protected]>
| Subject: datagrid can't get value to do insert
| Date: Tue, 11 Oct 2005 16:50:03 -0700
| Lines: 70
| Message-ID: <[email protected]>
| 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.datagridcontrol
| NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol:14477
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
|
| 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
|
 
S

Steven Cheng[MSFT]

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! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| X-Tomcat-ID: 65148547
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain
| Content-Transfer-Encoding: 7bit
| From: (e-mail address removed) (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.datagridcontrol
| Message-ID: <[email protected]>
| Newsgroups: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| Lines: 109
| Path: TK2MSFTNGXA02.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet.datagridcontrol: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! www.microsoft.com/security
|
| --------------------
| | 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=?=" <[email protected]>
| | Subject: datagrid can't get value to do insert
| | Date: Tue, 11 Oct 2005 16:50:03 -0700
| | Lines: 70
| | Message-ID: <[email protected]>
| | 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.datagridcontrol
| | NNTP-Posting-Host: TK2MSFTNGXA03.phx.gbl 10.40.2.250
| | Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGXA01.phx.gbl!TK2MSFTNGXA03.phx.gbl
| | Xref: TK2MSFTNGXA02.phx.gbl
| microsoft.public.dotnet.framework.aspnet.datagridcontrol:14477
| | X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet.datagridcontrol
| |
| | 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
| |
|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top