所以我的程序应该正常工作。单击“保存”按钮时,所有内容都将保存到数据库中。但是,当我第二次单击保存按钮时,它无法工作。索引超出了数组的范围。是私有空的错误btnTest_click在行字符串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);
}
以下是向数据库错误添加值的代码位于字符串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
}
}
答案 0 :(得分:3)
您的splitstrings
对象中没有三个对象,因此您无法访问索引2处的对象。因此,您应该在访问之前执行检查以确保它包含该索引处的对象,然后处理不合适的情况。
顺便说一句,下次你问一个关于为什么某些东西不起作用的问题时,你告诉我们你得到的异常绝对是至关重要的,而不是让我们要求它。并且您需要包含引发异常的代码。请参阅How to create a Minimal, Complete, and Verifiable example。
答案 1 :(得分:1)
检查来自WriteTextAndWaitForResponse调用的变量。它可能没有3个逗号。