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 6215609
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:05:20+00:00 2026-05-24T07:05:20+00:00

I am doing a college attendance project with Winforms, MySQL, and C#. In that

  • 0

I am doing a college attendance project with Winforms, MySQL, and C#.

In that I want to update “Total Absent” and “Total Present” in one table. So I wrote code for that:

int p = 0;
int a = 0;
string sno = "";

MySqlDataReader Reader;
command.CommandText = "select * from attendance_monthly_rpt";
connection.Open();
Reader = command.ExecuteReader();           

while (Reader.Read())
{
    p = 0;
    a = 0;
    sno = Reader[1].ToString();
    for (int k = 3; k <= 33; k++)
    {
        if ((Reader[k].ToString() == "P") || (Reader[k].ToString() =="OD"))
        {
            p += 1;
        }
        else if (Reader[k].ToString() == "Ab")
        {
            a += 1;
        }
    }
}
connection.Close();

Now I get “sno”, “a”, “p” values for that. But I want to update this record in the MySQL table. So I added an UPDATE statement:

while (Reader.Read())
{
    p = 0;
    a = 0;
    sno = Reader[1].ToString();
    for (int k = 3; k <= 33; k++)
    {
        if ((Reader[k].ToString() == "P") || (Reader[k].ToString() =="OD"))
        {
            p += 1;
        }
        else if (Reader[k].ToString() == "Ab")
        {
            a += 1;
        }
    }           
    command.CommandText = "update attendance_monthly_rpt set tot_persent = " +
                          p + ", tot_absent = " + a + " where student_no = '" +
                          sno + "'";
    connection.Open();  // the connection is already open
    command.ExecuteNonQuery();
    connection.Close();
}
connection.Close()

This throws the error “The connection is already open”. If I place the update statement after the loop, I get the last record only.

Please help me to update my “Tot absent”, “Tot present” for each “Student”.

  • 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-24T07:05:21+00:00Added an answer on May 24, 2026 at 7:05 am

    Look at your code after you’ve removed most of what’s not related to opening or closing the connection:

    …
    connection.Open();       // <--------------+
    …                        //                |
                             //                |
    while (…)                //                |
    {                        //                |
        …                    //                |
        connection.Open();   // the connection is already open
        …
        connection.Close();  // <--------------+
    }                        //                |
    connection.Close()       // likewise, it'll already be closed here
    

    It’s easy to see now why the connection is already open during the first run of the while loop.

    Apropos: I would suggest that you not .Open() and .Close() the connection for every single command. (Connections should indeed not be kept open any longer than is necessary, but you’re probably exaggerating it a little there. 🙂 Instead, just open the connection once before you start your updates, and close it after the while loop. And, like CodeBuzz suggested already, put the connection.Close() in a finally block to make sure the connection is closed even if something goes wrong.

    P.S.: Don’t forget to .Dispose() all objects that are IDisposable (ie. the data reader and connection objects), e.g. via a using block.

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

Sidebar

Related Questions

I am doing a college attendance project (Winforms, Mysql, C#) I want to update
I am doing Student Attendance project for college in win form with MySQL(C#). see
I am doing college project in winform, mysql ,c#. for that I have created
I am doing Student Attendance project for college in win form with MySQL(C#). I
i am doing a library project for my college. in that, i have table
I am doing a college assignment in Java that deals with currency. For that
I'm doing a project for a college class I'm taking. I'm using PHP to
I'm doing my college's task. I wrote like this int debut[10][100]; char ngroup[10][100]; do
Currently doing a group project for college in Java. The assignment is to produce
I am learning SQL and DB design for a college class. One assignment that

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.