Click here to Skip to main content
15,887,683 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
int income;
int withdrawal;
int balance;

string query = "select sum(NetAmount) from IncomeReport where MemberID='" + Session["rootid"].ToString() + "'";

SqlDataAdapter da = new SqlDataAdapter(query, con);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
	income = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}

string query2 = "select sum(RequestAmt) from IncomeReport where MemberID='" + Session["rootid"].ToString() + "' and status in('Pending','Approved')";

SqlDataAdapter da2 = new SqlDataAdapter(query, con);
DataSet ds2 = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
	withdrawal = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}

balance = income - withdrawal; // This is showing error line


What I have tried:

I am trying to subract two data which i have fetched from Database but in last line balance = income - withdrawal; It is showing Error as Use of unassigned local variable income and Use of unassigned local variable withdrawal
Posted
Updated 17-Jun-23 18:42pm
v2
Comments
Mike Hankey 17-Jun-23 16:26pm    
Give Income and withdrawal an initial value, say 0;

If your two if statements fail income and withdrawal will have unknown values.
Member 14596830 23-Jun-23 15:48pm    
Yes it worked by giving the value =0
Graeme_Grant 17-Jun-23 20:28pm    
When posting questions, and you have an error, you need to post the full error message. It has all of the key information to tell you, and us, exactly what the issue is. Saying there is an error is like saying my car won't start. Is it a compile error, is it a runtime error, or is it just that you don't agree with the result? We have no idea as we can't see your computer screen and we won't be running your code. Help us help you.
Member 14596830 23-Jun-23 15:48pm    
Thanks for your inputs. Surely will do the same in future.

C#
int income;
int withdrawal;
int balance;

...

// What is the value of withdrawal if condition is not met ?
if (ds.Tables[0].Rows.Count > 0)
{
	withdrawal = Convert.ToInt32(ds.Tables[0].Rows[0][0]);
}

balance = income - withdrawal; // This is showing error line


And same for income.
 
Share this answer
 
v2
The first problem is simple: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

The other is also simple: There is a path through that code where income is never assigned a value. If this test fails:
C#
if (ds.Tables[0].Rows.Count > 0)
Then no value is assigned, and the subtraction can't take place. The same thing will occur with withdrawal later.
The system notices this and even if in the real world if could never occur it raises an error to let you know because it has no idea what your DB or session will contain at run time.

You could fix this by assigning a default value, but a better solution is to handle the "no match" case yourself and exit the method while informing the user of the problem so he can let you know. Defaulting means you will potentially give the user the wrong information!

You should expect to get syntax errors every day, probably many times a day while you are coding - we all do regardless of how much experience we have! Sometimes, we misspell a variable, or a keyword; sometimes we forget to close a string or a code block. Sometimes the cat walks over your keyboard and types something really weird. Sometimes we just forget how many parameters a method call needs.

We all make mistakes.

And because we all do it, we all have to fix syntax errors - and it's a lot quicker to learn how and fix them yourself than to wait for someone else to fix them for you! So invest a little time in learning how to read error messages, and how to interpret your code as written in the light of what the compiler is telling you is wrong - it really is trying to be helpful!

So read this: How to Write Code to Solve a Problem, A Beginner's Guide Part 2: Syntax Errors[^] - it should help you next time you get a compilation error!

I'm not saying we don't want to help you fix them - sometimes I can't see my own errors because I read what I meant to write - but fixing syntax errors is part of the job, and if you can't do it for yourself people are going to look at you as a bit weird should you get a job in the industry!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900