Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

object reference not set to an instance of an object

Status
Not open for further replies.

mariuslaurentiu

Programmer
May 31, 2005
10
RO
Hello,
When i try to execute this part i received "object reference not set to an instance of an object"

Dim TextBoxText as String
Dim TxtBox as TextBox
For Each row As GridViewRow in GridView1.Rows
TxtBox=XType(row.FindControl("TxtStr"),TextBox)
TextBoxText=TxtBox.Text
Next

And i don't figure why is that.
Thank you
 
GridView1.Rows returns all rows: header, footer, row, empty row, separator, etc. you need to ensure the row is a data row before continuing.
Code:
foreach(var row in GridView1.Rows)
{
   if(row.RowType != GridViewRowType.DataRow) continue;

   TxtBox = row.FindControl("TxtStr") as TextBox;
   if (TxtBox ==  null) continue;

   TextBoxText=TxtBox.Text
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
I think this is the problem... the textbox i looking is declared like this:

<Columns>

<asp:TemplateField HeaderText="Strada">
<ItemTemplate>
<%# Eval("Strada")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="EditStrada" Text='<%# Bind("Strada") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="InsertStrada" Text='<%# Bind("Strada") %>' />
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Bloc">
<ItemTemplate>
<%# Eval("Bloc")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="EditBloc" Text='<%# Bind("Bloc") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="InsertBloc" Text='<%# Bind("Bloc") %>' />
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Scara">
<ItemTemplate>
<%# Eval("Scara")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="EditScara" Text='<%# Bind("Scara") %>' />
</EditItemTemplate>
<FooterTemplate>
<asp:TextBox runat="server" ID="InsertScara" Text='<%# Bind("Scara") %>' />
</FooterTemplate>
</asp:TemplateField>

<asp:TemplateField HeaderText="Commands">
<ItemTemplate>
<asp:Button runat="server" ID="Edit1" Text="Edit" CommandName="Edit" />
<asp:Button runat="server" ID="Delete1" Text="Delete" CommandName="Delete" />
</ItemTemplate>
<EditItemTemplate>
<asp:Button runat="server" ID="Update1" Text="Update" CommandName="Update" />
<asp:Button runat="server" ID="Cancel1" Text="Cancel" CommandName="Cancel" />
</EditItemTemplate>
<FooterTemplate>
<asp:Button runat="server" ID="Insert1" Text="Insert" CommandName="InsertNew" />
<asp:Button runat="server" ID="Cancel2" Text="Cancel" CommandName="CancelNew" />
</FooterTemplate>
</asp:TemplateField>


</Columns>
<EmptyDataTemplate>
<asp:TextBox runat="server" ID="TxtStrada" />
<asp:TextBox runat="server" ID="TxtBloc" />
<asp:TextBox runat="server" ID="TxtScara" />
<asp:Button runat="server" ID="NoDataInsert" CommandName="NoDataInsert" Text="Insert"/>
</EmptyDataTemplate>
 
I don't see [tt]TxtStr[/tt] declared anywhere, so yes that will be a problem.

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
ok, then you need to check if the data row type is EmptyData. However you can make some assumptions about your code at this point.
1. the button and the empty data are within the same template.
2. this template is rendered exactly one time.
you code could look like something like this instead
Code:
protected void NoDataInsert_Click(object sender, EventArgs e)
{
   var button  = (Control)sender;
   var parent  = button.Parent;
   var input   = (TextBox)Parent.FindControl("TxtStrada");
   TextBoxText = TxtBox.Text
}

Jason Meckley
Programmer

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top