C#在第二次单击后无法保存到数据库

本文关键字:保存 数据库 第二次 单击 | 更新日期: 2023-09-27 18:20:49

所以我的程序正常工作。当我单击保存按钮时,所有内容都会保存到数据库中。然而,当我第二次点击保存按钮时,它无法工作索引超出了数组的界限是私有void中的错误btnTest_click on line string Ullage=splitstring[2];

  private void btnSave_Click(object sender, EventArgs e)
    {
        OleDbConnection conn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:'2014-06-26 TEK687 Config Tool'TEK687 Config Tool'bin'Debug'Results.mdb");
        string SqlString = "insert into Table1  (PassOrFail,DateTested,TekNum,BatchNum,WeekNum,Serial,FirmwareVer,HardwareVer,TestPC,SettingsTest,Deleted,Ullage,SRC,Vbatt,Tamb,Ullage2,SRC2,Vbatt2,Tamb2) Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
        using (OleDbCommand cmd = new OleDbCommand(SqlString, conn))
        {
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("PassOrFail", txtPassFail.Text);
            cmd.Parameters.AddWithValue("DateTested", txtDateTested.Text);
            cmd.Parameters.AddWithValue("TekNum", txtTekPartNo.Text);
            cmd.Parameters.AddWithValue("BatchNum", txtBatchNumber.Text);
            cmd.Parameters.AddWithValue("WeekNum", txtWeekYearNo.Text);
            cmd.Parameters.AddWithValue("Serial", txtSerialNumber.Text);
            cmd.Parameters.AddWithValue("FirmwareVer", txtFirmwareVer.Text);
            cmd.Parameters.AddWithValue("HardwareVer", txtHardwareVer.Text);
            cmd.Parameters.AddWithValue("TestPC", txtTestPC.Text);
            cmd.Parameters.AddWithValue("SettingsTest", cboSettingsProfiles.SelectedIndex);
            cmd.Parameters.AddWithValue("Deleted", txtDeleted.Text);
            cmd.Parameters.AddWithValue("Ullage", txtUllage1.Text);
            cmd.Parameters.AddWithValue("SRC", txtSRC1.Text);
            cmd.Parameters.AddWithValue("Vbatt", txtVbatt1.Text);
            cmd.Parameters.AddWithValue("Tamb", txtTamb1.Text);
            cmd.Parameters.AddWithValue("Ullage2", txtUllage2.Text);
            cmd.Parameters.AddWithValue("SRC2", txtSRC2.Text);
            cmd.Parameters.AddWithValue("Vbatt2", txtVbatt2.Text);
            cmd.Parameters.AddWithValue("Tamb2", txtTamb2.Text);

            conn.Open();
            cmd.ExecuteNonQuery();
            MessageBox.Show("Data stored successfully");
        }
        ClearTextBoxes(this);
    }

