POST Data To ASP.NET API With Xamarin
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
add a comment |
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
add a comment |
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
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
c# asp.net xamarin xamarin.forms
asked Nov 22 '18 at 1:07
Doctor FordDoctor Ford
638
638
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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.
I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile errorType '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
|
show 3 more comments
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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.
I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile errorType '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
|
show 3 more comments
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.
I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile errorType '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
|
show 3 more comments
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.
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.
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 errorType '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
|
show 3 more comments
I made the first update you suggested and ran the code still nothing inserted into database. The second one I get this compile errorType '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
|
show 3 more comments
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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