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:
I run following code ,

MysqlConnection msql=new MySqlConnection();

MySqlDataReader myDataReader;
        myDataReader = myCommand.ExecuteReader();

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

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


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

                        MySqlCommand cmd = new MySqlCommand(amenity, scon);



and i get error in line

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


An existing Connection was forcibly closed by the remote host
Updated 26-Jan-14 20:04pm
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
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