locked
{System.OperationCanceledException: The operation was canceled. Exception RRS feed

  • Question

  • User-1701663573 posted

    Hi I am developing an app using xamarin and asp.net .net framework for API but I am getting an exception the operation was cancelled

    This is the code behind

      public partial class AddLiving : ContentPage
        {
            Response res = new Response();
           // private SQLiteAsyncConnection conn;
            public AddLiving()
            {
                InitializeComponent();
             //   conn = DependencyService.Get<ISqliteDb>().GetConnection();
            }
            private async void Button_Clicked(object sender, EventArgs e)
            {

               
                try
                {
                    var app = new Living()
                    {
                        fans = fan.Text,
                        lights = light.Text
                    };
                  //  await DisplayAlert("Hi",fan.Text,light.Text,"ok");

                
                        

                        string url = "https://192.168.1.5:80/api/Living/AddUser";
                        HttpClient client = new HttpClient();
                        string jsonData = JsonConvert.SerializeObject(app);
                        StringContent content = new StringContent(jsonData, Encoding.UTF8, "application/json");
                        HttpResponseMessage response = await client.PostAsync(url, content);
                        string result = await response.Content.ReadAsStringAsync();
                        res = JsonConvert.DeserializeObject<Response>(result);
                        if (res.responsestatus==true)
                        {
                            await Navigation.PopToRootAsync();
                        }
                        else
                        {
                          //  await DisplayAlert("Hmm..", "failed insert to in livingroom", "ok");
                        }
                        
                    
                    

                }
                catch(Exception ex)
                {
                    ex.ToString();
                }
            }
        }
    }

    This is the controller

     public class LivingController : ApiController
        {
            SqlConnection conn;
          
            void Connection()
            {
                string constring = ConfigurationManager.ConnectionStrings["getConnection"].ToString();
                conn = new SqlConnection(constring);
            }       
            public Response AddUser(Living living)

            {
                Response response = new Response();
                try
                {
                    
                    if (string.IsNullOrEmpty(living.fans) || string.IsNullOrEmpty(living.lights))
                    {
                        response.Message = "The Field Cannot be empty";
                        response.Responsestatus = false;

                    }
                    else
                    {
                        Connection();
                        SqlCommand cmd = new SqlCommand("spAddUsers", conn);
                        cmd.CommandType = CommandType.StoredProcedure;
                        cmd.Parameters.AddWithValue("@fans", living.fans);
                        cmd.Parameters.AddWithValue("@lights", living.lights);
                        conn.Open();
                        int i = cmd.ExecuteNonQuery();
                        conn.Close();
                        if (i >= 0)
                        {
                            response.Message = "Inserted succesfully";
                            response.Responsestatus = true;
                        }
                        else
                        {
                            response.Message = "failed to insert ";
                            response.Responsestatus = false;
                        }
                    }
                    
                }
                catch(Exception e)
                {
                    response.Message = e.Message;
                }
                return response;
            }
        }
    }

    Monday, October 5, 2020 5:12 AM

All replies

  • User-939850651 posted

    Hi nitishsantpur,

    According to your description, I did some searching for the problem, and I found that the exception may be that the called API did not respond.

    When the API call times out, similar problems will occur. You could refer to HttpClient.Timeout Property for more details.

    From the code point of view, this seems to be a data insertion operation. When the issue is occurs, has the data been inserted into the database?

    If I misunderstood something, please let me know.

    Best regards,

    Xudong Peng

    Tuesday, October 6, 2020 10:09 AM
  • User-1701663573 posted

    No, the data isn't inserted into the database I am a novice and using xamarin forms and asp.net project in the same solution but I am not sure if I have to run both the projects because for this I only ran the xamarin project. Also when I used the inner exception statement it shows null reference exception might be that is causing issue

    Thanks for the reply!! 

    Wednesday, October 7, 2020 4:05 AM
  • User-939850651 posted

    Hi nitishsantpur,

    No, the data isn't inserted into the database I am a novice and using xamarin forms and asp.net project in the same solution but I am not sure if I have to run both the projects because for this I only ran the xamarin project. Also when I used the inner exception statement it shows null reference exception might be that is causing issue

    This should be the cause of the problem. Access to Web API controllers and actions are based on urls. So now that they are on separate projects you need to run both projects at the same time to make your API available.

    Have you tried to start both projects at the same time and then test them? I think it will work.

    Best regards,

    Xudong Peng

    Wednesday, October 7, 2020 10:01 AM
  • User-1701663573 posted

    Thanks for the reply but I've mentioned my projects are in the same  solution but when I try run the API with separate instance it doesn't let my emulator run

    Thursday, October 8, 2020 3:44 AM
  • User-939850651 posted

    Hi nitishsantpur,

    Thanks for the reply but I've mentioned my projects are in the same  solution but when I try run the API with separate instance it doesn't let my emulator run

    Regarding how to start multiple projects in the same solution, you could try:

    Go to Solution properties → Common Properties → Startup Project and select Multiple startup projects.

    You could also refer to this case:

    Running two projects at once in Visual Studio

    Best regards,

    Xudong Peng

    Thursday, October 8, 2020 10:04 AM
  • User-1701663573 posted

    thanks will try

    Thursday, October 8, 2020 1:55 PM