tags:

views:

96

answers:

2

My program is downloading data from a server, once he received all the data, my program handles the data (performs checks like if that data is still on the server) and saves it into my database.

Now I was wondering what if my internet connection suddenly is disconnected, what happens with the data that I just was receiving. So I did a test. When I break the internet connection, my program stops fetching the data and goes to my method to check the data.

Now this wrong, because he will set some data already in my database that it isn't any more on the server, when it actually is.

The server is accessed by an API (webservices).

So my question is what is a good way to handle this kind of situation?

+4  A: 

Have you tried catching a WebException and handling it appropriately?

RandomNoob
I'm not catching this exception. I'll try that.
Gerbrand
+1  A: 

You need to find some way to determine if all of the data has been transferred. Either an end-of-file type of sequence in the data or you have a command that tells you how much data there is to be downloaded before you start downloading the data. That way, you can check if the connection is broken (ie. you stop receiving data) if you received everything you were supposed to.

TLiebe
Have to check the api documentation to see if I can access something that tells me how many data there is. But then what if the connection interrupts when I'm trying to get that information?
Gerbrand
If the connection is interrupted while you're trying to get the data size you should be able to catch an exception - either a timeout or some other error to indicate that the request was dropped.
TLiebe