Reputation:
This code is supposed to prompt the message: "successfully returned" when textbookid.Text=not empty string
and prompt the message: "selection error" when textbookid.Text=empty string
. The problem is that when textbookid.text=empty string
the code prompts: "successfully returned" instead of prompting "selection error".
try
{
connection.Open();
OleDbCommand command = new OleDbCommand();
command.Connection = connection;
string cb = "UPDATE book SET [Location] = 'lib' WHERE [Book_Id]='"
+ textbookid.Text
+ "'"
;
command = new OleDbCommand(cb);
command.Connection = connection;
command.ExecuteReader();
MessageBox.Show( "Successfully returned",
"Book Details",
MessageBoxButtons.OK,
MessageBoxIcon.Information
);
connection.Close();
//delete_records();
//refs();
//refchk();
}
catch
{
MessageBox.Show("Selection Error.");
}
Upvotes: 0
Views: 70
Reputation: 216243
Because a WHERE condition that results in nothing to update doesn't trigger any exception.
In any case you are coding this wrongly. You need to use the using statement around your connection and your command, then you need to use a parameterized query to avoid Sql Injection and parsing problems, finally, the correct method to use with an UPDATE/INSERT/DELETE statement is the ExecuteNonQuery. By the way, this method returns the number of rows changed by your command and you could use it to emit your error message in case nothing has been changed by your query...
try
{
using(OleDbConnection connection = new OleDbConnection(.....))
using(OleDbCommand command = new OleDbCommand(cb, connection))
{
connection.Open();
string cb = @"UPDATE book SET [Location] = 'lib'
WHERE [Book_Id]= ?";
command.Parameters.Add("@p1", OleDbType.VarWChar).Value = textbookid.Text;
int rowsUpdated = command.ExecuteNonQuery();
if(rowsUpdated > 0)
MessageBox.Show("Successfully returned", ....);
else
MessageBox.Show("Selection Error.");
}
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
Of course, I assume that you want to emit the error message for any kind of situations in which the command result in nothing updated and not just in the case where the textbox is empty. This could easily blocked with a single line before this code
if(string.IsNullOrWhiteSpace(textbookid.Text))
return;
Upvotes: 5
Reputation: 1505
It looks like you're not returning "Selection Error"
because it's in the catch
block. In case you're not aware, catch
will only execute if the code in try
returns an error.
In your case, you may want to try something like:
if (string.IsNullOrEmpty(textbookID.Text))
{
MessageBox.Show("Selection Error.");
}
else
{
MessageBox.Show("Successfully returned", "Book Details", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
Note, string.IsNullOrEmpty
will return true
if the string is ... null or empty. There is also string.IsNullOrWhitespace
, which will return true if string is null, empty or only spaces, i.e. " "
.
Upvotes: 3
Reputation: 1696
Only because the textbookid.text
is empty it still is a valid SQL command. Do separate checking beforehand e.g.:
if(String.IsNullOrWhiteSpace(textbookid.Text))
{
MessageBox.Show("Selection Error.");
return;
}
try
//the rest of your code
Upvotes: 1