Joanne Perry
Joanne Perry

Reputation: 40

Adding two values from database together

How would i be able to add 2 parameter together?

i have this following code but it dont seem to work

balanceDB = readdata[("balance"+"overdraftLimit")].ToString();

Upvotes: 0

Views: 170

Answers (4)

BizApps
BizApps

Reputation: 6130

I assume that readdata is a DataTable/datareader and what you want is to add values of balance and overdraftLimit.

int balanceDB;

balanceDB = (Convert.ToInt32(readdata["balance"])+Convert.ToInt32(readdata["overdraftLimit"]));

Supposed the value of balance =1 and overdraftLimit=1 then balanceDB will be equals to 2

Or if you just need to concatenate the two strings:

 string balanceDB;

balanceDB = readdata["balance"].ToString()+readdata["overdraftLimit"].ToString();

balanceDB will be equals to 11.

Regards

Upvotes: 0

user60456
user60456

Reputation:

I assume you are trying to do this with a DataReader, right?

You will need to read in each column separately, and concat them together. The code below assumes you are working with strings. If they are integers, or some other type, you need to cast accordingly

balanceDB = readdata["balance"].ToString() + readdata["overdraftlimit"].ToString();

Upvotes: 0

evasilchenko
evasilchenko

Reputation: 1870

Considering that balanceDB is a string. Have you tried this:

balanceDB = readdata["balance"].ToString() + readdata["overdraftLimit"].ToString();

Upvotes: 0

Jetti
Jetti

Reputation: 2458

balanceDB = (readdata["balance"] + readdata["overdraftlimit"]).ToString();

This is assuming that you will do the casts. If you don't have casts in your code you need to do something like:

balanceDB = (Convert.ToDouble(readdata["balance"].ToString()) + Convert.ToDouble(readdata["overdraftlimit"])).ToString(); 

Adjust to fit whatever data type is necessary for these two fields.

Upvotes: 1

Related Questions