Having trouble tallying items in data base

codebeginnerg

Member
Joined
Oct 13, 2014
Messages
6
Programming Experience
Beginner
I'm tallying a row in my access database. My databases contain movie company,movies produced by companys and the amount of movies sold. I want to search through the entire database and count each movie the movie company produced. This loads to the listBox automatically and don't need a button to invoke it.Ex:
Company #Movies
20 century 14
DreamWorks 2
LucasFilm 5 etc.

What I'm thinking is created a nested foreach loop and for every movie that is produced by a certain company add it to the listbox



C#:
  private void tabP3_Click(object sender, EventArgs e)
        {
           foreach (DataRowView row in bindingSource1.List)
            {
                foreach (DataRowView row2 in bindingSource1.List)
                {
                  if(((String)row2["Company"]).Equals(row["Movie"]))
                    {
                    int count = 0;
                    string company  = ((String)row["Company"]);
                    count++;
                    lbCompany.Items.Add((String.Format("{0,5}",company+ "    "  + count)));
                    }
                   
                }
               
            }
           
        }
 
Last edited:
What exactly are you retrieving from the database in the first place?

I'm retrieving movie info from the database. There is more info in the database than I mentioned, but for this problem I'm only looking at two columns. The movie company and the movies produced by that company. One column is called company and the other is called Movie. I need to count every movie that was produced by the movie company. I counted six different Movie companies in the database.
 
Do you actually need a list of every movie and the company that made it or do you only need the count? If all you need is the count then you can do it in SQL and just retrieve the count instead of retrieving a whole lot of data that you don't need and then using more C# code to process it further.
 
Do you actually need a list of every movie and the company that made it or do you only need the count? If all you need is the count then you can do it in SQL and just retrieve the count instead of retrieving a whole lot of data that you don't need and then using more C# code to process it further.

I need the movie and the number of movies produced(count) to display all in a lisbtbox. The format I can figure out.Ex

Company #Movies

20 century 14
DreamWorks 2
LucasFilm 5
 
If someone asks a question, please answer it.
Do you actually need a list of every movie and the company that made it or do you only need the count?
It appears that the answer is only the counts but if that's the case then please say so. I've been around forums too long to assume that what it looks like someone means is necessarily all that they mean.
 
I'm sorry, I thought I did answer your question. My goal is to simply display the movie company and the number movies it produced in a listbox.
 
In that case, as I said, just do it in SQL, e.g.
C#:
SELECT Company, COUNT(*) As MovieCount
FROM Movie
GROUP BY Company
You can then do whatever you like with that data.
 
Back
Top Bottom