I am trying to check 2 different datatable to see if the workorder exist. . Both the datatables have the same columns with different table name [c# barcode] and [c1 barcode]. The first check if there is the same workorder at [c# barcode]is working ,however i am getting the error No value in 1 or more parameter for the second check[c1 barcode] if it exist at the executescalar code.
private void save_Click(object sender, EventArgs e)
{
if (textBox1.Text == "")
{
MessageBox.Show("No data, Please scan workorder");
}
else
{
//Checking if workorder exist in main database
connection.Open();
OleDbCommand checkrecord = new OleDbCommand("SELECT COUNT(*) FROM [c# barcode] WHERE ([c# barcode].[Workorder] = @workorder)", connection);
checkrecord.Parameters.AddWithValue("@workorder", textBox1.Text);
int recordexist = (int)checkrecord.ExecuteScalar();
if (recordexist > 0)
{
textBox1.Clear();
MessageBox.Show("Workorder exist in main database");
connection.Close();
//load after every scan
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string comparequery = "SELECT * from [c# barcode]; ";
command.CommandText = comparequery;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt6 = new DataTable();
da.Fill(dt6);
dataGridView1.DataSource = dt6;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
}
else
{
//checking if record exisit in facility database
OleDbCommand checkrecord1 = new OleDbCommand("SELECT COUNT(*) FROM [c1 barcode] WHERE ([c1 barcode].[Workorder] = @workorder)", connection);
checkrecord.Parameters.AddWithValue("@workorder", textBox1.Text);
int recordexist1 = (int)checkrecord1.ExecuteScalar();//error no value given for 1 or more parameter
if (recordexist1 > 0)
{
textBox1.Clear();
MessageBox.Show("Workorder exist in facility database");
connection.Close();
//load after every scan
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string comparequery = "SELECT * from [c1 barcode]; ";
command.CommandText = comparequery;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt6 = new DataTable();
da.Fill(dt6);
dataGridView1.DataSource = dt6;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
}
else
{ //inserting workorder if it does not exist
string cmdText = "INSERT INTO [c1 barcode] ([Workorder],[Close for handover],[Name handover]) VALUES (@workorder,@Close,@name)";
using (OleDbCommand cmd = new OleDbCommand(cmdText, connection))
{
cmd.Parameters.AddWithValue("@workorder", textBox1.Text);
cmd.Parameters.AddWithValue("@Close", DateTime.Now.ToString("d/M/yyyy"));
cmd.Parameters.AddWithValue("@name", label6.Text);
if (cmd.ExecuteNonQuery() > 0)
{
textBox1.Clear();
//load after every scan
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string comparequery = "SELECT * from [c1 barcode]; ";
command.CommandText = comparequery;
OleDbDataAdapter da = new OleDbDataAdapter(command);
DataTable dt6 = new DataTable();
da.Fill(dt6);
dataGridView1.DataSource = dt6;
dataGridView1.FirstDisplayedScrollingRowIndex = dataGridView1.RowCount - 1;
}
else
{
textBox1.Clear();
MessageBox.Show("Please rescan");
connection.Close();
}
connection.Close();
}
}
}
}
}