索引超出范围错误 [英] Index out Of range Error

查看:121
本文介绍了索引超出范围错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的专家我正在创建一个窗口应用程序,其中即时通讯在我尝试在字段中填充数据表值时发现以下索引错误: -



代码:



Dear Experts I m Creatting a window application in which i m finding the following error of indexing while i m trying to fatch data table value in fields : -

Code :

private void dgEmployee_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {
            if (dgEmployee.Rows.Count > 0)
            {
                String EmpId = "";
                if (dgEmployee.CurrentRow.Cells[0].Value != null)
                {
                    EmpId = dgEmployee.CurrentRow.Cells[0].Value.ToString();
                    Friends.con.Open();
                    string str = "Select * From Admin Where EmployeeID = '" + dgEmployee.CurrentRow.Cells[0].Value.ToString() + "' ";
                    OleDbCommand cmd = new OleDbCommand(str, Friends.con);
                    OleDbDataAdapter adp = new OleDbDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adp.Fill(dt);
                    Friends.con.Close();
                    if (dt.Rows.Count > 0)
                    {
                        tbEployeeId.Text = dt.Rows[0][1].ToString();
                        tbEmpName.Text = dt.Rows[0][2].ToString();
                        cbEmpDept.SelectedItem = dt.Rows[0][3].ToString();
                        cbEmpDesig.SelectedItem = dt.Rows[0][4].ToString();
                        tbAdd1.Text = dt.Rows[0][5].ToString();
                        tbAdd2.Text = dt.Rows[0][6].ToString();
                        DtpDob.Text = dt.Rows[0][7].ToString();
                        DtpDoj.Text = dt.Rows[0][8].ToString();
                        
                        tbCity.Text = dt.Rows[0][9].ToString();
                        tbDistrict.Text = dt.Rows[0][10].ToString();
                        tbPinCode.Text = dt.Rows[0][11].ToString();
                        tbState.Text = dt.Rows[0][12].ToString();
                        tbCountry.Text = dt.Rows[0][13].ToString();
                        tbTelephone.Text = dt.Rows[0][14].ToString();
                        tbMobile.Text = dt.Rows[0][15].ToString();
                        tbEmail.Text = dt.Rows[0][16].ToString();
                        tbPan.Text = dt.Rows[0][17].ToString();
                        tbTan.Text = dt.Rows[0][18].ToString();
                        tbSalary.Text = dt.Rows[0][19].ToString();
                        tbSWA.Text = dt.Rows[0][20].ToString();
                        tbTHoliday.Text = dt.Rows[0][21].ToString();
                        tbEHolidays.Text = dt.Rows[0][22].ToString();
                        tbBonusDays.Text = dt.Rows[0][23].ToString();
                        cbWorkCreater.SelectedItem = dt.Rows[0][24].ToString();
                        cbAdministration.SelectedItem = dt.Rows[0][25].ToString();

                        //DateTime selectdate = Convert.ToDateTime(DtpDoj.Value);
                        //DtpDoj.Value = dt.Rows[0][5].ToString();
                    }
                }
                tbEployeeId.Enabled = false;
                tbEmpName.Enabled = false;
                cbEmpDesig.Enabled = false;
                cbEmpDept.Enabled = false;

                groupBox2.Enabled = true;
                BtnAdd.Text = "Save";
                BtnModify.Text = "Cancel";
                BtnDelete.Enabled = false;
               
            }
        }



当我们在点击网格视图填充网格视图后运行此应用程序时,它会显示索引和错误一件事日期时间选择器也没有找到我存储在数据库中的对象时间。我存储datetimevalue类型是文本。



plz给我解决方案



代码块添加 - OriginalGriff [/ edit]


When We run this application after fill the gridview on click the grid view it show the error of indexing and one thing date time picker also not found the adject time which i store in database . i store datetimevalue type is text .

plz give me the solution

[edit]Code block added - OriginalGriff[/edit]

推荐答案

在不知道哪一行引发异常的情况下 - 错误会给出这个,或者调试器将停止在线 - 我们无法帮助那么多,但是最明显的问题可能是你使用魔术数字来访问你的DataTable。我注意到你开始索引为1,而不是零 - 这是否意味着你可能有一个一个一个错误,并且没有从数据库返回的第25列?



无论如何,我强烈推荐三种变化:

1)不要连接字符串来构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。

2)不要使用SELECT *访问数据库 - 指定要按名称接收的列。它更有效,因为它不返回不必要的列,并且它确保在更改表并删除列时返回合适的错误消息。它还指定了返回列的顺序,如果要使用数字索引,这是必不可少的。

3)停止使用数字索引!使用文本索引 - 它使您的代码更具可读性和可靠性。



这些更改很可能会消除您的问题,但如果他们不这样做然后使用调试器检查抛出错误的行,并准确告诉我们它是哪一行!
Without knowing which line throws the exception - and the error will give this, or the debugger will stop on the line - we can't help that much, but the the most obvious problem is probably your use of "magic numbers" to access your DataTable. I notice that you start indexing at 1, not zero - does that mean that you possibly have an off-by-one error, and there is no column 25 returned from the DB?

Whatever, there are three changes I would strongly recommend:
1) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
2) Don't use "SELECT *" to access your database - specify the columns you want to receive by name. It's more efficient, in that it doesn't return unnecessary columns, and it ensures that a suitable error message is returned if the table is changed and a column removed. It also specifies the order in which columns are returned which is essential if you want to use numeric indexes.
3) Stop using numeric indexes! Use text indexes instead - it makes your code a lot more readable, and reliable.

It's quite likely that these changes will remove your problem, but if they don't then use the debugger to examine the line that throws the error, and tell us exactly which one it is!


这篇关于索引超出范围错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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