Click to See Complete Forum and Search --> : Total Value of Array Column


wilerk
05-30-2006, 10:23 AM
Hi There,

I'm pretty new to programming and wonder if somebody wouldn't mind assisting me with a little problem I'm facing. I have an Array filled with data from a SqlDataReader and I would like to add the values to a total value. The output is for example 5000 and 10000 and I would like to see this added to 15000. Here is what I have so far to check if I am getting anything into my Array:

SqlConnection sqlConn = new SqlConnection("TraLaLaLaaaaa");
string getinvoiceID = Request.QueryString["invoiceID"];
string slcStmt = "Select NettPrice From tblAddProd WHERE invoiceID = " + getinvoiceID;
SqlCommand sqlCmd = new SqlCommand(slcStmt, sqlConn);

sqlCmd.Connection.Open();

ArrayList arNettPrice = new ArrayList();
SqlDataReader sqlReader = sqlCmd.ExecuteReader();

while (sqlReader.Read())
{
object[] values = new object[sqlReader.FieldCount];
sqlReader.GetValues(values);
arNettPrice.Add(values);
}
sqlReader.Close();
sqlConn.Close();

foreach(object[] row in arNettPrice) {
foreach(object column in row) {
lblSubTots.Text += column.ToString() + "<br>";
}
}

Thanks in advance,

Xander

handshakeit
05-31-2006, 03:32 AM
I can't understand what u have done in the given code
sqlReader.FieldCount gives the no of columns in each row (1 in this case for all rows)
for find the total change your querry
use SUM function for example
SELECT sum( mark ) FROM `student`;
in your case I m just trying (check it urself)
"Select SUM[Total.NettPrice] From (Select NettPrice From tblAddProd WHERE invoiceID = " + getinvoiceID+")as Total"

somthing like this..........

wilerk
05-31-2006, 03:44 AM
Hi Abhishek Goel,

Thank you for your reply. I have tried the SUM function and it works fine but I think I kind of lost the plot myself in this.

What I am trying to do is firstly see the quantity of the products times the NettAmount. Then I would like to add those totals per row and display them on a label. From there on I still need to calculate the tax of it as well.

Now lucky me the invoice will only display one product so I made a working copy of this, but I want to extend it for the just-in-case scenario. So you can add products and it will automatically (for the user of course) do the calculations.

Thanks,

Xander

wilerk
06-02-2006, 09:30 AM
Hi Abhishek Goel,

Back again, learned some new tricks thank you very much for your pointing me to the right direction. I used the SUM function and all is well. Thing was I didn't understand you could do interrow calculations with the SUM function I though you could only get the total within a column.

Cheers,

Xander