索引超出范围Microsoft Access错误

时间:2016-04-22 15:34:44

标签: c# ms-access ado.net windows-forms-designer indexoutofrangeexception

我正在使用C#创建Windows窗体应用程序并连接到Microsoft Access数据库。我的问题是,当我创建一个新记录时,我关闭窗口,然后打开一个不同的窗口,然后在我的数据库中加载新创建的记录。但是我不断得到索引超出范围错误,因为它似乎没有在数据库中及时创建记录以供第二个查询检索它。

我知道创建了记录,它不会及时创建,以便第二个查询检索它。

有人可以帮忙吗?我将在下面发布我的代码:

  conn.Open();
                OleDbDataAdapter da =
                       new OleDbDataAdapter(query, conn);
                OleDbCommandBuilder cmdB =
                       new OleDbCommandBuilder(da);
                da.MissingSchemaAction =
                       MissingSchemaAction.AddWithKey;


                da.Fill(ds, tableName);

                //-------- Update the first Row ---
                DataRow row = ds.Tables["ReservationDatabase"].NewRow();
                row["FirstName"] = textBox1.Text;
                row["LastName"] = textBox2.Text;
                row["EmailAddress"] = textBox3.Text;
                row["GuestStreet"] = textBox4.Text;
                row["GuestCity"] = textBox5.Text;
                row["GuestZip"] = textBox6.Text;
                row["CreditCard"] = textBox7.Text;
                row["ExpDate"] = textBox8.Text;
                row["CVVNumber"] = textBox9.Text;
                row["NumOfGuests"] = numericUpDown1.Value;
                row["CheckInDate"] = dateTimePicker1.Value;
                row["CheckOutDate"] = dateTimePicker2.Value;
                row["SpecialRequests"] = textBox10.Text;
                row["CheckedIn"] = false;
                row["CurrentReservation"] = true;
                ds.Tables["ReservationDatabase"].Rows.Add(row);
                da.Update(ds, "ReservationDatabase");

这是我创建记录的地方

String connectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\Users\Cody Richard\Desktop\Software Engineering\Front Desk Prototype\Reservations.accdb";
        DataSet ds = new DataSet();
        String tableName = "ReservationDatabase";
        string SqlString = String.Format("SELECT LastName, ID, CheckInDate FROM ReservationDatabase WHERE (CurrentReservation=true);");
        OleDbConnection conn = new OleDbConnection(connectionString);
        try
        {
            conn.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(SqlString, conn);
            da.Fill(ds, tableName);
            label5.Text = ds.Tables["ReservationDatabase"].Rows[1]["ID"].ToString();
            ReservationID = int.Parse(ds.Tables["ReservationDatabase"].Rows[1]["ID"].ToString());
           label6.Text = ds.Tables["ReservationDatabase"].Rows[1]["LastName"].ToString();
           label7.Text = ds.Tables["ReservationDatabase"].Rows[1]["CheckInDate"].ToString();

这是我用查询调用该记录并将其设置为等于Windows窗体上的标签的代码。

如果您想看到其他任何内容,请与我们联系。谢谢!

0 个答案:

没有答案