Click here to Skip to main content
15,904,823 members
Please Sign up or sign in to vote.
3.67/5 (2 votes)
See more:
Hi
I run following code ,

MysqlConnection msql=new MySqlConnection()

msql.open();

C#
MySqlDataReader myDataReader;
        myDataReader = myCommand.ExecuteReader();
        myDataReader.Read();

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



 MySqlConnection scon = new MySqlConnection(System.Configuration.ConfigurationManager.AppSettings["ConnectionString"]);

                scon.Open();

                   string amenity = "insert into table(hotelid,amenitieid) values ('12','24')";

                        MySqlCommand cmd = new MySqlCommand(amenity, scon);


                 scon.close();
             }
          }

msql.close();


and i get error in line


SQL
if (myDataReader.HasRows)
       {
           while (myDataReader.Read())
           {<


as


An existing Connection was forcibly closed by the remote host
Posted
Updated 26-Jan-14 20:04pm
v2
Comments
Peter Leow 27-Jan-14 5:37am    
Not right. Could you explain what you want your code to do? You existing code is opening and closing database connection and inserting same values into a table repeatedly in a while loop. I suspect hotelid is a primary key. Use the 'Improve question' widget.

1 solution

This can be happened if your service throws an unhandled exception.Please check the below mentioned links for more info.

MSDN Forum

An existing connection was forcibly closed by the remote host

Exception Handling : ASP.NET Error Handling
 
Share this answer
 
v2
Comments
maulikshah1990 27-Jan-14 4:08am    
Is this error server side or my code is wrong somewhere
Sampath Lokuge 27-Jan-14 4:37am    
What happen when you put a debug and run ? Check whether is there any run time exception or not ?
maulikshah1990 27-Jan-14 4:41am    
It is on server,,how will i debug or catch run time exception and save in log

pls give code
Sampath Lokuge 27-Jan-14 4:55am    
You can use try/catch block on your code base and then use 'ExceptionUtility' class for log those errors.Please check the link which I have updated on my answer above.

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