POST Data To ASP.NET API With Xamarin












1















I am using C# asp.net and attempting to create my first Xamarin app. I have altered my asp.net API to hold the below syntax



private SqlConnection con;
private SqlCommand com;

private void connection()
{
string constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
con = new SqlConnection(constr);
}

[HttpPost]
public string AddUser(User user)
{
connection();
com = new SqlCommand("InsertData", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@FName", user.FName);
com.Parameters.AddWithValue("@Lname", user.LName);
com.Parameters.AddWithValue("@Phone", user.Phone);
com.Parameters.AddWithValue("@Compnay", user.Company);
com.Parameters.AddWithValue("@Email", user.Email);
com.Parameters.AddWithValue("@Pass", user.Pass);
com.Parameters.AddWithValue("@Registrationdate", user.Registrationdate);
con.Open();
int i = com.ExecuteNonQuery();
con.Close();
if (i >= 1)
{
return "New User Added Successfully";
}
else
{
return "Failed to Add User";
}
}

[HttpGet]
public string Get()
{
return "";
}


And I Have in my Xamarin syntax the below



void OnRegisterTap(object sender, EventArgs e)
{
InsertUser().ConfigureAwait(true);
}

private async Task InsertUser()
{
try
{
var httpClient = new HttpClient();
var url = "http://XXX.XXX.X.XXX:8888/api/user/adduser";
var data = new
{
FName = fname.Text,
LName = lname.Text,
Company = company.Text,
Email = Email.Text,
Pass = Password.Text,
Registrationdate = DateTime.UtcNow.ToString()
};
var jsonData = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
var result = await httpClient.PostAsync(url, jsonData);
}
catch (Exception e)
{
Console.WriteLine(e.Message);
}
}


Now there is no error thrown when I press the button from my Xamarin app, I have verified that all the variables holds the appropriate values, however my issue is that no data is actually input into the database.



What step did I miss or did I improperly code that is keeping the data from being inserted?










share|improve this question



























    1















    I am using C# asp.net and attempting to create my first Xamarin app. I have altered my asp.net API to hold the below syntax



    private SqlConnection con;
    private SqlCommand com;

    private void connection()
    {
    string constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
    con = new SqlConnection(constr);
    }

    [HttpPost]
    public string AddUser(User user)
    {
    connection();
    com = new SqlCommand("InsertData", con);
    com.CommandType = CommandType.StoredProcedure;
    com.Parameters.AddWithValue("@FName", user.FName);
    com.Parameters.AddWithValue("@Lname", user.LName);
    com.Parameters.AddWithValue("@Phone", user.Phone);
    com.Parameters.AddWithValue("@Compnay", user.Company);
    com.Parameters.AddWithValue("@Email", user.Email);
    com.Parameters.AddWithValue("@Pass", user.Pass);
    com.Parameters.AddWithValue("@Registrationdate", user.Registrationdate);
    con.Open();
    int i = com.ExecuteNonQuery();
    con.Close();
    if (i >= 1)
    {
    return "New User Added Successfully";
    }
    else
    {
    return "Failed to Add User";
    }
    }

    [HttpGet]
    public string Get()
    {
    return "";
    }


    And I Have in my Xamarin syntax the below



    void OnRegisterTap(object sender, EventArgs e)
    {
    InsertUser().ConfigureAwait(true);
    }

    private async Task InsertUser()
    {
    try
    {
    var httpClient = new HttpClient();
    var url = "http://XXX.XXX.X.XXX:8888/api/user/adduser";
    var data = new
    {
    FName = fname.Text,
    LName = lname.Text,
    Company = company.Text,
    Email = Email.Text,
    Pass = Password.Text,
    Registrationdate = DateTime.UtcNow.ToString()
    };
    var jsonData = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
    var result = await httpClient.PostAsync(url, jsonData);
    }
    catch (Exception e)
    {
    Console.WriteLine(e.Message);
    }
    }


    Now there is no error thrown when I press the button from my Xamarin app, I have verified that all the variables holds the appropriate values, however my issue is that no data is actually input into the database.



    What step did I miss or did I improperly code that is keeping the data from being inserted?










    share|improve this question

























      1












      1








      1








      I am using C# asp.net and attempting to create my first Xamarin app. I have altered my asp.net API to hold the below syntax



      private SqlConnection con;
      private SqlCommand com;

      private void connection()
      {
      string constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
      con = new SqlConnection(constr);
      }

      [HttpPost]
      public string AddUser(User user)
      {
      connection();
      com = new SqlCommand("InsertData", con);
      com.CommandType = CommandType.StoredProcedure;
      com.Parameters.AddWithValue("@FName", user.FName);
      com.Parameters.AddWithValue("@Lname", user.LName);
      com.Parameters.AddWithValue("@Phone", user.Phone);
      com.Parameters.AddWithValue("@Compnay", user.Company);
      com.Parameters.AddWithValue("@Email", user.Email);
      com.Parameters.AddWithValue("@Pass", user.Pass);
      com.Parameters.AddWithValue("@Registrationdate", user.Registrationdate);
      con.Open();
      int i = com.ExecuteNonQuery();
      con.Close();
      if (i >= 1)
      {
      return "New User Added Successfully";
      }
      else
      {
      return "Failed to Add User";
      }
      }

      [HttpGet]
      public string Get()
      {
      return "";
      }


      And I Have in my Xamarin syntax the below



      void OnRegisterTap(object sender, EventArgs e)
      {
      InsertUser().ConfigureAwait(true);
      }

      private async Task InsertUser()
      {
      try
      {
      var httpClient = new HttpClient();
      var url = "http://XXX.XXX.X.XXX:8888/api/user/adduser";
      var data = new
      {
      FName = fname.Text,
      LName = lname.Text,
      Company = company.Text,
      Email = Email.Text,
      Pass = Password.Text,
      Registrationdate = DateTime.UtcNow.ToString()
      };
      var jsonData = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
      var result = await httpClient.PostAsync(url, jsonData);
      }
      catch (Exception e)
      {
      Console.WriteLine(e.Message);
      }
      }


      Now there is no error thrown when I press the button from my Xamarin app, I have verified that all the variables holds the appropriate values, however my issue is that no data is actually input into the database.



      What step did I miss or did I improperly code that is keeping the data from being inserted?










      share|improve this question














      I am using C# asp.net and attempting to create my first Xamarin app. I have altered my asp.net API to hold the below syntax



      private SqlConnection con;
      private SqlCommand com;

      private void connection()
      {
      string constr = ConfigurationManager.ConnectionStrings["getconn"].ToString();
      con = new SqlConnection(constr);
      }

      [HttpPost]
      public string AddUser(User user)
      {
      connection();
      com = new SqlCommand("InsertData", con);
      com.CommandType = CommandType.StoredProcedure;
      com.Parameters.AddWithValue("@FName", user.FName);
      com.Parameters.AddWithValue("@Lname", user.LName);
      com.Parameters.AddWithValue("@Phone", user.Phone);
      com.Parameters.AddWithValue("@Compnay", user.Company);
      com.Parameters.AddWithValue("@Email", user.Email);
      com.Parameters.AddWithValue("@Pass", user.Pass);
      com.Parameters.AddWithValue("@Registrationdate", user.Registrationdate);
      con.Open();
      int i = com.ExecuteNonQuery();
      con.Close();
      if (i >= 1)
      {
      return "New User Added Successfully";
      }
      else
      {
      return "Failed to Add User";
      }
      }

      [HttpGet]
      public string Get()
      {
      return "";
      }


      And I Have in my Xamarin syntax the below



      void OnRegisterTap(object sender, EventArgs e)
      {
      InsertUser().ConfigureAwait(true);
      }

      private async Task InsertUser()
      {
      try
      {
      var httpClient = new HttpClient();
      var url = "http://XXX.XXX.X.XXX:8888/api/user/adduser";
      var data = new
      {
      FName = fname.Text,
      LName = lname.Text,
      Company = company.Text,
      Email = Email.Text,
      Pass = Password.Text,
      Registrationdate = DateTime.UtcNow.ToString()
      };
      var jsonData = new StringContent(JsonConvert.SerializeObject(data), Encoding.UTF8, "application/json");
      var result = await httpClient.PostAsync(url, jsonData);
      }
      catch (Exception e)
      {
      Console.WriteLine(e.Message);
      }
      }


      Now there is no error thrown when I press the button from my Xamarin app, I have verified that all the variables holds the appropriate values, however my issue is that no data is actually input into the database.



      What step did I miss or did I improperly code that is keeping the data from being inserted?







      c# asp.net xamarin xamarin.forms






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 22 '18 at 1:07









      Doctor FordDoctor Ford

      638




      638
























          1 Answer
          1






          active

          oldest

          votes


















          0














          Try changing



          var result = await client.PostAsync(url, jsonData);


          To



          var response = await client.PostAsync(url, jsonData);
          var result = await response.Content.ReadAsStringAsync();


          Add FromBody to your controller (assuming User class is correct)



          [HttpPost]
          public string AddUser([FromBody] User user)


          Use fiddler to Watch your request & response also your controller should return a proper HTTP response.






          share|improve this answer
























          • I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile error Type 'UserController' already defines a member called 'AddUser' with the same parameter types API. --> how do I use fiddler to watch my request & response?

            – Doctor Ford
            Nov 22 '18 at 1:39











          • once installed & running fiddler will capture any HTTP requests, when you see the request going to your address select it and on the right you will see the screen split, the top shows the request, the bottom shows the response, look for a response message like 404 not found etc

            – saj
            Nov 22 '18 at 2:07











          • Would I install it on the server or on the machine I'm making the request from?

            – Doctor Ford
            Nov 22 '18 at 2:09











          • the error message is simply saying a method called AddUser already exists ?

            – saj
            Nov 22 '18 at 2:09











          • install it on your client PC

            – saj
            Nov 22 '18 at 2:10











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422572%2fpost-data-to-asp-net-api-with-xamarin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          Try changing



          var result = await client.PostAsync(url, jsonData);


          To



          var response = await client.PostAsync(url, jsonData);
          var result = await response.Content.ReadAsStringAsync();


          Add FromBody to your controller (assuming User class is correct)



          [HttpPost]
          public string AddUser([FromBody] User user)


          Use fiddler to Watch your request & response also your controller should return a proper HTTP response.






          share|improve this answer
























          • I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile error Type 'UserController' already defines a member called 'AddUser' with the same parameter types API. --> how do I use fiddler to watch my request & response?

            – Doctor Ford
            Nov 22 '18 at 1:39











          • once installed & running fiddler will capture any HTTP requests, when you see the request going to your address select it and on the right you will see the screen split, the top shows the request, the bottom shows the response, look for a response message like 404 not found etc

            – saj
            Nov 22 '18 at 2:07











          • Would I install it on the server or on the machine I'm making the request from?

            – Doctor Ford
            Nov 22 '18 at 2:09











          • the error message is simply saying a method called AddUser already exists ?

            – saj
            Nov 22 '18 at 2:09











          • install it on your client PC

            – saj
            Nov 22 '18 at 2:10
















          0














          Try changing



          var result = await client.PostAsync(url, jsonData);


          To



          var response = await client.PostAsync(url, jsonData);
          var result = await response.Content.ReadAsStringAsync();


          Add FromBody to your controller (assuming User class is correct)



          [HttpPost]
          public string AddUser([FromBody] User user)


          Use fiddler to Watch your request & response also your controller should return a proper HTTP response.






          share|improve this answer
























          • I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile error Type 'UserController' already defines a member called 'AddUser' with the same parameter types API. --> how do I use fiddler to watch my request & response?

            – Doctor Ford
            Nov 22 '18 at 1:39











          • once installed & running fiddler will capture any HTTP requests, when you see the request going to your address select it and on the right you will see the screen split, the top shows the request, the bottom shows the response, look for a response message like 404 not found etc

            – saj
            Nov 22 '18 at 2:07











          • Would I install it on the server or on the machine I'm making the request from?

            – Doctor Ford
            Nov 22 '18 at 2:09











          • the error message is simply saying a method called AddUser already exists ?

            – saj
            Nov 22 '18 at 2:09











          • install it on your client PC

            – saj
            Nov 22 '18 at 2:10














          0












          0








          0







          Try changing



          var result = await client.PostAsync(url, jsonData);


          To



          var response = await client.PostAsync(url, jsonData);
          var result = await response.Content.ReadAsStringAsync();


          Add FromBody to your controller (assuming User class is correct)



          [HttpPost]
          public string AddUser([FromBody] User user)


          Use fiddler to Watch your request & response also your controller should return a proper HTTP response.






          share|improve this answer













          Try changing



          var result = await client.PostAsync(url, jsonData);


          To



          var response = await client.PostAsync(url, jsonData);
          var result = await response.Content.ReadAsStringAsync();


          Add FromBody to your controller (assuming User class is correct)



          [HttpPost]
          public string AddUser([FromBody] User user)


          Use fiddler to Watch your request & response also your controller should return a proper HTTP response.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 22 '18 at 1:31









          sajsaj

          1,5941329




          1,5941329













          • I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile error Type 'UserController' already defines a member called 'AddUser' with the same parameter types API. --> how do I use fiddler to watch my request & response?

            – Doctor Ford
            Nov 22 '18 at 1:39











          • once installed & running fiddler will capture any HTTP requests, when you see the request going to your address select it and on the right you will see the screen split, the top shows the request, the bottom shows the response, look for a response message like 404 not found etc

            – saj
            Nov 22 '18 at 2:07











          • Would I install it on the server or on the machine I'm making the request from?

            – Doctor Ford
            Nov 22 '18 at 2:09











          • the error message is simply saying a method called AddUser already exists ?

            – saj
            Nov 22 '18 at 2:09











          • install it on your client PC

            – saj
            Nov 22 '18 at 2:10



















          • I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile error Type 'UserController' already defines a member called 'AddUser' with the same parameter types API. --> how do I use fiddler to watch my request & response?

            – Doctor Ford
            Nov 22 '18 at 1:39











          • once installed & running fiddler will capture any HTTP requests, when you see the request going to your address select it and on the right you will see the screen split, the top shows the request, the bottom shows the response, look for a response message like 404 not found etc

            – saj
            Nov 22 '18 at 2:07











          • Would I install it on the server or on the machine I'm making the request from?

            – Doctor Ford
            Nov 22 '18 at 2:09











          • the error message is simply saying a method called AddUser already exists ?

            – saj
            Nov 22 '18 at 2:09











          • install it on your client PC

            – saj
            Nov 22 '18 at 2:10

















          I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile error Type 'UserController' already defines a member called 'AddUser' with the same parameter types API. --> how do I use fiddler to watch my request & response?

          – Doctor Ford
          Nov 22 '18 at 1:39





          I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile error Type 'UserController' already defines a member called 'AddUser' with the same parameter types API. --> how do I use fiddler to watch my request & response?

          – Doctor Ford
          Nov 22 '18 at 1:39













          once installed & running fiddler will capture any HTTP requests, when you see the request going to your address select it and on the right you will see the screen split, the top shows the request, the bottom shows the response, look for a response message like 404 not found etc

          – saj
          Nov 22 '18 at 2:07





          once installed & running fiddler will capture any HTTP requests, when you see the request going to your address select it and on the right you will see the screen split, the top shows the request, the bottom shows the response, look for a response message like 404 not found etc

          – saj
          Nov 22 '18 at 2:07













          Would I install it on the server or on the machine I'm making the request from?

          – Doctor Ford
          Nov 22 '18 at 2:09





          Would I install it on the server or on the machine I'm making the request from?

          – Doctor Ford
          Nov 22 '18 at 2:09













          the error message is simply saying a method called AddUser already exists ?

          – saj
          Nov 22 '18 at 2:09





          the error message is simply saying a method called AddUser already exists ?

          – saj
          Nov 22 '18 at 2:09













          install it on your client PC

          – saj
          Nov 22 '18 at 2:10





          install it on your client PC

          – saj
          Nov 22 '18 at 2:10




















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422572%2fpost-data-to-asp-net-api-with-xamarin%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          android studio warns about leanback feature tag usage required on manifest while using Unity exported app?

          SQL update select statement

          'app-layout' is not a known element: how to share Component with different Modules