here are my two functions:
public void SetCompanies()
//set the Companies table from Shret.net DataBase
{
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("http://api.sherut.net/?method=Company");
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
try
{
webClient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void webClient_OpenReadCompleted(object sender, OpenWriteCompletedEventArgs e)
{
try
{
DataContractJsonSerializer serializer = null;
var companies = (Companies)serializer.ReadObject(e.Result);
foreach (Company c in companies.data)
{
MessageBox.Show(c.Name + " " + c.CompanyID);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
and this is the error i get:
"No overload for 'webClient_OpenReadCompleted' matches delegate
'System.Net.OpenReadCompletedEventHandler'"
i dont understand why, because i wrote the handler after this function......
Thanks in advance!
OpenReadCompleted doesn't take OpenWriteCompletedEventArgs.
Related
I'm unable to debug the the SaveFileToDisk method. Please me help me out how to resolve the issue. Thanks in advance.
private void Download_TapGestureRecognizer_Tapped(object sender, EventArgs e)
{
try
{
PortalTeacherDairyAttachmentList attachment = ((TappedEventArgs)e).Parameter as PortalTeacherDairyAttachmentList;
byte[] attachmentbyte = attachment.Attachment;
DependencyService.Get<IDownloadManager>().SaveFileToDisk(attachment.FileName,attachmentbyte);
}
catch (Exception ex)
{
}
}
SaveFileToDiskCode:
{
Toast.MakeText(Android.App.Application.Context, "Downloading started...", ToastLength.Long).Show();
var dir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryDownloads);
var absolutepath = dir.AbsolutePath;
string name = filename;
string filePath = System.IO.Path.Combine(absolutepath, name);
try
{
System.IO.File.WriteAllBytes(filePath, data);
var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile);
mediaScanIntent.SetData(Uri.FromFile(new File(filePath)));
Xamarin.Forms.Forms.Context.SendBroadcast(mediaScanIntent);
OpenFile(filePath, filename);
Toast.MakeText(Android.App.Application.Context, "File downloaded at" + filePath, ToastLength.Long).Show();
CreateNotificationChannel(name);
}
catch (System.Exception e)
{
System.Console.WriteLine(e.ToString());
Toast.MakeText(Android.App.Application.Context, "Error occured", ToastLength.Long).Show();
}
}
I don't understand why but I'm receiving Flurl Exceptions and those are not being caught by the try/catch block. Any ideas on why that's happening?
Here's the code:
try
{
var x = await Utils.Sales.GetUrl()
.PostJsonAsync(new Sale
{
MerchantId = Constants.Sandbox.MerchantId
})
.ReceiveJson<Sale>();
var b = x;
}
catch (FlurlHttpTimeoutException)
{
//LogError("Timed out!"); //todo:
}
catch (FlurlHttpException ex)
{
var x = ex.Message;
//todo:
//if (ex.Call.Response != null)
// LogError("Failed with response code " + call.Response.StatusCode);
//else
// LogError("Totally failed before getting a response! " + ex.Message);
}
catch (Exception ex)
{
var a = ex.Message;
}
Here's the output (the only reason why I know the exception is being thrown):
Maybe this page will help https://msdn.microsoft.com/zh-cn/library/jj619227.aspx
Sorry don't have a english version, you can try google translate it.
It's someting wrong with you catch exception type or await code.
Try this way catch your exception:
```
try
{
await t1;
}
catch (AggregateException ex)
{
var innerEx = ex.InnerExceptions[0];
if (innerEx is NotSupportedException)
{
...
}
else if (innerEx is NotImplementedException)
{
...
}
else
{
...
}
}
```
I have this code
protected void Button_Click(object sender, EventArgs e)
{
try
{
// some code
con.Open();
string result = command.ExecuteScalar().ToString();
if (result != string.Empty)
{
// some code
Response.Redirect("Default.aspx");
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
con.Close();
}
It gives an exception from Response.Redirect("Default.aspx");
ex: Thread was being aborted.
any idea why?
thanx
Redirecting from within a Try...Catch statement will result in this Exception being thrown, so this is not what you want to do.
I would update your code to;
string result = string.Empty;
try
{
// some code
con.Open();
result = command.ExecuteScalar().ToString();
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
con.Close();
}
if (result != string.Empty)
{
// some code
Response.Redirect("Default.aspx");
}
This is a typical Exception that is thrown by ASP.NET when performing a redirect. It's quite well documented on the Interweb.
Try the following catch block to swallow the exception and all should be fine. It's supposed to do nothing!
catch(ThreadAbortException)
{
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
finally
{
con.Close();
}
I got a problem in my WP-7 app.
I have an event when I read from the cloud - OpenReadCompletedEventHandler,
but somehow when the event occurs the EventHandler cannot be executed.
Here is my functions:
public void SetCategories()
//set the Companies table from Shret.net DataBase
{
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("http://api.sherut.net/?method=Category");
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
try
{
webClient.OpenReadAsync(uri);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
DataContractJsonSerializer serializer = null;
try
{
serializer = new DataContractJsonSerializer(typeof(Categories));
var categories = (Categories)serializer.ReadObject(e.Result);
// foreach (Company c in companies.data)
// MessageBox.Show(c.Name + " " + c.CompanyID+" "+c.CompanyGUID);
//לכתוב לבסיס נתונים באפליקציה
BuildCategoriesDB(); //build the local data base
AddCategoriesData(categories);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
It reads from the cloud, but it doesn't go inside the handler: webClient_OpenReadCompleted
Check to make sure the event is being created and "thrown".
I'm trying to read from a Uri which i created and to display it on windows phone 7 app.
(I'm doing this tutorial:http://msdn.microsoft.com/en-us/windowsmobile/Video/hh237494).
My problem is that the program doesnt get into the OpenReadCompletedEventHandler and i dont know why. (i putted message box in order to debug and i found out that the program doesnt get into the OpenReadCompletedEventHandler). Here is the relevant code:
void myButton_Click(object sender, RoutedEventArgs e)
{
try
{
WebClient webClient = new WebClient();
Uri uri = new Uri("http://localhost:44705/Service1.svc/GetAllBrands");
webClient.OpenReadCompleted += new OpenReadCompletedEventHandler(webClient_OpenReadCompleted);
try
{
webClient.OpenWriteAsync(uri);
MessageBox.Show("opening sucsseded");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
void webClient_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
MessageBox.Show("OpenRead Handler");
// OpenWriteCompletedEventArgs temp = (OpenWriteCompletedEventArgs)e;
DataContractJsonSerializer serializer = null;
try
{
serializer = new DataContractJsonSerializer(typeof(ObservableCollection<Brand>));
ObservableCollection<Brand> Brands = serializer.ReadObject(e.Result) as ObservableCollection<Brand>;
foreach (Brand b in Brands)
{
int id = b.BrandId;
string name = b.BrandName;
listBrands.Items.Add(id + " " + name);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
Thanks in advance!
I have never used this but a quick google takes me to this page on MSDN - http://msdn.microsoft.com/en-us/library/system.net.webclient.openreadcompleted.aspx
This should tell you why it's not working - because you are using a read event for a write operation. You should be using OpenWriteCompletedEventHandler with OpenWriteAsync as per this page on MSDN - http://msdn.microsoft.com/en-us/library/system.net.webclient.openwritecompleted.aspx