在Windows窗体中获取异常 [英] getting exception in windows form

查看:80
本文介绍了在Windows窗体中获取异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

例外:

指数超出范围。必须是非负数且小于集合的大小。

参数名称:index



计划:



 私人  void  dataGridView1_CellClick( object  sender,DataGridViewCellEventArgs e)
{
尝试
{
string ccode = dataGridView1.SelectedRows [ 0 ]。单元格[ 0 ]。Value.ToString();
字符串 selectString = 选择名称,持续时间,费用,先决条件来自;
selectString + = ccode =' + ccode + ';
con.Open();
cmd = new SqlCommand(selectString,con);
rdr = cmd.ExecuteReader();

if (rdr.HasRows)
{
while (rdr.Read())
{
textBox1.Text = ccode;
textBox2.Text = rdr [ 0 ]。ToString();
textBox3.Text = rdr [ Duration]。ToString();
textBox4.Text = rdr [ 2 ]。ToString();
textBox5.Text = rdr [ 3 ]。ToString();
}
}

}


catch (Exception ex)
{

MessageBox.Show(ex.Message);
}

最后
{
con.Close();
}

}

解决方案

检查列是否存在



if(row.Table.Columns.Contains(ColName))


使用以下代码,您正在读取索引0,1处的值, 2,列名持续时间。

<前lang =cs> (rdr.Read())
{
textBox1.Text = ccode;
textBox2.Text = rdr [ 0 ]。ToString();
textBox3.Text = rdr [ Duration]。ToString();
textBox4.Text = rdr [ 2 ]。ToString();
textBox5.Text = rdr [ 3 ]。ToString();
}



从错误中可以看出,对于某些索引,读取器中没有该值。

请相应地调试和修改你的代码。


exception:
Index was out of range.Must be non negative and less than the size of collection.
parameter name :index

program:

private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            try
            {
                string ccode = dataGridView1.SelectedRows[0].Cells[0].Value.ToString();
              String   selectString = "select name,duration,fee,prerequesites from ";
                selectString += "courses where ccode = '" + ccode + "'";
                con.Open();
                cmd = new SqlCommand(selectString, con);
                rdr = cmd.ExecuteReader();

                if (rdr.HasRows)
                {
                    while (rdr.Read())
                    {
                        textBox1.Text = ccode;
                        textBox2.Text = rdr[0].ToString();
                        textBox3.Text = rdr["Duration"].ToString();
                         textBox4.Text = rdr[2].ToString();
                        textBox5.Text = rdr[3].ToString();
                    }
                }

                    }


            catch (Exception ex)
            {

                MessageBox.Show(ex.Message);
            }

            finally
            {
                con.Close();
            }

        }

解决方案

Check for column existence with

if (row.Table.Columns.Contains("ColName"))


With the below code, you are reading values at index 0, 1, 2 and a column name "Duration".

while (rdr.Read())
{
    textBox1.Text = ccode;
    textBox2.Text = rdr[0].ToString();
    textBox3.Text = rdr["Duration"].ToString();
    textBox4.Text = rdr[2].ToString();
    textBox5.Text = rdr[3].ToString();
}


From the Error, it is quite clear that for some index the value is not present in the reader.
Please debug and modify your code accordingly.


这篇关于在Windows窗体中获取异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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