Click here to Skip to main content
15,908,907 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi i have two page one MasterPage.master and default
i think this error for two table[0]
when run defualt.aspx this error see:
HTML
 Specified argument was out of the range of valid values.
Parameter name: index
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.
Parameter name: index

Source Error:


Line 88:             tbl.BorderWidth = 0;
Line 89:             tbl.Attributes.Add("Style", "text-align:right");
Line 90:             ImageButton ButtonPolls = (ImageButton)tbl.Controls[0];
Line 91:             ButtonPolls.ImageUrl = "../images/poll/CastVote.jpg";
Line 92:                     

this code use MasterPage.master
C#
string strSQL = "select QuestionText from TPollQuestions where Iscurrent=1 and Isarchived=0";
          string cmdtext = "";
          SqlConnection conn = Conn;
          Pollcontrol1.CanVote = true;

          if (conn.State == System.Data.ConnectionState.Closed)
              conn.Open();

          cmdtext = "select QuestionText from TPollQuestions where Iscurrent=1 and Isarchived=0";
          cmd = new SqlCommand(cmdtext, conn);
          Pollcontrol1.PollQuestion = cmd.ExecuteScalar().ToString();
          conn.Close();
          cmdtext =
              "select optionID,PollID,OptionText,Votes from TPollOptions where pollID in(select PollID from TPollquestions where Iscurrent=1 and Isarchived=0)";
          SqlDataAdapter da = new SqlDataAdapter(cmdtext, conn);
          DataTable dt = new DataTable();
          da.Fill(dt);

          for (int i = 0; i < dt.Rows.Count; i++)
          {
              Pollcontrol1.AddPollAnswer(Convert.ToInt32(dt.Rows[i]["pollID"]), Convert.ToInt32(dt.Rows[i]["optionID"]), dt.Rows[i]["optionText"].ToString(), Convert.ToInt32(dt.Rows[i]["votes"]));
          }

          TableCell tbl = (TableCell)Pollcontrol1.Controls[0].Controls[Pollcontrol1.Controls[0].Controls.Count - 1].Controls[0];
          tbl.BorderWidth = 0;
          tbl.Attributes.Add("Style", "text-align:right");
          ImageButton ButtonPolls = (ImageButton)tbl.Controls[0];
          ButtonPolls.ImageUrl = "../images/poll/CastVote.jpg";

and this code in default.aspx
C#
PagedDataSource pgsource = new PagedDataSource();
    int findex, lindex;
    DataRow dr1;
    static string str = "select *  from TNews where 1=1";
    protected void Page_Load(object sender, EventArgs e)
    {
   
        if (!IsPostBack)
        {
            //CurrentPage = 0;
     GetData(str);
        }
    }
   
    DataTable GetData(string str)
    {
        DataTable dtable1= new DataTable();

        SqlConnection Conn;
        SqlCommand Cmd;

        Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["bakerConnectionString"].ToString());
        Cmd = new SqlCommand();
        Conn.Open();
        Cmd.Connection = Conn;

        Cmd.CommandText = str;

        SqlDataAdapter dap1= new SqlDataAdapter(Cmd);
        DataSet ds1 = new DataSet();
        dap1.Fill(ds1, "ds1");
        pgsource.DataSource = ds1.Tables[0].DefaultView;
        DataBind();
        return ds1.Tables[0];
    }
Posted

whenever you are trying to access an index from a collection pls validate it..
if you are not getting a valid result , then apply a break point to the collection object and check for the valid index.

C#
if (tbl.Controls.Count >= 1)
               if (tbl.Controls[0] is ImageButton)
               {
                   ImageButton ButtonPolls = tbl.Controls[0] as ImageButton;
                   ButtonPolls.ImageUrl = "../images/poll/CastVote.jpg";
               }
 
Share this answer
 
Comments
saeed1364 7-Jan-14 2:12am    
thanks
when trace this code t bl.Controls.Count is zero
and sloved problem
Karthik_Mahalingam 7-Jan-14 2:58am    
ok, Please close this post.
if this helps, pls mark it as answer,
or if u have answer, post the same as solution and mark that as answer..
Quote:
Specified argument was out of the range of valid values.
Parameter name: index
The Exception may be due to the following line.
Line 90:             ImageButton ButtonPolls = (ImageButton)tbl.Controls[0];

While debugging check if there are any Controls in tbl object.

I guess you don't have any Controls and when you are trying to get the 0th index Control, it is throwing Exception.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900