0
votes

I having a problem while I trying to select data from a Firebird database, here is my code:

string SQLCash = "SELECT sum(t2.Cash-Change)" +
            "FROM dbFBHdr t1, dbFBCollection t2" +
            "WHERE t1.Branch = t2.Branch AND" +
            "t1.CashNo = t2.CashNo AND" +
            "t1.CashDate >= '08/10/2018 00:00:00' AND" +
            "t1.CashDate <= '08/10/2018 23:59:59' AND" +
            "t1.Status = 'CLOSED'";
FbCommand cmdCASH = new FbCommand(SQLCash, FbCon);
cmdCASH.ExecuteNonQuery();
FbDataReader readerCASH = cmdCASH.ExecuteReader();
while (readerCASH.Read() == true)
{
   PDC.CASH += (reader["Cash"].ToString());
}
this.txtCash.Text = PDC.CASH;

But with this code, I having an error but I don't know how to solve it.
I attach my error at below:

enter image description here

1
The error says that you have unknown token t1, probably you need to add keyword as like this: FROM dbFBHdr AS t1 - Markiian Benovskyi
Hi Markiian Benovskyi, I had been tried to add as keyword in my sql query but the problem still existed string SQLCash = "SELECT sum(t2.Cash-Change)" + "FROM dbFBHdr as t1, dbFBCollection as t2" + "WHERE t1.Branch = t2.Branch AND" + "t1.CashNo = t2.CashNo AND" + "t1.CashDate >= '08/10/2018 00:00:00' AND" + "t1.CashDate <= '08/10/2018 23:59:59' AND" + "t1.Status = 'CLOSED'"; - Teo Lawrence
Ah, when you are concatenating this with + you receive incorrect string, try to write the command in single string without concatenation - Markiian Benovskyi
change your query to: SELECT sum(t2.Cash-Change)" + " FROM dbFBHdr as t1, dbFBCollection as t2" + " WHERE t1.Branch = t2.Branch AND" + " t1.CashNo = t2.CashNo AND" + " t1.CashDate >= '08/10/2018 00:00:00' AND" + " t1.CashDate <= '08/10/2018 23:59:59' AND" + " t1.Status = 'CLOSED. what I did was adding white space between lines. - Long Luong
Hi Thank you ~ I changed my code to {"SELECT sum(t2.Cash-Change) AS Cash FROM dbFBHdr t1, dbFBCollection t2 WHERE t1.Branch = t2.Branch AND t1.CashNo = t2.CashNo AND t1.CashDate >= '08/10/2018 00:00:00' AND t1.CashDate <= '08/10/2018 23:59:59' AND t1.Status = 'CLOSED'"} but another problem error comes is 'Could not find specified column in results: Cash - Teo Lawrence

1 Answers

1
votes

I ran your code inside my project and this is the result

"SELECT sum(t2.Cash-Change)FROM dbFBHdr t1, dbFBCollection t2WHERE t1.Branch = t2.Branch ANDt1.CashNo = t2.CashNo ANDt1.CashDate >= '08/10/2018 00:00:00' ANDt1.CashDate <= '08/10/2018 23:59:59' ANDt1.Status = 'CLOSED'"

as you can see the code above is missing spaces between string addition, either add it in the beginning of each string or at the end of them.

also since you are using multiple lines you can use @ before the string which will allow you to have multiple lines in one string, example

string SQLCash = @"SELECT sum(t2.Cash-Change)
        FROM dbFBHdr t1, dbFBCollection t2
        WHERE t1.Branch = t2.Branch AND
        t1.CashNo = t2.CashNo AND
        t1.CashDate >= '08/10/2018 00:00:00' AND
        t1.CashDate <= '08/10/2018 23:59:59' AND
        t1.Status = 'CLOSED'";