Я пытаюсь загрузить и извлечь время секундомера из базы данных SQL Server с кодом, показанным ниже, но я получаю ошибку:
"System.IndexOutOfRangeException: 'currentTimeVASN".
Код работает нормально, если я удаляю код для загрузки и извлечения текста секундомера в базу данных
Я разместил код ниже.
Ценю вашу помощь.
public partial class TestWindowMLCC : Window
{
String SNconnectionstring = (@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\SNDB.mdf;Integrated Security=True;");
DispatcherTimer dtVASN = new DispatcherTimer();
Stopwatch swVASN = new Stopwatch();
string currentTimeVASN = string.Empty;
public TestWindowMLCC()
{
InitializeComponent();
}
private void mlccSNCreate_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection sqlConSN = new SqlConnection(SNconnectionstring))
{
sqlConSN.Open();
SqlCommand sqlCmdSN = new SqlCommand("SNadd", sqlConSN);
sqlCmdSN.CommandType = System.Data.CommandType.StoredProcedure;
sqlCmdSN.Parameters.AddWithValue("@SerialNumber", sNtxtbox.Text);
sqlCmdSN.Parameters.AddWithValue("@Test1", SNTest1.Text.Trim());
sqlCmdSN.Parameters.AddWithValue("@Test2", txtVASN.Text.Trim());
sqlCmdSN.ExecuteNonQuery();
MessageBox.Show("SN has been created! You may proceed to key in the data!");
}
}
private void mlccOpen_Click(object sender, RoutedEventArgs e)
{
using (SqlConnection sqlConSN = new SqlConnection(SNconnectionstring))
{
sqlConSN.Open();
String sqlSelectQuerySN = ("Select * FROM tblSN WHERE SerialNumber = @serialNumber");
SqlCommand sqlCmdSNLoad = new SqlCommand(sqlSelectQuerySN, sqlConSN);
sqlCmdSNLoad.Parameters.Add("@serialNumber", System.Data.SqlDbType.VarChar).Value = sNtxtbox.Text;
SqlDataReader drSNReader = sqlCmdSNLoad.ExecuteReader();
if (drSNReader.Read())
{
SNTest1.Text = (drSNReader["Test1"].ToString());
txtVASN.Text = (drSNReader["currentTimeVASN"].ToString());
}
else
{
SNTest1.Text = "";
txtVASN.Text = "";
MessageBox.Show("The SN you keyed in is not exist. Please click Create Button to Create.");
}
}
}
private void sNbtn_Click(object sender, RoutedEventArgs e)
{
mLCCTabControl.SelectedIndex = 1;
dtVASN.Tick += new EventHandler(dtVASN_Tick);
dtVASN.Interval = new TimeSpan(0, 0, 0, 0, 1);
}
void dtVASN_Tick(object sender, EventArgs e)
{
if (swVASN.IsRunning)
{
TimeSpan tsVASN = swVASN.Elapsed;
currentTimeVASN = String.Format("{0:00}:{1:00}:{2:00}",
tsVASN.Hours, tsVASN.Minutes, tsVASN.Seconds);
txtVASN.Text = currentTimeVASN;
}
}
private void btnTimeStartSN_Click(object sender, RoutedEventArgs e)
{
swVASN.Start();
dtVASN.Start();
}
private void btnTimeStopSN_Click(object sender, RoutedEventArgs e)
{
if (swVASN.IsRunning)
{
swVASN.Stop();
}
}
}
}
Вот содержимое SNAdd SP:
CREATE PROC SNadd @SerialNumber VARCHAR (50)
, @Test1 VARCHAR (50)
, @currentTimeVASN VARCHAR (50)
AS
INSERT INTO tblSN(SerialNumber,Test1,currentTimeVASN) VALUES (@SerialNumber,@Test1,@currentTimeVASN)