索引超出了该档案的范围。 [英] Index was outside the bounds of the arrary.

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

问题描述

大家好,



i正在处理我希望根据某些条件在数据库中进行更改的Web应用程序。我的代码如下所示.. < br $> b $ b

Hello everyone,

i am working with the web application in which i want to make changes in database based on some condition.my code is as below..

protected void Page_Load(object sender, EventArgs e)
        {

           
            if (DateTime.Now.ToString("dd") == "1")
            {
            SqlConnection conn = new SqlConnection();
            conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"].ConnectionString;
            conn.Open();
            SqlCommand cmd = new SqlCommand("select count(*) from leave_bal", conn);
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.Read())
            {

                int l = dr.GetInt32(0);
                dr.Close();

                for (int i = 0; i < l; i++)
                {
                    SqlCommand cmd2 = new SqlCommand("select name from Leave_bal", conn);
                    SqlDataReader dr2 = cmd2.ExecuteReader();
                    if (dr2.Read())
                    {

                        string temp = dr2.GetString(i);
                        dr2.Close();
                        DateTime daten = DateTime.Now;
                        SqlCommand cmd_gdate = new SqlCommand("select jdate from emp_details where name='" + temp + "'", conn);
                        SqlDataReader dr3 = cmd_gdate.ExecuteReader();
                        if (dr3.Read())
                        {

                            DateTime datej = Convert.ToDateTime(dr3.GetValue(0));
                            dr3.Close();

                            System.TimeSpan span = daten - datej;
                            int diff = (int)span.TotalDays;
                            if (diff >= 365)
                            {
                                SqlCommand acc = new SqlCommand("update leave_bal set cl=(cl+1.5) where name='" + temp + "'", conn);
                                acc.ExecuteNonQuery();
                            }
                            else
                            {
                                SqlCommand acc = new SqlCommand("update leave_bal set cl=(cl+1) where name='" + temp + "'", conn);
                                acc.ExecuteNonQuery();
                            }
                        }

                    }
                }


            }

           }
        }



,首先我要检查今天的日期是否为1,如果是的话,我正在使用for循环,它迭代数据库中的总记录。每次我获得员工姓名时,相应地更改表leave_bal中的值''cl''。问题在于,它适用于for循环的第一次迭代,即leave_bal表中的第一个记录,但是当它涉及到第二次迭代,它给出了我的code.how中突出显示的行索引超出数组范围的错误,以实现这一目标吗?任何帮助都会非常适合。


in the above code,first i am checking if today''s date is 1st,if so i am using for loop which iterates for the total records in the database. each time i am getting employee name an changing the value ''cl'' in table leave_bal accordingly.the problem is that,it works for the first iteration of for loop,that is for first record in leave_bal table,but when it comes to second iteration,it gives me the error that Index was outside the bounds of the array on the line highlighted in my code.how to accomplish this?any help would be greatly appriciated.

推荐答案

更新数据库的奇怪方式!



我会将此写入存储过程,但这只是方式我工作。



使用c#我会做以下事情。



使用数据表而不是阅读器并将记录恢复到应用程序



从表中选择名称



然后使用foreach和循环通过数据表



Odd way of updating the database!

I would have written this into a stored proc, but that is just the way I work.

Using c# I would do the following.

Use a Datatable instead of a reader and get the records back into the aplication

Select Name from Table

then use a foreach and loop through the datatable

foreach(datarow dr in datatable.rows)
{
//Update the database using the column indexer from the row dr["Name"]
}


中的列索引器我猜测错误是at



I''m guessing the error is at

string temp = dr2.GetString(i);







这里你使用的是i,这是的计数返回行,作为您正在处理的数据行中集合的索引。



我怀疑你想要








Here you are using i, which is the count of the rows returned, as an index into the collection of columns in the data row you are dealing with.

I suspect you want


string temp = dr2.GetString(XXXXX);





其中XXXXX是你想要的列的索引



where XXXXX is the index to the column you want


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

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