Click here to Skip to main content
15,885,141 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
When i run my MVC(C#,use entity Framework+MySql) project, it show that:There is already an open DataReader associated with this Connection which must be closed first. And the error code is:
[assembly: EdmRelationshipAttribute("blogModel", "cid", "cls", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(blog.Models.cls), "news", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(blog.Models.news), true)] [assembly: EdmRelationshipAttribute("blogModel", "uid", "users", System.Data.Metadata.Edm.RelationshipMultiplicity.One, typeof(blog.Models.users), "news", System.Data.Metadata.Edm.RelationshipMultiplicity.Many, typeof(blog.Models.news), true)]
[XmlIgnoreAttribute()]     [SoapIgnoreAttribute()]     [DataMemberAttribute()]     [EdmRelationshipNavigationPropertyAttribute("blogModel", "cid", "cls")]     public cls cls     {         get         {             return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<cls>("blogModel.cid", "cls").Value;         }         set         {             ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<cls>("blogModel.cid", "cls").Value = value;         }     }     /// <summary>     /// 没有元数据文档可用。     /// </summary>     [BrowsableAttribute(false)]     [DataMemberAttribute()]     public EntityReference<cls> clsReference     {         get         {             return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<cls>("blogModel.cid", "cls");         }         set         {             if ((value != null))             {                 ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<cls>("blogModel.cid", "cls", value);             }         }     }      /// <summary>     /// 没有元数据文档可用。     /// </summary>     [XmlIgnoreAttribute()]     [SoapIgnoreAttribute()]     [DataMemberAttribute()]     [EdmRelationshipNavigationPropertyAttribute("blogModel", "uid", "users")]     public users users     {         get         {             return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<users>("blogModel.uid", "users").Value;         }         set         {             ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<users>("blogModel.uid", "users").Value = value;         }     }     /// <summary>     /// 没有元数据文档可用。     /// </summary>     [BrowsableAttribute(false)]     [DataMemberAttribute()]     public EntityReference<users> usersReference     {         get         {             return ((IEntityWithRelationships)this).RelationshipManager.GetRelatedReference<users>("blogModel.uid", "users");         }         set         {             if ((value != null))             {                 ((IEntityWithRelationships)this).RelationshipManager.InitializeRelatedReference<users>("blogModel.uid", "users", value);             }         }     }      #endregion 

THis code is in the model file xx.Designer.cs,I want to know how to end the GetRelateReference or close DataReader, there no DataReader in xx.Designer.cs. and when i add MultipleActiveResultSets=true to connection string in web.config, i try again, it show me: The format of the initialization string does not meet specifications, how i can fix this problem?
/// 请使用应用程序配置文件的"blogEntities"部分中的连接字符串初始化新 blogEntities 对象。     ///      public blogEntities() : base("name=blogEntities", "blogEntities")     {         this.ContextOptions.LazyLoadingEnabled = true;         OnContextCreated();     }      /// <summary>     /// 初始化新的 blogEntities 对象。     /// </summary>     public blogEntities(string connectionString) : base(connectionString, "blogEntities")     {         this.ContextOptions.LazyLoadingEnabled = true;         OnContextCreated();     }      /// <summary>     /// 初始化新的 blogEntities 对象。     /// </summary>     public blogEntities(EntityConnection connection) : base(connection, "blogEntities")     {         this.ContextOptions.LazyLoadingEnabled = true;         OnContextCreated();     }      #endregion <pre lang="c#">
Posted
Updated 18-Jul-16 2:02am
v2

The error says that that the datareader is in open state else where. first close the datareader and the open it when u want to use it again. It is simlar to connection object.
something like this
C#
//some function
connectionobject.open()
//do some code
sqldatareader dr = cmd.executereader();
dr.open();
//do some code
dr.close();
//some function2
 connectioobject.open();
//do some code
sqldatareader dr1 = cmd.executereader();
dr1.open();
//do some code
dr1.close();


This should solve your issue
 
Share this answer
 
v2
Comments
gavinv 2-Jul-12 4:30am    
thanx, but i'm beginer in MVC, so should you post an extractly code for me? since no SQLDATAREADER in the Designer.cs file.
Arjun YK 2-Jul-12 5:17am    
If u can send me the coe may be I'll try to solve the issue
gavinv 2-Jul-12 7:37am    
thanx,maybe i will send code to you later,now i get some code maybe can solve this problem, but the last line i can find what's it,would you explain it or collected it to me?
public static void RemoveAllRelatedObjects(EntityObject entity)
{

if (entity == null)
{ return;
}

var relationshipManager = ((IEntityWithRelationships)entity).RelationshipManager;

if (relationshipManager == null)
{ return;

}

foreach (var re in relationshipManager.GetAllRelatedEnds())

{
var enumerator = re.GetEnumerator();

if (enumerator == null)
{

continue;
}

var allObject = new List();
while (enumerator.MoveNext())
{
allObject.Add((IEntityWithRelationships)enumerator.Current);

}
foreach(var v in allObject)
{

re.Remove(v);
}

MiRelatedEndSetIsLoad.Invoke(re, new object[] {false});

}
}
Quote:
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Configuration;
using System.Threading;
using System.Net;
using System.IO;

namespace ConsoleApplication2
{

    class Program
    {
        static void Main(string[] args)
        {
            Thread t1 = new Thread(Program.Go);

            //Thread t1 = new Thread(() => Go());

            t1.Start(); // Run Go() on the new thread.
           
           
        }// Simultaneously run Go() in the main thread. 

      

        public static void Go() // public static void Go()
        {
            try
            {

                string ConnectionString = "Server=localhost; Database=XXX; Uid=root; Pwd=XXXX12;";
            
               
                using (MySqlConnection conn = new MySqlConnection(ConnectionString))
                {

                    conn.Open();

                    using (MySqlCommand cmd = new MySqlCommand("select iSno,vcMsisdn,vcMessage from smsmt where iStatus=0;", conn))
                    {
                        MySqlDataReader reader = cmd.ExecuteReader();

                        if (reader.HasRows)
                        {
                            while (reader.Read())
                            {
                               

                                string url = "iSno=" + reader["iSno"].ToString() + "&from=" + reader["vcMsisdn"].ToString() + "&msg=" + reader["vcMessage"].ToString();

                                Console.WriteLine(url);

                                
                                //string res = Program.HttpWebResponseInfo(url);
                                

                                MySqlCommand  cmd1 = new MySqlCommand("UPDATE smsmt set iStatus=1 where iSno="+reader["iSno"],conn) ;

                                int rows = cmd1.ExecuteNonQuery();

                                Console.WriteLine("Record Updated Successfully");
                                Console.ReadLine();
                        
                                    }
                              reader.Close();
                            

                            }
                       
                        }
                        
                      
                    }
                                   }
               
            
            catch (Exception ex)
            {
                //Log exception
                //Display Error message
            }
            
        }


    }
}














error:----------
Quote:
There is already an open DataReader associated with this Connection which must be closed first
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900