6

When I pause my application and then resume after a while, I get this error message:

System.IO.IOException: Unable to read data from the transport connection. Connection reset by peer ---> system.net.sockets.....

I've assigned a class which defines all my objects inside my activity example:

public class Connection: Activity
{
    protected SqlConnection con;
    protected string MyIp;

    protected override void OnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);

        var prefs = Application.Context.GetSharedPreferences("Preferences", FileCreationMode.Private);
        MyIp = prefs.GetString("IpAdress", null);

        con = new SqlConnection("Data Source = " + MyIp + "; Initial Catalog = WiOrder; user id = admin; password = 1234;Connection Timeout=5");

    }
}

Here is how I call this class inside activity:

public class Main : Connection
{
    protected override void OnCreate(Bundle savedInstanceState)
    {

        base.OnCreate(savedInstanceState);
       //Rest of my code
       con.Open();
       SqlCommand cmd = new SqlCommand (//query,con);
       //.........

    }
}
Anas Alweish
  • 2,818
  • 4
  • 30
  • 44
daa daa
  • 143
  • 1
  • 2
  • 8
  • So `MyIp` is an external and publicly accessible IP address (i.e. *not* localhost) ?and you are trying to connect to Microsoft SQL Server database directly from within a mobile app? – SushiHangover Jul 08 '18 at 21:23
  • Ip is from a computer with local sql server database; and i'm trying to connect directly from my mobile. Is there any case to delete my sql connection string after a long pause? – daa daa Jul 08 '18 at 21:46
  • @daadaa did you get any solution? I am facing the same issue – UserID0908 Dec 29 '18 at 07:39

1 Answers1

3

I faced the same issue and I was getting this exception (sometimes) when trying to connect to the server.

Unable to read data from the transport connection: Connection reset by peer.

After many searches, I found this answer

Quote from the answer

This error usually means that the target machine is running, but the service that you're trying to connect to is not available. (Either it stopped, crashed, or is busy with another request.)

In English: The connection to the machine (remote host/server/PC that the service runs at) was made but since the service was not available on that machine, the machine didn't know what to do with the request.

So, you should check your network connection

For more information,

Take a look at the similar questions here and here

IOException

Community
  • 1
  • 1
Anas Alweish
  • 2,818
  • 4
  • 30
  • 44