2013-10-02 125 views
0

夥計們我有一個查詢返回3個值(-4.00,-7.53,-15.00。 我正在返回這些名爲ReaderResult的列表,如代碼所示,但是當它們被添加到列表中時我也想保持列表中添加的總要素添加列表中的元素c#

DataReader = DoQuery(" select oa.[OtherAmount] FROM [Biatss_PC_SRV].[Pax].[SalesDocumentOtherAmount] as oa " 
      +"INNER JOIN [Biatss_PC_SRV].[Pax].[SalesRelatedDocumentInformation] as rd on oa.RelatedDocumentGuid = rd.RelatedDocumentGuid " 
      +"INNER JOIN [Biatss_PC_SRV].[Pax].[SalesDocumentHeader] as h on rd.HdrGuid = h.HdrGuid where h.DocumentNumber = '" + txtTicketNumber.Text.ToString() + "'"); 
       //check DataReader 
       if (DataReader == null) 
       { 
        DataReader.Close(); 
        DataReader.Dispose(); 
       } 
       else 
       { 
        if (DataReader.HasRows) 
        { 
         //setting variables to be used to initial values 
         ii = 0; 
         double sumOfTaxes = 0; 

         ReaderResult.Clear(); 
         while (DataReader.Read()) 
         { 
          //for (ii = 0; ii < DataReader.FieldCount; ii++) 
          //{ 
          if (DataReader.IsDBNull(0)) 
           { 
            string CaseNull = ""; 
            ReaderResult.Add(CaseNull); 
           } 
           else 
           { 
            //put results in LIST<> 
            ReaderResult.Add(Convert.ToString(DataReader.GetDecimal(0))); 
            //double sumLaSa =+ Convert.ToDouble(ReaderResult.Last());  VALUE HERE IS ONLY THE LAST OF THE 3 NOT THE SUM 
            //sumOfTaxes =+ Convert.ToDouble(DataReader.GetDecimal(0)); 
            //double sumLaSa =+ Convert.ToDouble(ReaderResult.Last().Last()); ERROR I GET HERE says cannont convert char to double 
            myTextboxListForAmounts[ii].Text = Convert.ToString(DataReader.GetDecimal(0)); 

          // } 

          if (ii > 6) 
          { 
           //make txtXT visible and display summed up amount in it 
           lblXT.Visible = true; 
           txtXT.Visible = true; 
           txtXT.Text = Convert.ToString(sumOfTaxes); 
          } 
          ii++; 

           } 
         } 
        } 
        else 
        { 
         DataReader.Close(); 
         DataReader.Dispose(); 
        } 
        DataReader.Close(); 
        DataReader.Dispose(); 
       } 

我怎樣才能獲得值的總和回來好嗎?

+0

如果希望SQL輸出你總是有SQL命令SUM() –

+0

的總和我會建議你使用存儲過程,並從那裏返回的總和! –

+2

有什麼錯'sumOfTaxes = + Convert.ToDouble(DataReader.GetDecimal(0));'? ...哦,等等,你的意思是'+ ='? - 順便說一句,您可能需要閱讀[SQL Incection](http://technet.microsoft.com/library/ms161953.aspx)並使用[using](http://msdn.microsoft.com/library/yh598w02 .aspx)與[IDisposables](http://msdn.microsoft.com/library/system.idisposable.aspx)或至少[try-finally](http://msdn.microsoft.com/library/zwc8s4fz的.aspx)。 – Corak

回答

0

解決的問題。

達到目的使用由Corak點燃的解決方案

sumOfTaxes =+ Convert.ToDouble(DataReader.GetDecimal(0)); 

修改

sumOfTaxes += Convert.ToDouble(DataReader.GetDecimal(0)); (+= instead of =+) 
enter code here