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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T08:50:53+00:00 2026-05-15T08:50:53+00:00

I have this problem that is driving me insane. I have a project to

  • 0

I have this problem that is driving me insane.

I have a project to deliver before Thursday. Basically an app consiting of three components that communicate with each other in WCF.

I have one console app and one Windows Forms app. The console app is a server that’s connected to the database. You can add records to it via the Windows Forms client that connectes with the server through the WCF.

The code for the client:

namespace BankAdministratorClient
{
    [CallbackBehavior(ConcurrencyMode = ConcurrencyMode.Single, UseSynchronizationContext = false)]
    public partial class Form1 : Form, BankServverReference.BankServerCallback
    {
        private BankServverReference.BankServerClient server = null;
        private SynchronizationContext interfaceContext = null;

        public Form1()
        {
            InitializeComponent(); 
            interfaceContext = SynchronizationContext.Current;

            server = new BankServverReference.BankServerClient(new InstanceContext(this), "TcpBinding");

            server.Open();
            server.Subscribe();

            refreshGridView("");
        }

        public void refreshClients(string s)
        {
            SendOrPostCallback callback = delegate(object state)
            { refreshGridView(s); };
            interfaceContext.Post(callback, s);
        }

        public void refreshGridView(string s)
        {
            try
            {
                userGrid.DataSource = server.refreshDatabaseConnection().Tables[0];
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            server.addNewAccount(Int32.Parse(inputPIN.Text), Int32.Parse(inputBalance.Text));
        }

        private void Form1_FormClosing(object sender, FormClosingEventArgs e)
        {
            try
            {
                server.Unsubscribe();
                server.Close();
            }catch{}
        }

    }
}

The code for the server:

namespace SSRfinal_tcp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine(MessageHandler.dataStamp("The server is starting up"));

            using (ServiceHost server = new ServiceHost(typeof(BankServer)))
            {
                server.Open();
                Console.WriteLine(MessageHandler.dataStamp("The server is running"));
                Console.ReadKey();
            }
        }
    }

    [ServiceBehavior(ConcurrencyMode = ConcurrencyMode.Single, InstanceContextMode = InstanceContextMode.PerCall, IncludeExceptionDetailInFaults = true)]
    public class BankServer : IBankServerService
    {
        private static DatabaseLINQConnectionDataContext database = new DatabaseLINQConnectionDataContext();
        private static List<IBankServerServiceCallback> subscribers = new List<IBankServerServiceCallback>();

        public void Subscribe()
        {
            try
            {
                IBankServerServiceCallback callback = OperationContext.Current.GetCallbackChannel<IBankServerServiceCallback>();
                if (!subscribers.Contains(callback))
                    subscribers.Add(callback);
                Console.WriteLine(MessageHandler.dataStamp("A new Bank Administrator has connected"));
            }
            catch
            {
                Console.WriteLine(MessageHandler.dataStamp("A Bank Administrator has failed to connect"));
            }
        }

        public void Unsubscribe()
        {
            try
            {
                IBankServerServiceCallback callback = OperationContext.Current.GetCallbackChannel<IBankServerServiceCallback>();
                if (subscribers.Contains(callback))
                    subscribers.Remove(callback);
                Console.WriteLine(MessageHandler.dataStamp("A Bank Administrator has been signed out from the connection list"));
            }
            catch
            {
                Console.WriteLine(MessageHandler.dataStamp("A Bank Administrator has failed to sign out from the connection list"));
            }
        }

        public DataSet refreshDatabaseConnection()
        {
            var q = from a in database.GetTable<Account>()
                    select a;
            DataTable dt = q.toTable(rec => new object[] { q });
            DataSet data = new DataSet();
            data.Tables.Add(dt);

            Console.WriteLine(MessageHandler.dataStamp("A Bank Administrator has requested a database data listing refresh"));

            return data;
        }

        public void addNewAccount(int pin, int balance)
        {
            Account acc = new Account()
            {
                PIN = pin,
                Balance = balance,
                IsApproved = false
            };
            database.Accounts.InsertOnSubmit(acc);
            database.SubmitChanges();
            database.addNewAccount(pin, balance, false);
            subscribers.ForEach(delegate(IBankServerServiceCallback callback)
            {
                callback.refreshClients("New operation is pending approval.");
            });

        }
    }
}

This is really simple and it works for a single window. However, when you open multiple instances of the client window and try to add a new record, the windows that is performing the insert operation crashes with the ExecuteReader error and the ” requires an open and available connection. the connection’s current state is connecting” bla bla stuff. I have no idea what’s going on. Please advise.

  • 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-15T08:50:53+00:00Added an answer on May 15, 2026 at 8:50 am

    It’s most likely because you have declared your DatabaseLINQConnectionDataContext to be static. That’s a BIG no-no! When a variable is static, it’s shared across all threads (requests). This is a huge problem because a DataContext stores unit-of-work information about changes you’ve made ask you make them.

    Initialize one DatabaseLINQConnectionDataContext per client, otherwise you’ll run into errors like these. Try initializing database in a using block around your data accesses.

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

Sidebar

Related Questions

This is a really weird problem that I have been having. When I download
I am wondering how you would approach this problem I have two Taxrates that
This is my problem. I have a program that has to run in a
I have this problem I'm hoping someone knows the answer to. I have an
everybody; I have this problem in asp.net, I have a page where I insert
I have seen this problem arise in many different circumstances and would like to
I have faced this problem quite often during the last couple of months, during
I enjoy developing algorithms using the STL, however, I have this recurring problem where
I have hit upon this problem about whether to use bignums in my language
Consider this problem: I have a program which should fetch (let's say) 100 records

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.