Note that there are some explanatory texts on larger screens.

plurals
  1. POCount how many columns 'is not null' for each row and put that information into another column
    primarykey
    data
    text
    <p>I am new to SQL and have started a kind of test project to get my hands dirty working with SQL and C#. I have been using VS2012's database wizards to set everything up. So here's the rundown of my setup.</p> <p>SQL CE table named <code>ACDTable</code> and it is connected to my C# project by a dataset <code>dataSet1</code> and a table adapter <code>dataTableAdapter</code></p> <p><code>ACDTable</code> has 22 columns and what I am trying to do is for each row that has a value (is not null) for each column I specify it adds one to a counter and then adds the sum to another column I specify.</p> <p>Essentially I need to find how many columns are not null for each row and turn that number into something usable in C#.</p> <p>Example:</p> <pre><code> |Col1 | Col2 | Col3| Col4| Col5| | | | | | | | x | x | | x | 3 | | | x | x | | 2 | | x | | | | 1 | </code></pre> <p>I am using the example <a href="https://stackoverflow.com/questions/18193365/count-of-non-null-columns-in-each-row">Count of non-null columns in each row</a> and it does work great if I just run it as a query via SQL. </p> <p>The example I am using:</p> <pre><code>SELECT Col1, Col2, Col3, Col4, CASE WHEN Col1 IS NOT NULL THEN 1 ELSE 0 END + CASE WHEN Col2 IS NOT NULL THEN 1 ELSE 0 END + CASE WHEN Col3 IS NOT NULL THEN 1 ELSE 0 END + CASE WHEN Col4 IS NOT NULL THEN 1 ELSE 0 END AS Col5 FROM ACDTable </code></pre> <p>The problem I am running into is when I use Visual Studio to create a method based on this Query and I try to get the values of <code>Col5</code> via that method I am getting a NullDB exception. If I run the debugger and look at the table after I have called the method the <code>Col5</code> is completely null. I have a feeling the problem is the query itself is not actually changing the 5th column but my inexperience with SQL is showing and I cannot figure out the correct syntax to fix this.</p> <p><strong>Fixed typo where I had Col5 in the SELECT list</strong></p> <p><strong>Edit to show the code I am using</strong></p> <p>The method itself is the one generated by Visual Studio using the Add Query option in the Table Adapter. It is if I understand correctly supposed to run the above query and then put the values into <code>Col5</code> for me. I then just want to to see those values represented in a richtextbox using</p> <pre><code>GetTotals(); //The Generated method from VS that runs my query var totals = from p in dataTable select p.Col5; foreach (var total in totals) { testTextBox.Text = total } </code></pre>
    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.
 

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