I have generated an array of required field validators using following code:

 Panel[] divMain = new Panel[22];
 DropDownList[] gender = new DropDownList[22];
 TextBox[] txtFirstName = new TextBox[22];
 TextBox[] txtMiddleName = new TextBox[22];
 TextBox[] txtLastName = new TextBox[22];
 TextBox[] txtAge = new TextBox[22];
 RequiredFieldValidator[] req = new RequiredFieldValidator[30];

and i am creating some dynamic controls using following code:

     for (int i = 0; i < noOfad - 1; i++)

            HtmlGenericControl p = new HtmlGenericControl("p");
            HtmlGenericControl strong = new HtmlGenericControl("strong");
            strong.InnerText = "Adult" + Convert.ToString(i + 2);
            Panel div = new Panel();
            HtmlGenericControl p1 = new HtmlGenericControl("p");
            p1.InnerHtml = "<span><strong>Full Name (As per Valid Govt Id Proof valid )</strong></span> <span><strong style='  margin-left: 549px;'>Age(in Years 18 +) </strong></span>";

            HtmlGenericControl p2 = new HtmlGenericControl("p");
            gender[i] = new DropDownList();
            gender[i].Items.Add(new ListItem("Title", "0"));
            gender[i].Items.Add(new ListItem("Mr.", "1"));
            gender[i].Items.Add(new ListItem("Mrs.", "2"));
            gender[i].Items.Add(new ListItem("Ms.", "3"));
            gender[i].CssClass = "txt";

            txtFirstName[i] = new TextBox();
            txtFirstName[i].Attributes.Add("placeholder", "First Name");
            txtFirstName[i].CssClass = "txt input";
            txtFirstName[i].ID = "txtFirstName" + Convert.ToString(i);
            req[i].ControlToValidate = txtFirstName[i].ID;//Object refrence not set to an instance ofo an object
            req[i].ForeColor = System.Drawing.Color.Red;
            req[i].ErrorMessage = "*";
            txtFirstName[i].CssClass = "input txt";
            txtMiddleName[i] = new TextBox();
            txtMiddleName[i].Attributes.Add("placeholder", "Middle Name");
            txtMiddleName[i].CssClass = "txt input";
            txtMiddleName[i].CssClass = "input txt";
            txtMiddleName[i].ID = "txtMiddleName" + Convert.ToString(i);
            txtLastName[i] = new TextBox();
            txtLastName[i].Attributes.Add("placeholder", "Last Name");
            txtLastName[i].CssClass = "txt input";
            txtLastName[i].CssClass = "input txt";
            txtLastName[i].ID = "txtLastName" + Convert.ToString(i);
            txtAge[i] = new TextBox();
            txtAge[i].Attributes.Add("placeholder", "Enter Age");
            txtAge[i].CssClass = "txt";





But when i am assigning the control to validate to the validator req[i] then there comes an error Object reference not set to an instance of an object. Can not understand why this problem is occuring. Any help to correct my code! Thanks

Related posts

Recent Viewed