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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:49:35+00:00 2026-06-11T08:49:35+00:00

I am using ServiceBrokerClass which i got from CodeProject . But now when i

  • 0

I am using ServiceBrokerClass which i got from CodeProject. But now when i try to save any record into database using EnityFramework and .Net WPF. I am getting the following error.

ServiceBrokerUtility Class :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Objects;
using System.Collections;
using System.Data.SqlClient;
using System.Runtime.Remoting.Messaging;
using System.Collections.Specialized;

namespace JIMS.UtilityClasses
{
    // see http://www.codeproject.com/KB/database/autorefresh_ef_ssb.aspx
    public static class ServiceBrokerUtility
    {
        private static readonly List<string> connectionStrings = new List<string>();
        private const string sqlDependencyCookie = "MS.SqlDependencyCookie";
        private static ObjectContext objectContext;
        private static RefreshMode refreshMode;
        private static readonly Dictionary<string, IEnumerable> collections = new Dictionary<string, IEnumerable>();        

        static public void AutoRefresh(this ObjectContext objectContext, RefreshMode refreshMode, IEnumerable collection)
        {                        
            string efConnStr=System.Configuration.ConfigurationManager.ConnectionStrings[objectContext.Connection.ConnectionString.Replace("name=", "").Trim()].ConnectionString;
            string dbConnectionString = new System.Data.EntityClient.EntityConnectionStringBuilder(efConnStr).ProviderConnectionString;
            if (!connectionStrings.Contains(dbConnectionString))
            {
                connectionStrings.Add(dbConnectionString);
                SqlDependency.Start(dbConnectionString);
            }
            ServiceBrokerUtility.objectContext = objectContext;
            ServiceBrokerUtility.refreshMode = refreshMode;
            AutoRefresh(collection);
        }

        /// <summary>
        /// Stops the auto refresh.
        /// </summary>
        /// <param name="collection">The collection.</param>
        static public void StopAutoRefresh(IEnumerable collection)
        {
            var kvp = collections.FirstOrDefault(x => x.Value == collection);
            if (!kvp.Equals(new KeyValuePair<string, IEnumerable>()))
                collections.Remove(kvp.Key);
        }

        static private void AutoRefresh(IEnumerable collection)
        {
            var oldCookie = CallContext.GetData(sqlDependencyCookie);
            try
            {
                var dependency = new SqlDependency();
                collections.Add(dependency.Id, collection);
                CallContext.SetData(sqlDependencyCookie, dependency.Id);
                dependency.OnChange += dependency_OnChange;
                objectContext.Refresh(refreshMode, collection);
            }
            finally
            {
                CallContext.SetData(sqlDependencyCookie, oldCookie);
            }
        }

        static void dependency_OnChange(object sender, SqlNotificationEventArgs e)
        {
            if (e.Info == SqlNotificationInfo.Invalid)
            {                
                return;
            }
            try
            {
                var id = ((SqlDependency)sender).Id;
                IEnumerable collection;
                if (collections.TryGetValue(id, out collection))
                {
                    collections.Remove(id);
                    AutoRefresh(collection);
                    var notifyRefresh = collection as INotifyRefresh;
                    if (notifyRefresh != null)
                        System.Windows.Application.Current.Dispatcher.BeginInvoke(
                         (Action)(notifyRefresh.OnRefresh));
                }
            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print("Error in OnChange: {0}", ex.Message);
            }
        }
    }
    public interface INotifyRefresh : INotifyCollectionChanged
    {
        void OnRefresh();
    }
}

AutoRefreshCollection Class :

namespace JIMS.UtilityClasses
{
    public class AutoRefreshCollection<T> : IEnumerable<T>, INotifyRefresh
        where T : EntityObject
    {
        public IEnumerable<T> objectQuery;
        public AutoRefreshCollection(ObjectQuery<T> objectQuery, RefreshMode refreshMode)
        {
            this.objectQuery = objectQuery;
            objectQuery.Context.AutoRefresh(refreshMode, this);
        }

        public void StopAutoRefresh()
        {
            ServiceBrokerUtility.StopAutoRefresh(this);
        }

        public IEnumerator<T> GetEnumerator()
        {
            return objectQuery.GetEnumerator();
        }

        IEnumerator IEnumerable.GetEnumerator()
        {
            return this.GetEnumerator();
        }

        public void OnRefresh()
        {
            try
            {
                if (this.CollectionChanged != null)
                    CollectionChanged(this,
                      new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

            }
            catch (Exception ex)
            {
                System.Diagnostics.Debug.Print("Error in OnRefresh: {0}", ex.Message);
            }
        }

        public event NotifyCollectionChangedEventHandler CollectionChanged;
    }
}

My Code :

public void Create()
{
    try
    {
        dbContext.AddToLedgers(
            new JIMSDAL.Ledger
            {
                LedgerName = LedgerName,
                Group = LedgerGroup,
                Street = Street,
                City = City,
                PinCode = PinCode,
                State = State,
                Phone = Phone,
                TIN = TIN,
                Balance = Balance
            }
        );                
        dbContext.SaveChanges();
        JIMSMessage.Show("Ledger Created Successfully.");
        Clear();
    }
}

Can anyone help me on this.

Inner Exception Stack Trace :

 at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean breakConnection)
   at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()
   at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)
   at System.Data.SqlClient.TdsParser.TdsExecuteTransactionManagerRequest(Byte[] buffer, TransactionManagerRequestType request, String transactionName, TransactionManagerIsolationLevel isoLevel, Int32 timeout, SqlInternalTransaction transaction, TdsParserStateObject stateObj, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransactionYukon(TransactionRequest transactionRequest, String transactionName, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnectionTds.ExecuteTransaction(TransactionRequest transactionRequest, String name, IsolationLevel iso, SqlInternalTransaction internalTransaction, Boolean isDelegateControlRequest)
   at System.Data.SqlClient.SqlInternalConnection.BeginSqlTransaction(IsolationLevel iso, String transactionName)
   at System.Data.SqlClient.SqlInternalConnection.BeginTransaction(IsolationLevel iso)
   at System.Data.SqlClient.SqlConnection.BeginDbTransaction(IsolationLevel isolationLevel)
   at System.Data.Common.DbConnection.BeginTransaction(IsolationLevel isolationLevel)
   at System.Data.EntityClient.EntityConnection.BeginDbTransaction(IsolationLevel isolationLevel)
  • 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-11T08:49:37+00:00Added an answer on June 11, 2026 at 8:49 am

    Look at this .
    This should solve your issues if its similar.

    Entity Framework Exception

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

Sidebar

Related Questions

Using C#.NET, i wrote a toolbar which is stored now into toolbar.cs file. I
Using mercurial, I've run into an odd problem where a line from one committer
Using Microsoft SQL Server 2005, is there any way to see when a table
Using the navigator.geolocation object in JavaScript. Trying to establish accurate ranges, but wondering exactly
Using android 2.3.3, I have a background Service which has a socket connection. There's
Using NSDateComponents I know how to get the day component, but this gives me
Using MVC2 I have an AJAX form which is posting to a bound model.
Using Delphi 2010. I am looking for (possibly) a function or procedure which can
Using php/html, I want to retrieve email addresses (plus other information) from MySQL and
Using emacs on Ubuntu 11.10. I want to connect to a SQL Server database

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.