-2

I am trying to find sum of values in a column using a condition but the result am getting is null am not sure of where the problem is coz i have tried running the sql database access but and it works but when i it at in the progam it does not work

So i need assistance on this one

The code is below

Blockquote

  p.Query="SELECT SUM(income) FROM Emp_income  WHERE username ='" + frmlogin.username + "'"; 
            using (IDbCommand command = new OleDbCommand(p.Query, p.Con))

            {
                object answer=command.ExecuteScalar();
                lblincome.Text=Convert.ToString(answer);
            }
wohlstad
  • 12,661
  • 10
  • 26
  • 39
Dumile
  • 9
  • 5
  • You need to put a breakpoint to see if there's a value on the frmlogin.username but I am guessing there's not because you are accessing the whole control. Try replacing it with frmlogin.username.Text to access the text inside that control. – Izanagi Dec 30 '22 at 08:36
  • You have ```'``` that you have misplace at the end of your first line. Change ```"'``` by ```'"``` But I don't konw if it's just something that came when you copy-pasted? – Scryper Dec 30 '22 at 08:38
  • What's the error message? – MichaelMao Dec 30 '22 at 08:42
  • Username is a public static variable that keeps the username of the person who has logined in and it is initialised in another form if a person wants to check there total income while still logged in they will there is a button which executes a method for the sum sql and only the records with income corresponding to the username should be added – Dumile Dec 30 '22 at 08:42
  • There is no error message but it is returning nothing yet there should be something – Dumile Dec 30 '22 at 08:46
  • I'd start by reading about the infamous "SQL Injection": https://stackoverflow.com/questions/2216107/what-is-sql-injection – Stefan Wuebbe Dec 30 '22 at 08:49

1 Answers1

0

Add try/catch block to ExcuteScalar() for error message, for connection oriented ADO.Net you have to Open connection before executing the command like con.Open()