Forum General

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

Xamarin Consumin Services

fytofyto CLMember ✭✭

I am consuming a service in Xamarin (Visual studio proyecto PCL) ... I want to enter Infringements and for this I enter in a form .... by pressing the button "Create Infringement" this throws me the message "Successfully created infringement" when in my SQL database does not create Nothing, I put a try and a catch to see the exceptions but it does not fall anywhere, I need help please, greetings to all!

public partial class NewInfraccionPage : ContentPage
{
private Usuarios usuarios;

public NewInfraccionPage(Usuarios usuarios)
{

    InitializeComponent();
    this.usuarios = usuarios;    
    saveButton.Clicked += saveButton_Clicked;
}

private async void saveButton_Clicked(object sender, EventArgs e)
{

    if (string.IsNullOrEmpty(motivoEntry.Text))
    {
        await DisplayAlert("Error", "Debe Ingresar un Motivo de Infraccion", "Aceptar");
        motivoEntry.Focus();
        return;
    }

    if (string.IsNullOrEmpty(patenteEntry.Text))
    {
        await DisplayAlert("Error", "Debe Ingresar una patente", "Aceptar");
        patenteEntry.Focus();
        return;
    }


    if (string.IsNullOrEmpty(lugarEntry.Text))
    {
        await DisplayAlert("Error", "Debe Ingresar un Lugar", "Aceptar");
        lugarEntry.Focus();
        return;
    }


    if (string.IsNullOrEmpty(lugarEntry.Text))
    {
        await DisplayAlert("Error", "Debe Ingresar un Lugar", "Aceptar");
        lugarEntry.Focus();
        return;
    }

    waitActivityIndicator.IsRunning = true;

    var infraccion = new Infraccion
    {               
        FechaInfraccion = fechaDatePicker.Date,              
        Motivo = motivoEntry.Text,
        PatenteAuto = patenteEntry.Text,
        LugarInfraccion = lugarEntry.Text,             
        MarcaAuto = marcaEntry.Text,
        ModeloAuto = modeloEntry.Text,
        RutInfractor = rutEntry.Text,
        Observacion = observacionEntry.Text,
    };


    //serializar el objeto infraccion
    var jsonRequest = JsonConvert.SerializeObject(infraccion);

    //codificamos el objeto infraccion y lo guardamos en una variable content
    var content = new StringContent(jsonRequest, Encoding.UTF8, "text/json");

    string result;
    try
    {                
        HttpClient client = new HttpClient();
        client.BaseAddress = new Uri("http://200.27.70.5/web_sit");
        string url = string.Format("/api/InfraccionsAPI", content);
        var response = await client.PostAsync(url, content);
        result = response.Content.ReadAsStringAsync().Result;
    }

    catch (Exception ex)
    {
        await DisplayAlert("Error", "No se encuentra la Conexion", "Aceptar");
        waitActivityIndicator.IsRunning = false;
        return;
    }

    waitActivityIndicator.IsRunning = false;
    await DisplayAlert("Confirmacion", "Infraccion correctamente creada!", "Aceptar");
    await Navigation.PushAsync(new InfraccionPage(usuarios));

}                

}}

Answers

  • kentuckerkentucker USMember ✭✭✭✭✭

    Sounds like the issue is in the web service not the mobile app

  • fytofyto CLMember ✭✭
    edited April 2017

    I believe the same .... My WEB API is the one that brings the default CRUD in MVC

    My PUT and POST methods are:

    // PUT: api/InfraccionsAPI/5
    [ResponseType(typeof(void))]
    public IHttpActionResult PutInfraccion(int id, Infraccion infraccion)
    {
    if (!ModelState.IsValid)
    {
    return BadRequest(ModelState);
    }

            if (id != infraccion.IdInfraccion)
            {
                return BadRequest();
            }
    
            db.Entry(infraccion).State = EntityState.Modified;
    
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InfraccionExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
    
            return StatusCode(HttpStatusCode.NoContent);
        }
    
        // POST: api/InfraccionsAPI
        [ResponseType(typeof(Infraccion))]
        public IHttpActionResult PostInfraccion(int id, Infraccion infraccion)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
    
            db.Infraccion.Add(infraccion);
            db.SaveChanges();
    
            return CreatedAtRoute("DefaultApi", new { id = infraccion.IdInfraccion }, infraccion);
        }
    

    I need a tip is my first web service !! I have lost 7 days in my project !! @kentucker

  • fytofyto CLMember ✭✭

    @kentucker add my Code.... can you helpme?

    public IHttpActionResult PutInfraccion(int id, Infraccion infraccion)
    {
    if (!ModelState.IsValid)
    {
    return BadRequest(ModelState);
    }

            if (id != infraccion.IdInfraccion)
            {
                return BadRequest();
            }
    
            db.Entry(infraccion).State = EntityState.Modified;
    
            try
            {
                db.SaveChanges();
            }
            catch (DbUpdateConcurrencyException)
            {
                if (!InfraccionExists(id))
                {
                    return NotFound();
                }
                else
                {
                    throw;
                }
            }
    
            return StatusCode(HttpStatusCode.NoContent);
        }
    
        // POST: api/InfraccionsAPI
        [ResponseType(typeof(Infraccion))]
        public IHttpActionResult PostInfraccion(int id, Infraccion infraccion)
        {
            if (!ModelState.IsValid)
            {
                return BadRequest(ModelState);
            }
    
            db.Infraccion.Add(infraccion);
            db.SaveChanges();
    
            return CreatedAtRoute("DefaultApi", new { id = infraccion.IdInfraccion }, infraccion);
        }
    
  • kentuckerkentucker USMember ✭✭✭✭✭

    Does the infraccion table have a primary key? Entity framework does not work right on tables without a primary key

  • fytofyto CLMember ✭✭

    Yes, I have my first key in my BD SQL (set primary key) and my MVC Backend ([Key] data notations) @kentucker
    If to build my model in MVC, occupy an Entity Data Model from EF Designer from Database.... Is it okay

Sign In or Register to comment.