Teo Lawrence
Teo Lawrence

Reputation: 37

c# int and string a same value but come with two different number

Good Day ! I'm having a problem which is when I do a SQL query and want to return an int value, that queries suppose to return a zero value, but when I get int it will automatic becomes 48, but when I get from string then it is zero, here is my code

string SQLCtn = @"SELECT sum(t7.Qty) AS Ctn 
                        FROM dbFBHDR t1, dbFBDTL t7, dbProduct t3 LEFT OUTER JOIN dbClass t4 
                        ON t3.PClass = t4.Class LEFT OUTER JOIN dbCategory t5 
                        ON t3.PClass = t5.Class 
                        AND t3.PCategory = t5.Category LEFT OUTER JOIN dbType t6 
                        ON t3.PClass = t6.Class 
                        AND t3.PCategory = t6.Category 
                        AND t3.PType = t6.PType 
                        WHERE t7.ProductCode = t3.Code 
                        AND t3.PClass = '" + PDC.classBeer + "' " +
                        "AND t3.PCategory >= '" + PDC.catBeerFrom + "' " +
                        "AND t3.PCategory <= '" + PDC.catBeerTo + "' " +
                        "AND t3.PType = '" + PDC.typeCTN + "' " +
                        "AND t1.Status = 'CLOSED' " +
                        "AND t1.CashNo = t7.CashNo " +
                        "AND t1.Branch = t7.Branch " +
                        "AND t1.CashDate >= '" + PDC.DateFrom + "' " +
                        "AND t1.CashDate <= '" + PDC.DateTo + "' " +
                        "AND t1.Branch = '" + PDC.branch + "' " +
                        "AND extract(hour from CheckInTime) >= '0' " +
                        "AND extract(hour from CheckInTime) <= '9999'";

        FbCommand cmdCTN = new FbCommand(SQLCtn, FbCon);
        cmdCTN.ExecuteNonQuery();
        FbDataReader readerCTN = cmdCTN.ExecuteReader();
        while (readerCTN.Read() == true)
        {
            int iCTN;
            string sCTN;

            if (readerCTN["Ctn"].ToString() == "")
            {
                iCTN = '0';
                sCTN = "0";
                MessageBox.Show("" + iCTN + "", "int");
                MessageBox.Show("" + sCTN + "", "string");
            }
            else
            {
                iCTN = readerCTN.GetInt32(readerCTN.GetOrdinal("Ctn"));
                sCTN = readerCTN.GetString(readerCTN.GetOrdinal("Ctn"));
                MessageBox.Show("" + iCTN + "", "int");
                MessageBox.Show("" + sCTN + "", "string");
            }
        }

And this is the result

Result with int

Result with string

Upvotes: 1

Views: 115

Answers (2)

Michael
Michael

Reputation: 909

To clarify Guy's answer, computers view all data as Binary which is a system of numbers much like the decimal system we're familiar with.

There's no such thing as an "A", a "b", a "6" or any other letter to a computer. In fact even "0" and "1", the numbers we use to count binary are just how we represent what a computer sees.

In order to represent letters and numbers in a string we use a convention. One of the earliest and widely used is ASCII (but others like UTF exist too).

If you look at the table for ASCII, the number '0' is represented at 48, the letter 'A' is 65 and the letter 'a' is 97. To the computer they are always the numbers 48, 65 and 97 but if you tell the computer to treat them as a character or a string it will represent them to the screen as '0', 'A', and 'a' for you.

To a computer int foo = '0'; is the same as int foo = 48;
And similarly char bar = 65; is the same as char bar = 'A';

Upvotes: 1

Guy
Guy

Reputation: 50819

You converted the char '0' to int, so you see its ASCII code 48.

Just assign 0, iCTN = 0;

Upvotes: 1

Related Questions