First of all, check past answer to your question:
How to insert increment(+1) value in access table[
^]
Second of all... Your query is wrong and will never execute:
update Login set Sales_count= IIF(IsNull(Sales_count), 0, Sales_count) + 1, [To_Date]=Date()
where [Unique_No]=@Unique_No
You have to use
MAX[
^] function to return the maximum value for
Sales_count
field.
SELECT Max([Login].Sales_count) AS MaxOfSalesCount
FROM [Login]
WHERE (([Login].Unique_No=@Unique_No) AND [Login].To_Date=Date());
To return single cell (result), use
OleDbCommand.ExecuteScalar Method (System.Data.OleDb)[
^]
Usage:
public int NextSalesNo(string queryString,
OleDbConnection connection)
{
OleDbCommand command = new OleDbCommand(queryString, connection);
command.Connection.Open();
var result = command.ExecuteScalar();
return result == null ? 0 : result +1;
connection.Close();
}
In case of above method return
0
(zero), you have to insert new data.
In other case you have to update
Login
table, by using below query:
UPDATE Login SET Sales_count = @NewSales
WHERE (([Login].Unique_No=@Unique_No) AND [Login].To_Date=Date());
where
@NewSales
is the value you get returned from the above method.
Try!
[EDIT]
My second thought: your database design is wrong.
You don't have to change / update
Sales_count
, you should be able to count it. For example: you have got table
Sales
. In that table you store the data about someone's sales:
CREATE TABLE Sales
{
SaleID INT,
UserID INT,
SaleDate DateTime,
...
};
To be able to count it, you should group data by
UserID
. For total sales:
SELECT UserID, COUNT(*) As CountOfSales
FROM Sales
GROUP BY UserID;
For daily sales:
SELECT UserID, DateOfSale, COUNT(*) As CountOfSales
FROM Sales
GROUP BY UserID, DateOfSale;
For specific user and date:
SELECT COUNT(*) As CountOfSales
FROM Sales
WHERE ((UserID=@UserID) AND (DateOfSale=@Date));
So, please, re-think your database design!
[/EDIT]