Object reference not set to an instance of an object error when using Master Page and Content Page

Hi,
  I am creating a Master page and content page.In my content page i have button functions.
Previously before using the master page my code inside the button function was
controlid1 = "textbox" + counter1.ToString();
                    TextBox tb =form2.FindControl(controlid1) as TextBox;
                    if (tb.Text == String.Empty)

As i included the Master page  i replaced form2.FindControl(controlid1) as TextBox;
to FindControl(controlid1) as TextBox;

But now when i click my button functions I get this error

Object reference not set to an instance of an object.
near the below code
controlid1 = "textbox" + counter1.ToString();
                    TextBox tb = FindControl(controlid1) as TextBox;
                    if (tb.Text == String.Empty)

is that due to the replacement of
 form2.FindControl(controlid1) as TextBox;
in to
FindControl(controlid1) as TextBox;

How can solve this problem?



asudhaaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

DhaestCommented:
Can you show the findControl-method ?

Probably it only searches on the current form (and not on his parent and/or childs)


Using FindControl with a MasterPage in ASP.NET & C#
http://www.aspnettutorials.com/tutorials/controls/findcontrol-csharp.aspx
0
wellholeCommented:
If your control is in your master page, you can try Master.FindControl().
0
asudhaaAuthor Commented:
My Master Page has only the form2.
In my content page I  am creating dymanic text boxes.Thes textboxes are displayed in the content page.In the content pabe i gave a button function ReloadBtn which loads the data from the database into the text boxes.But now when i click the Button Reload i get the error
Object reference not set to an instance of an object.
This is the code for the button function
 
protected void ReloadBtn_Click(object sender, EventArgs e)
        {
            int counter = tblCols;
            string controlid = "1";
            int institute = Convert.ToInt32(Label1.Text);
            int period = Convert.ToInt32(DropDownList1.SelectedValue);
            sqlCon.Open();
            // Create DataAdapter object    
            SqlDataAdapter da = new SqlDataAdapter("SELECT Staff_period_begin,Incoming,Outgoing,During_First_Six_Months,Dismissed,Own_Initiatives,Transfers,Staff_period_end,Bank,PCH FROM OrderedReport3 WHERE Institution_no=" + institute + " AND Period_ID=" + period + "", sqlCon);
                      DataTable dt = new DataTable();
            da.Fill(dt);
            Response.Write(dt);
            //6:Access data row wise using for each loop 
            foreach (DataRow row in dt.Rows)
            {
                foreach (DataColumn col in dt.Columns)
                {
                    controlid = "textbox" + counter.ToString();
                    TextBox tb = Master.FindControl(controlid) as TextBox;
                    tb.Text = (row[col]).ToString();
                                    if ((counter == ((tblCols - 1) * 7)) || (counter == ((tblCols - 1) * 10)) || (counter == ((tblCols - 1) * 24)) || (counter == ((tblCols - 1) * 31)) || (counter == ((tblCols - 1) * 35)))
                        counter = counter + 11;

                    else
                        counter = counter + 1;
                                   }
            }

            sqlCon.Close();

        }

Open in new window



the master page code is

<%@ Master Language="C#" AutoEventWireup="true" CodeBehind="FirstPage.master.cs" Inherits="webapp.FirstPage" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">

  <title>Untitled Page</title>

  <link href="~/StyleSheet.css" rel="Stylesheet" type="text/css" />

</head>
<body>
  <form id="form2" runat="server">
    <asp:Menu ID="Menu1" runat="server" Orientation="Horizontal">
      <Items>
        <asp:MenuItem NavigateUrl="~/Default.aspx" Text="Home" Value="Home"/>
        <asp:MenuItem NavigateUrl="~/WebForm1.aspx" Text="  Report1  " Value="Report1"/>
        <asp:MenuItem NavigateUrl="~/WebForm2.aspx" Text="  Report2  " Value="Report2"/>
        <asp:MenuItem NavigateUrl="~/WebForm3.aspx"  Text="  Report3  " Value="Report3"/>
      </Items>      
    </asp:Menu>
    <div>
      <div id="main">
        <asp:ContentPlaceHolder ID="mainContent" runat="server" />
      </div>
      <div id="footer">
        <asp:Literal ID="Footer" runat="server" Text="xxxx" />
      </div>
  </div>
  </form>
</body>
</html>

Open in new window


i tried to use Master.FindControl() but this is not working
 
0
HohlovDimaCommented:
Try this:
Add id to Table that you create dynamically:
tbl.ID = "tableee";

Open in new window

Add the following code:
controlid = "textbox" + counter.ToString();
Table t = PlaceHolder1.FindControl("tableee") as Table;
TextBox tb = t.FindControl(controlid) as TextBox;
tb.Text = "Text";

Open in new window

instead of:
controlid1 = "textbox" + counter1.ToString();
TextBox tb = FindControl(controlid1) as TextBox;

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
asudhaaAuthor Commented:
Thanks ,
 BY making this change it works perfectly now
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.