C#程序报告“索引超出数组范围”

时间:2019-01-26 17:37:49

标签: c# database indexing

请问,如何得到此错误?真是令人困惑。我收到这样的错误,我尝试修复,但它根本不起作用。我只是一个初学者。

namespace WindowsFormsApp1
{
    public partial class Schedule : Form
    {
        public Schedule()
        {
            InitializeComponent();
        }
        MySqlConnection con = new MySqlConnection(@"Data Source=localhost;port=3306;Initial Catalog=Payroll;User Id=root;password=''");
        MySqlDataReader dr;
        int tc = 0;
        private void Schedule_Load(object sender, EventArgs e)
        {
            datagrid();
            fillsched();

        }
        public void datagrid()
        {
            con.Open();
            MySqlDataAdapter sda = new MySqlDataAdapter("Select * from employee where Pstatus='Active'", con);
            DataTable data = new DataTable();
            sda.Fill(data);
            dataGridView1.DataSource = data;
            con.Close();
        }
        public void fillsched()
        {
            con.Open();
            MySqlDataReader dr;
            MySqlCommand cmd = new MySqlCommand("select * from updateschedule ", con);
            dr = cmd.ExecuteReader();

            while (dr.Read())
            {
                int data = dr.GetInt32("empSched");
                comboBox1.Items.Add(data);
            }
            con.Close();
        }
        public void getsched()
        {

            if (Int32.TryParse(comboBox1.SelectedItem.ToString(), out tc))
            {
                con.Open();
                MySqlCommand cmd = new MySqlCommand("select * from updateschedule where empSched=@empSched ", con);
                cmd.Parameters.Add("@empSched", MySqlDbType.Int32).Value = tc;
                dr = cmd.ExecuteReader();

                if (dr.Read())
                {
                    textBox2.Text = dr["TimeIn"].ToString();
                    textBox3.Text = dr["TimeOut"].ToString();
                    label5.Text = tc.ToString();//to pass the data in the combobox1
                }
                con.Close();
            }
        }
        public void view()
        {
            textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            getsched();
        }

        private void dataGridView1_CellMouseClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.RowIndex >= 0)
            {

                view();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            insert();
            insertempsched();
        }
        public void insert()
        {


                MySqlCommand cmd = new MySqlCommand("INSERT INTO schedule (empSchedID,empID,empIN,empOut)VALUES(@empSchedID,@empID,@empIn,@EmpOut)", con);
                cmd.Parameters.Add("@empSchedID", MySqlDbType.Int32).Value = label5.Text;
                cmd.Parameters.Add("@empID", MySqlDbType.VarChar).Value = textBox1.Text;
                cmd.Parameters.Add("@empIn", MySqlDbType.Date).Value = textBox2.Text;
                cmd.Parameters.Add("@empOut", MySqlDbType.VarChar).Value = textBox3.Text;
                execnonquery(cmd, "Data Inserted");

        }
        public void insertempsched()
        {

                con.Open();
                MySqlCommand cmd = new MySqlCommand("Update employee set empSched=empSched where empID=@empID", con);
                cmd.Parameters.Add("@empSchedID", MySqlDbType.Int32).Value = label5.Text;
                cmd.Parameters.Add("@empID", MySqlDbType.VarChar).Value = textBox1.Text;
                cmd.ExecuteNonQuery();
                con.Close();

        }
        public void execnonquery(MySqlCommand sqc, string mymsg)
        {
            con.Open();
            if (sqc.ExecuteNonQuery() == 1)
            {
                MessageBox.Show(mymsg);
            }
            else
            {
                MessageBox.Show("Query not Executed");
            }
            con.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

c#中的“索引在数组的边界之外”始终表示您试图从datagrid或datatables或数组中基于列索引号或行索引号获取值,并且该位置不存在列或行或索引。

我认为您在“视图”方法中存在的以下行出现错误。

textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();

要捕获错误,最好在所有方法中使用try catch块。

您可以像这样修改您的方法

public void view()
{
         try
        {
                 textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
        }
        catch(Exception ex)
        {
                 MessageBox.Show(ex.Message);
        }
}