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

  • SEARCH
  • Home
  • 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 7940349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T23:21:38+00:00 2026-06-03T23:21:38+00:00

I am writing a program in C#.NET I would like to gather the total

  • 0

I am writing a program in C#.NET
I would like to gather the total frequency of a class (let imagine every class has many words and each word has its own frequency in the corresponding class)

So i used the sum() function in mysql. But there is an error saying that my cast is wrong.

 public void average_each_type()
        {
            MySqlDataReader result;
            int total_freq = 0;
            string type = "";

            command.CommandText = "select class_name ,SUM(frequency) as sum_of_freq from training_set group by class_name ";


                result = command.ExecuteReader();
                while (result.Read())
                {

                    total_freq = (int)result["sum_of_freq"]; //error happened here
                    type = result["class_name"].ToString();
                    //.....then so on...//
  • 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-06-03T23:21:40+00:00Added an answer on June 3, 2026 at 11:21 pm

    SUM in MySql will return a decimal or double value, depending on the type within “frequency”. From the documentation for SUM:

    The SUM() and AVG() functions return a DECIMAL value for exact-value arguments (integer or DECIMAL), and a DOUBLE value for approximate-value arguments (FLOAT or DOUBLE). (Before MySQL 5.0.3, SUM() and AVG() return DOUBLE for all numeric arguments.)

    If you want an integer, you can use Convert to get one no matter what the source type happens to be:

    total_freq = Convert.ToInt32(result["sum_of_freq"]);
    

    The advantage here is the Convert.ToInt32 will work no matter what type of value is returned from the database, provided it is a numeric type.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm writing a program which has both an ASP.NET configuration system and a Silverlight
I'm writing a programm on my laptop in C#.Net, Windows Forms. I would like
Writing an asp.net mvc app and i have something like this... Public Class AA
I am newbie at using DataSet. I am writing a program with VB.NET, there
I'm writing a plug-in for another program in C#.NET, and am having performance issues
I am writing an asp.net program for creating reservations in which I have a
I have started writing my own WebDAV server class in .NET, and the first
I'm writing a c# program that will launch many child processes. At some time
I am writing a program in VB.Net to manage text messages sent through an
I have an ASP.Net page that has a button that when clicked I would

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.