-4

How do I do this? I use Windows Forms and label.

How to count the number of rows in a table with a condition? C# & MS Access.

My code:

string CONECTION = @"Provider = Microsoft.Jet.OLEDB.4.0; Data Source = SRC\\DATA\\DBACCSES2005.mdb";

string Query = "Select count(*) FROM SALES WHERE DATE="+DateTime.Today;

OleDbConnection con = new OleDbConnection(CONECTION);
OleDbCommand cmd = new OleDbCommand(Query, con);

con.Open();
int TOTALSALES = (Int32)cmd.ExecuteScalar();

SALES.TEXT = totalid.ToString();
CON.CLOSE();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
  • 1
    What have you tried? `table.Rows.Count()` is the simplest way in C# once you have it in a `DataTable` and out of Access – Jaskier Feb 07 '19 at 20:44
  • What is the error message you are getting when you try this? – Adas Feb 07 '19 at 20:57
  • Possible duplicate of [how to update a table using oledb parameters?](https://stackoverflow.com/questions/2675610/how-to-update-a-table-using-oledb-parameters) – mjwills Feb 07 '19 at 22:10
  • If you take the contents of `Query` and run it directly against Access, it also won't work. Check my duplicate for the proper way to pass parameters (instead of string concatenation). – mjwills Feb 07 '19 at 22:14

1 Answers1

0

Use the correct formatting of a string expression for a date value:

string Query = "Select Count(*) FROM SALES WHERE [DATE] = #" + DateTime.Today.ToString("yyyy'/'MM'/'dd") +"#";

Or, simpler, use the function of Access:

string Query = "Select Count(*) FROM SALES WHERE [DATE] = Date()";
Gustav
  • 53,498
  • 7
  • 29
  • 55