How to insert data into database using REST API in xamarin forms

I want to insert the data into the database using the REST API through xamarin forms ? can anyone help me please and I am using the POSTAYSNC to post the data.

Best Answers

Answers

  • JohnHardmanJohnHardman GBUniversity mod

    @viharip said:
    I want to insert the data into the database using the REST API through xamarin forms ? can anyone help me please and I am using the POSTAYSNC to post the data.

    Which part are you having difficulty with? Posting the HTTP message from your app? Receiving the HTTP message on the server (Web API)? Inserting the data into a database on the server?

  • viharipviharip Member ✭✭

    can you give the sample code for sending the data

  • JohnHardmanJohnHardman GBUniversity mod
    edited August 10

    @viharip said:
    can you give the sample code for sending the data

    There are many, many samples available for this online. It's also covered by many books. Have you Googled how to do it? Have you checked Xamarin's/Microsoft's own documentation and samples?

    If you've written some code and it's not working, post the code and describe the problem.

  • viharipviharip Member ✭✭
    edited August 12

    string reftype;

                reftype = "D";
    
    
            HttpClient _client = new HttpClient();
    
            Patient pt = new Patient()
            {
    
    
                Reftype = reftype,
                //Refby = txtPatientName.Text,
                Refby = refby,
                DoctorContact = txtContact.Text,
                // HospitalName = txtAddress.Text,
    
    
                hospital = "Pradhama",
                Hornifics = Hornifics.SelectedItem.ToString(),
                PatientName = SearchBar.Text,
                Address = txtAddress.Text,
                Email = txtEmail.Text,
                Contact = txtContact.Text,
                Age = Convert.ToInt32(txtAge.Text),
                Sex = "1",
                ConsultDoctor = "1"
    
    
            };
    
            var json = JsonConvert.SerializeObject(pt);
            var content = new StringContent(json, Encoding.UTF8, "application/json");
    
            var response = _client.PostAsync("http://XXXX/api/patientapi/InsertPatient", content);
    

    this is my code can you check whats the wrong i written

  • viharipviharip Member ✭✭
    Problem is that unable to insert the data into database i m getting this response
    Id = 2, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed
    2)insertpatient is a method for inserting the data
  • AnubhavRanjanAnubhavRanjan INXamarin Team Xamurai

    @viharip Like @JohnHardman mentioned, you will have to make use of await before making the call

    var response = await _client.PostAsync("http://XXXX/api/patientapi/InsertPatient", content);
    

    Else you will end up getting just a return of type Task.
    Once the method is made to use await, you will get proper response from the API

  • JohnHardmanJohnHardman GBUniversity mod

    @viharip said:
    Problem is that unable to insert the data into database i m getting this response
    Id = 2, Status = WaitingForActivation, Method = "{null}", Result = "{Not yet computed
    2)insertpatient is a method for inserting the data

    As I said before:

    (1) You will want to put "await" before "_client.PostAsync"

  • viharipviharip Member ✭✭

    Thank you @JohnHardman and @AnubhavRanjan after adding await its work and i have one more doubt can you help me out I am binding two values to the picker and I want to get the value of the picker


    I want to get the selected value from that

  • JohnHardmanJohnHardman GBUniversity mod

    @viharip said:
    Thank you @JohnHardman and @AnubhavRanjan after adding await its work

    Don't forget to Like and/or mark as Accepted Answer any posts that helped you.

    @viharip said:
    i have one more doubt can you help me out I am binding two values to the picker and I want to get the value of the picker

    As that is unrelated to the original question, can you ask it in a new thread please.

  • viharipviharip Member ✭✭

    ok i will

Sign In or Register to comment.