Reputation: 363
I have a C# application that submits information to an Access database. I fill in three required boxes , and click "Submit". The script should respond in the following manner when the use clicks the button:
Step 1. Look into database Table A, to see if the value in textBox1 exists in the database.
Step 2. If the value exists, add the values of textBox1, textBox2, and textBox3 into database Table B columns, respectively.
Step 3. If any one of the three text boxes are left blank, display a message.
Step 4. If the value in textBox1 is not in the database table, display a message. (eventually, I plan the replace the message with a default population of the database fields)
THE PROBLEM: When I run the program, in any of the above cases, the result is Step number 4 above. It seems to skip the first "if" statement, and jumping right to the "else" outcome.
Any help resolving this, would be greatly appreciated! The "Private Void "code is below. Thanks in advance.
private void button1_Click(object sender, EventArgs e)
{
OleDbCommand cmd = new OleDbCommand("select * from script_Orders where cust_Name = @UserID", vcon);
OleDbParameter param = new OleDbParameter();
param.ParameterName = "@UserID";
param.Value = textBox1.Text;
cmd.Parameters.Add(param);
OleDbDataReader reader = cmd.ExecuteReader();
{
if (reader.HasRows)
{
if (textBox1.Text == "" || textBox2.Text == "" || textBox3.Text == "")
{
MessageBox.Show("You must fill in all fields.");
return;
}
else
{
OleDbCommand dbCommand;
OleDbDataReader dbReader;
new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;data source=C:\Search\Database.accdb");
dbCommand = new OleDbCommand("select count(*) as Record_Count from script_received", vcon);
dbReader = dbCommand.ExecuteReader();
if (dbReader.Read() == true)
rowCount = dbReader["Record_Count"].ToString();
else
return;
var date = DateTime.Now.ToString("MM/dd/yyyy");
{
using (OleDbCommand command = new OleDbCommand("INSERT INTO script_Received (script, qty, emp_id, received_Date) VALUES (@script,@qty,@emp_Id,@rec_date)"))
{
command.CommandType = CommandType.Text;
command.Parameters.Add("@script", OleDbType.Integer).Value = textBox1.Text;
command.Parameters.Add("@qty", OleDbType.VarChar).Value = textBox2.Text;
command.Parameters.Add("@emp_id", OleDbType.VarChar).Value = textBox3.Text;
command.Parameters.Add("@rec_date", OleDbType.Date).Value = date;
command.Connection = vcon;
command.ExecuteNonQuery();
}
this.textBox1.Clear();
this.textBox2.Clear();
this.textBox1.Focus();
}
}
}
else
{
MessageBox.Show("The value of textBox1 is not in the orders table");
return;
}
}
}
Upvotes: 0
Views: 533
Reputation: 39085
If it jumps to the else
of if(reader.HasRows)
without throwing any exceptions, then reader
must not be null
, and it's HasRows
property must be false
. That means your query executed successfully but returned no rows.
You might try running the select
statement by hand, which might help you understand what's wrong. Most likely, you're typing something in the textbox that doesn't match any of the cust_name
values.
Upvotes: 2