如何解决这个有效值的问题范围。参数名称:index [英] how sloved this problem range of valid values. Parameter name: index

查看:114
本文介绍了如何解决这个有效值的问题范围。参数名称:index的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi
i有两个页面一个MasterPage.master和默认

i认为这个错误为两个表[0]

运行时defualt.aspx这个错误见:

指定的参数超出了有效值的范围。 
参数名称:index
描述:执行当前Web请求期间发生未处理的异常。请查看堆栈跟踪以获取有关错误及其源自代码的位置的更多信息。

异常详细信息:System.ArgumentOutOfRangeException:指定的参数超出有效值范围。
参数名称:index

来源错误:


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



此代码使用MasterPage.master

  string  strSQL =  从TPollQuestions中选择QuestionText,其中Iscurrent = 1且Isarchived = 0< /跨度>; 
string cmdtext = ;
SqlConnection conn = Conn;
Pollcontrol1.CanVote = true ;

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

cmdtext = 从TPollQuestions中选择QuestionText,其中Iscurrent = 1且Isarchived = 0;
cmd = new SqlCommand(cmdtext,conn);
Pollcontrol1.PollQuestion = cmd.ExecuteScalar()。ToString();
conn.Close();
cmdtext =
选择optionID,PollID,OptionText,从TPollOptions投票,其中pollID在(选择来自TPollquestions的PollID,其中Iscurrent = 1且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] [ 投票]));
}

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



和default.aspx中的此代码

 PagedDataSource pgsource =  new  PagedDataSource(); 
int findex,lindex;
DataRow dr1;
static string str = 从TNews中选择*,其中1 = 1;
受保护 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 ];
}

解决方案

每当您尝试从集合中访问索引时请验证它..

如果你没有得到有效的结果,那么给集合对象应用一个断点并检查有效的索引。



  if (tbl.Controls.Count >  =  1 
if (tbl.Controls [ 0 ] ImageButton)
{
ImageButton ButtonPolls = tbl.Controls [ 0 ] as ImageButton;
ButtonPolls.ImageUrl = ../ images / poll / CastVote.jpg;
}


Quote:

指定的参数不在有效值的范围。

参数名称:index

异常可能是由于以下行。

第90行:ImageButton ButtonPolls =(ImageButton)tbl.Controls [0]; 



调试时检查是否有 tbl 对象中控制



我猜你没有任何控件当你试图获得第0个索引 Control 时,它会抛出 Exception


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:

 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

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

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];
    }

解决方案

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.

if (tbl.Controls.Count >= 1)
               if (tbl.Controls[0] is ImageButton)
               {
                   ImageButton ButtonPolls = tbl.Controls[0] as ImageButton;
                   ButtonPolls.ImageUrl = "../images/poll/CastVote.jpg";
               }


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.


这篇关于如何解决这个有效值的问题范围。参数名称:index的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