DateTime.Now
already gives you a valid DateTime value. Why are you trying to convert it, twice?
Moreover, you should not build SQL queries by concatenating strings like you do; you should use parameterized queries instead.
Protect Your Data: Prevent SQL Injection[
^]
Finally, your datetime fields in your database should have a datetime type, not a string (or varchar/nvarchar) type. The way you wrote your query (enclosing the DateTime value between single quotes) makes me think you are storing datetimes as strings, which is a bad practice.
You could try:
DateTime _date = DateTime.Now;
SqlCommand command2 = new SqlCommand("SELECT date, acno, machinenumber, modified FROM CHECKINCHECKOUT WHERE acno = @sdwEnrollNumber AND modified = 0 AND machinenumber = @iMachineNumber AND date = @_date", con);
command2.Parameters.AddWithValue("@sdwEnrollNumber", sdwEnrollNumber);
command2.Parameters.AddWithValue("@iMachineNumber", iMachineNumber);
command2.Parameters.AddWithValue("@_date", _date);
con.Open();
SqlDataReader reader2 = command2.ExecuteReader();
reader2.Close();
con.Close();
Hope this helps. Kindly.
Edit:
Trying to fix the format issue, you can try:
string formattedDateTime = _date.ToString("yyyy-MM-dd hh:mm:ss");