以下是向数据库添加值的代码。错误位于string Ullage=splitstring[2]

 private void btnTest_Click(object sender, EventArgs e)
    {
        Cancel = false;
        if (DiscoverDevice(false) == false)
        {
            MessageBox.Show("Error, device is not responding");
            return;
        }
        string servo = "";
        MyModemSerialInterfaceLayer.WriteTextAndWaitForResponse("TL,S," + txtToolID.Text + ",30,'r'n", "'r'n", 4, ref servo, 500);
        string test = "";
        MyModemSerialInterfaceLayer.WriteTextAndWaitForResponse("TL,T," + txtToolID.Text + "'r'n", "'r'n", 4, ref test, 500);
        string[] splitstring = test.Split(',');
        **string Ullage = splitstring[2];**
        string SRC = splitstring[3];
        string RSSI = splitstring[4];
        string AlarmStatus = splitstring[5];
        string RateofChange = splitstring[6];
        string Tamb = splitstring[7];
        string Vbatt = splitstring[8];
        Ullage = Ullage.Replace("u:", "");
        Tamb = Tamb.Replace("t:", "");
        Vbatt = Vbatt.Replace("v:", "");
        int Ull = Convert.ToInt32(Ullage);
        int src = Convert.ToInt32(SRC);
        int BV = Convert.ToInt32(Vbatt);
        int temp = Convert.ToInt32(Tamb);
        StringBuilder sb = new StringBuilder();
        sb.Append(Ull);
        txtUllage1.Text += sb.ToString();
        StringBuilder sc = new StringBuilder();
        sc.Append(src);
        txtSRC1.Text += sc.ToString();
        StringBuilder sd = new StringBuilder();
        sd.Append(BV);
        txtVbatt1.Text += sd.ToString();
        StringBuilder se = new StringBuilder();
        se.Append(temp);
        txtTamb1.Text += se.ToString();
        StringBuilder sm = new StringBuilder();

        if ((Ull < 11) || (Ull > 13) || (src < 9) || (BV < 29))
        {
            txtPassFail.Text += "12cm FAIL";
              txtResultsReading.Font = new Font(txtResultsReading.Font, FontStyle.Bold);
            StringBuilder th = new StringBuilder();
               th.Append("12cm FAIL");
              txtResultsReading.Text += th.ToString();
        }
        string servoTwo = "";
        MyModemSerialInterfaceLayer.WriteTextAndWaitForResponse("TL,S," + txtToolID.Text + ",31,'r'n", "'r'n", 4, ref servoTwo, 500);
        string tests = "";
        MyModemSerialInterfaceLayer.WriteTextAndWaitForResponse("TL,T," + txtToolID.Text + "'r'n", "'r'n", 4, ref tests, 500);

        string[] splitstrings = tests.Split(',');
        string Ullage2 = splitstrings[2];
        string SRC2 = splitstrings[3];
        string RSSI2 = splitstrings[4];
        string AlarmStatus2 = splitstrings[5];
        string RateofChange2 = splitstrings[6];
        string Tamb2 = splitstrings[7];
        string Vbatt2 = splitstrings[8];
        Ullage2 = Ullage2.Replace("u:", "");
        Tamb2 = Tamb2.Replace("t:", "");
        Vbatt2 = Vbatt2.Replace("v:", "");
        int Ull2 = Convert.ToInt32(Ullage2);
        int src2 = Convert.ToInt32(SRC2);
        int BV2 = Convert.ToInt32(Vbatt2);
        int temp2 = Convert.ToInt32(Tamb2);
        StringBuilder sf = new StringBuilder();
        sf.Append(Ull2);
        txtUllage2.Text += sf.ToString();
        StringBuilder sg = new StringBuilder();
        sg.Append(src2);
        txtSRC2.Text += sg.ToString();
        StringBuilder sh = new StringBuilder();
        sh.Append(BV2);
        txtVbatt2.Text += sh.ToString();
        StringBuilder si = new StringBuilder();
        si.Append(temp2);
        txtTamb2.Text += si.ToString();

        txtDateTested.Text = DateTime.Now.ToString();
        txtTestPC.Text = System.Environment.UserName;
        //boSettingsProfiles.Text= Settings.SettingsFile;
        txtDeleted.Text = "CURRENT";
        if ((Ull < 11) || (Ull > 13) || (src < 9) || (BV < 29))
        {
            txtPassFail.Text += "  3M FAIL";
            txtResultsReading.Font = new Font(txtResultsReading.Font, FontStyle.Bold);
            StringBuilder tg = new StringBuilder();
            tg.Append(Environment.NewLine);
            tg.Append("3M FAIL");
            txtResultsReading.Text += tg.ToString();
        }
        else if ((Ull >= 11) && (Ull <= 13) && (src == 9) && (BV >= 29) &&
            (Ull2 >= 11) && (Ull2 <= 13) && (src2 == 9) && (BV2 >= 29))
        {
            txtPassFail.Text += "PASS";
            txtResultsReading.Font = new Font(txtResultsReading.Font, FontStyle.Bold);
            txtResultsReading.Text += "PASS";
        }
        ReleaseDevice();//release the config tool after config is completed
    }
}

C#在第二次单击后无法保存到数据库

splitstrings对象中没有三个对象,因此无法访问索引2处的对象。因此,在访问之前,您应该实现一个检查,以确保它在该索引中包含一个对象,然后在不合适的情况下进行处理。

顺便说一句,下次你问为什么有些东西不起作用时,告诉我们你得到的异常是绝对重要的,而不是让我们要求。你需要包括引发异常的代码。请参阅如何创建最小、完整和可验证的示例。

检查来自WriteTextAndWaitForResponse调用的变量。它可能没有3个逗号。