how can display message box when no results found in ms access db using c#?
i'm using code search:
connection.open(); oledbcommand command = new oledbcommand(); command.connection = connection; string query = "select * emp emp_id '" + textbox5.text + "'"; command.commandtext = query; oledbdatareader reader = command.executereader(); while (reader.read()) { slip.text = reader["emp_id"].tostring(); } connection.close();
you can use hasrows
property.
gets value indicates whether oledbdatareader contains 1 or more rows.
if(reader.hasrows) { } else { // show message box here }
also should use parameterized queries. kind of string concatenations open sql injection attacks.
and use using
statement dispose oledbconnection
, oledbcommand
, oledbdatareader
automatically instead of calling .close()
or .dispose()
methods manually.
using(var connection = new oledbconnection(constring)) using(var command = connection.createcommand()) { command.commandtext = "select * emp emp_id ?"; command.parameters.addwithvalue("?", "%" + textbox5.text + "%"); using(var reader = command.executereader()) { if(reader.hasrows) { while (reader.read()) { slip.text = reader["emp_id"].tostring(); } } else { // show message box here } } }
Comments
Post a Comment