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
<aspataGrid 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>
</aspataGrid>
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
<aspataGrid 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>
</aspataGrid>