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?

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

8 Comments

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?
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
Would I install it on the server or on the machine I'm making the request from?
the error message is simply saying a method called AddUser already exists ?
install it on your client PC
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.