Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4120548
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T23:14:12+00:00 2026-05-20T23:14:12+00:00

This has been bugging me for the past several hours and I can’t seem

  • 0

This has been bugging me for the past several hours and I can’t seem to find the answer..

I have the following query

SELECT        A, SUM(B) AS total
FROM          table
GROUP BY      A

now the column B in table can only hold 0 or 1.
and A is total1, total2 or total3

now when I use this directly in the SQL database I get a nice table holding

A        total
total1   1
total2   0
toatl3   5

This is exactly what I want it to do.
However if using in my c# program. if one of the totals is 0 it is not displayed at all..
Below is the code I’m using but it only works fine when total1, total2 and total3 are bigger then 0
so the table above would only display total1 and total3…

string total = "A        total";       
SqlConnection conn = new SqlConnection("connection string goes here I know");
try
{
    conn.Open();
    SqlCommand total = new SqlCommand(
        "SELECT A, SUM(B) AS total FROM table GROUP BY A", conn);

    SqlDataReader total_reader = total.ExecuteReader();
    while (total_reader.Read())
    {
        total += total_reader["A"].ToString() + "  " + total_reader["total"] + "\n";
    }
}
catch (Exception err)
{
    serverstats += err.ToString();
}
finally
{
    conn.Close();
}

How can I make it so that it would display the table properly, even if total1, total2 and total3 are 0

thus displaying:

A        total
total1   0
total2   0
toatl3   0

I know that a 0 in SQL generally equals to null and such.

I suspect that this is the reason that C# assumes that if the value is 0 that it is not of interest.

I hope I explained it properly enough, thanks for any help in advance!

=======EDIT======

COALESCE or ISNULL does not make a difference 🙁

I assume it’s to do with the C# reader bit not in the SQL querying bit.

As you can see in my example the SQL bit DOES create a table with the correct Rows and does not write them as NULL. But the C# bit seems to read it as NULL.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T23:14:12+00:00Added an answer on May 20, 2026 at 11:14 pm

    If column B can have nulls, try

    SELECT        A, COALESCE(SUM(B),0) AS total
    FROM          table
    GROUP BY      A
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This has been driving me nuts for hours now. Consider the following test script
This has been bugging me for some time now, I will greatly appreciate the
Ok, this has been bugging me for a while now. And I wonder how
This question has been bugging me for a while now. When writing a CSS
This has been driving me crazy all day. I just want the value of
I see many MVC implementations for websites have a single-entry point such as an
I can write: AClass[] array = {object1, object2} I can also write: AClass[] array
Whenever I disable the APE comet Server, AJAX file uploads run properly. And when
On newer Windows systems there is an option to make the OS draw a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.