Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>What you could do is:</p> <ol> <li>Get data from database into data table</li> <li>Pick the two lines for Rate and Total from the result</li> <li>(If you don't need to display them, remove these lines from the table)</li> <li>Do your calculation based on the values in the two rows</li> <li>Add a new row to the table manually for the results</li> </ol> <p><strong>EDIT</strong><br> Maybe I didn't fully understand your data structure. So let's look at some scenarios. If yours isn't in there, please provide more detail on how one row in your data table actually looks like:</p> <p><em>Case a</em>: Data is stored within columns, you want the Totals and Amount columns within that row, too</p> <pre><code>Value1 Value2 Value3 Rate Total Amount 1 5 3 10 ? ? </code></pre> <p>You could adapt your SQL statement as follows:</p> <pre><code>SELECT Value1, Value2, Value3, Rate, (Value1+Value2+Value3) AS Total, ((Value1+Value2+Value3) * Rate) AS Amount FROM &lt;table&gt; </code></pre> <p>Alternatively, if you're using a typed dataset in C#, you could use a normal <code>SELECT * ...</code> statement and add columns with respective expressions (<code>Expression</code> property - the equivalent to computed columns) manually to the table in your typed dataset.</p> <p><em>Case b</em>: Data is stored within rows, you want Total and Amount to be rows, too</p> <pre><code>RowTitle Value Value1 1 Value2 5 Value3 2 Rate 10 </code></pre> <p>Now you need to use a for-loop in C# to iterate over all the rows, sum up the values and then create two new rows with <code>RowTitle</code> "Total" and "Amount" and add them to your table.</p> <p><strong>EDIT 2</strong><br> You, so I understand from your second comment that you actually have two tables in your database. One that contains the actual data, and one that contains the Rates. You now select from these tables according to user defined settings and you actually get two tables in your data set in C# - one for that data, one for the Rate.</p> <p>AFAIK the SQL Server Compact Edition does not allow you to use <code>JOIN</code>, but that would have been the easiest: select the data from your data table and join it to the "Rates" table, selecting the respective rate according to the user settings. Then you could do the calculation I noted in "Case a" above.</p> <p>However, I think you can not use <code>JOIN</code>, so I'd suggest that you get the data into your application (resulting in two separate tables, one for the data, one for the Rate). You modify your data <code>SELECT</code> statement so that you get an additional column <code>Amount</code> with default value <code>0</code>.</p> <pre><code>SELECT *, 0 AS Amount FROM &lt;data table&gt; </code></pre> <p>Then, in C# you iterate over all the records in your data table and calculate the value of <code>Amount</code> for the record.</p> <pre><code>private void GetData(...) { // Call SELECT statements to fill a dataset with two tables DataSet set = new DataSet(); .... // set now contains tables[0] (for the data) and tables[1] (for the Rate). // Get the Rate int rate = (int)set.Tables[1].Rows[0]["Rate"]; // Assuming that the column is named "Rate" // Now iterate over all data rows and calculate amount foreach (DataRow row in set.Tables[0]) { row["Amount"] = ((int)row["Total"] * rate); } // Mark the data set as unmodified set.AcceptChanges(); } </code></pre> <p>Note that this code may need some tuning - I've not actually tested it. If the "Rates"-Table contains more than one row and the correct rate needs to be selected depending on other settings, you might consider creating a function that returns the correct row depending on these settings:</p> <pre><code>private DataRow FindRateRow(DataSet set, &lt;parameters for selecting the correct row&gt;) { DataRow[] row = set.Tables[1].Select(&lt;Filter string according to parameters&gt;); if (row != null &amp;&amp; row.Length &gt; 0) return row[0]; return null; } </code></pre> <p>Use <code>int rate = (int)FindRateRow(set, ...)["Rate"];</code> above.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload