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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:08:12+00:00 2026-06-05T20:08:12+00:00

I have a Windows Forms app in C#/.NET4 with a SQL 2008 back end.

  • 0

I have a Windows Forms app in C#/.NET4 with a SQL 2008 back end. I open a SqlConnection in my button click event handler, then call multiple backgroundworkers each of which calls the connection’s CreateCommand method inside of a using block:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
        cn.Open();
        bw01.RunWorkerAsync(x);
        bw02.RunWorkerAsync(x);
        while (bw01.IsBusy || bw02.IsBusy)
            Application.DoEvents();
        cn.Close();
    }

    private void bw01_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            using (SqlCommand cmd = cn.CreateCommand())
            {
                cmd.CommandText = <some SQL>
                e.Result = Convert.ToInt32(cmd.ExecuteScalar());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
    }

    private void bw02_DoWork(object sender, DoWorkEventArgs e)
    {
        try
        {
            using (SqlCommand cmd = cn.CreateCommand())
            {
                cmd.CommandText = <some SQL>
                e.Result = Convert.ToInt32(cmd.ExecuteScalar());
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
            return;
        }
    }

The code works some of the time, but other times I get errors, most commonly “A severe error occurred on the current command. The results if any should be discarded.”

I have MultipleActiveResultSets=true in my connection string. What am I doing wrong?

  • 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-05T20:08:13+00:00Added an answer on June 5, 2026 at 8:08 pm

    You connection variable is not thread safe as both threads are trying to use it. ADO.NET will manage connection pool for you so you should move connection creation inside each thread and make it atomic

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

Sidebar

Related Questions

I have a .NET application that contains a checkbox (System.Windows.Forms.Checkbox). This component (WindowsForms10.BUTTON.app.0.378734a1) is
I have a Windows Forms app that is able to open up a console
I have a windows forms app (c# 4.0) and the X button won't close
I have a Windows Forms app written in C#, and part of the app
I have a windows forms app where I have split different functionality into several
I have installed a C# windows forms app on a client machine which does
I have a very basic windows forms app, working in C#, and I was
In a Windows Forms app is there any control that allow to have a
I have an embedded database in windows forms app. Is it possible to browse
I'm moving my old app from windows forms to WPF and have encountered an

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.