The form is sending but the problem is the Success message not showing even though the ex.Message Value is Null it should be printing the "Form submitted successfully." but it did not.
And when the ex.Message is not Null .. the error message is working it print "Error submiting message."
need help, Thanks in advance!
Controller.cs
if (!ModelState.IsValid)
return CurrentUmbracoPage();
MailMessage message = new MailMessage();
message.To.Add("test#gmail.com");
message.Subject = "New Contact request";
message.From = new System.Net.Mail.MailAddress(model.Email, model.Name);
message.Body = model.Message;
SmtpClient smtp = new SmtpClient("mail.domain.com", 26);
try
{
smtp.Send(message);
}
catch (Exception ex)
{
if (ex.Message == null)
{
ViewBag.Success = "Form submitted successfully.";
}
else {
ViewBag.ErrorMessage = "Error submiting message.";
}
return CurrentUmbracoPage();
}
If the email is sent successfully there is no exception to catch, which is probably why your message does not show up.
Instead, move that message to the try block after .Send, that should do it.
EDIT: Also, I believe that your return statement should be moved outside the try-catch block.
Related
guard let uid = FirebaseManager.shared.auth.currentUser?.uid else {return}
FirebaseManager.shared.firestore.collection("User").document(uid).getDocument {
snapshot, error in
if let error = error{
print("Failed to connection user:", error)return}
guard let data = snapshot?.data() else {
self.errMessager = "no data found"
return
}
self.errMessager = ("data: \(data.description)")
}
}
i have run the emulator but still not working.
How can I fix it? I tried to connect to firestore but still not working
I keep getting the HTTP 500 Internal Server Error in my android studio trying to call a loginQuery with apollo client for android, following this tutorial : link to apollo docs
Yet when I run the same query in graphiQL in my browser, it succeeds.
here's a picture of the error in studio :
here's my code:
String userNumber = numberInput.getText().toString();
String password = passwordInput.getText().toString();
Intent intent;
LogInQuery logInQuery = LogInQuery.builder().nationalID(userNumber).password(password).build();
apolloClient.query(logInQuery).enqueue(new ApolloCall.Callback<LogInQuery.Data>() {
#Override
public void onResponse(#NotNull Response<LogInQuery.Data> response) {
LogInQuery.Data data = response.getData();
List<Error> error = response.getErrors();
assert error != null;
String errorMessage = error.get(0).getMessage();
assert data != null;
Log.d(TAG, "onResponse: " + data.login() + "-" + data.login());
if(data.login() == null){
Log.e("Apollo", "an Error occurred : " + errorMessage);
runOnUiThread(() -> {
// Stuff that updates the UI
loading.setVisibility(View.GONE);
Toast.makeText(LogInActivity.this,
errorMessage, Toast.LENGTH_LONG).show();
});
} else {
runOnUiThread(() -> {
// Stuff that updates the UI
loading.setVisibility(View.GONE);
Toast.makeText(LogInActivity.this,
"New User Created!", Toast.LENGTH_LONG).show();
//Intent intent = new Intent(LogInActivity.this, LogInActivity.class);
//startActivity(intent);
});
}
}
#Override
public void onFailure(#NotNull ApolloException e) {
runOnUiThread(() -> {
Log.e("Apollo", "Error", e);
Toast.makeText(LogInActivity.this,
"An error occurred : " + e.getMessage(), Toast.LENGTH_LONG).show();
loading.setVisibility(View.GONE);
});
}
});
}
I can't find much on the internet to help me solve it. If it's duplicate, direct me to the correct answer.
Check your API in Postman. It's working fine or not! Please share API if possible.
Currently on my application when i run it the code is working correctly, except when i add a 'symptom' that already belongs in the 'symptom history' , currently when i add a symptom which is already added in the symptom history the app gives out the message "Duplicate Symptom", "You already have recorded this symptom" and "Historical Symptom", "This symptom is in your history - Please restore from here". How can i get to it to only display "Historical Symptom", "This symptom is in your history - Please restore from here" message.
Currently if the symptom isnt deleted and moved to the symptom history the app will only output the "Duplicate Symptom", "You already have recorded this symptom" message corrrectly.
The functionality is working correctly just need to only display that one message on the function instead of both of them.
This is the current code on my button:
async void btnAdd_Clicked(object sender, EventArgs e)
{
CheckSymptomInHistory(AutoCompleteSymptomToAdd.Id);
//If the autocomplete is not empty - add that symptom to the user symptom table
if (AutoCompleteSymptomToAdd != null)
{
//If the user already has symptoms, loop through them to make sure that they are not adding a duplicate
if (UserSymptoms.Count > 0)
{
foreach (usersymptom item in UserSymptoms)
{
if (item.Symptomid == AutoCompleteSymptomToAdd.Id)
{
await DisplayAlert("Duplicate Symptom", "You already have recorded this symptom", "OK");
return;
}
//Check if it is not active (i.e in SYmptom History)
else
{
UserSymptomToAdd.Symptomid = AutoCompleteSymptomToAdd.Id;
UserSymptomToAdd.UserID = Helpers.Settings.UserKey;
UserSymptomToAdd.Datetimeadded = DateTime.Now.ToString();
UserSymptomToAdd.IsActive = true;
try
{
await usersymptommanager.AddUserSymptom(UserSymptomToAdd);
await AddInitialFeedback(UserSymptomToAdd.Id);
//await DisplayAlert("Symptom Added", "Your Symptom has been added", "OK");
}
catch (Exception ex)
{
Analytics.TrackEvent("App Screen: " + Title + ": " + ex);
//await DisplayAlert("Error", ex.ToString(), "OK");
}
}
}
}
The code for my history message:
async void CheckSymptomInHistory(string id)
{
foreach (string item in SymptomHistoryIDs)
{
if (id == item)
{
await DisplayAlert("Historical Symptom", "This symptom is in your history - Please restore from here", "OK");
}
}
}
So the first thing I see is that you're doing a null-check on AutoCompleteSymptomToAdd after you used it for CheckSymptomInHistory. I would put the null-check before CheckSymptomInHistory, otherwise you may be subjected to a NullReferenceException:
if (AutoCompleteSymptomToAdd == null)
{
// Display an error message?
return;
}
// AutoCompleteSymptomToAdd is not null, proceed to use it
CheckSymptomInHistory(AutoCompleteSymptomToAdd.Id);
I would then change the return type of CheckSymptomInHistory to return bool like so:
bool CheckSymptomInHistory(string id)
{
foreach (string item in SymptomHistoryIDs)
{
if (id == item)
{
return true;
}
}
return false;
}
Then in your handler, check the return type of the CheckSymptomInHistory method like so:
bool isSymptomInHistory = CheckSymptomInHistory(AutoCompleteSymptomToAdd.Id);
if (isSymptomInHistory)
{
await DisplayAlert("Historical Symptom", "This symptom is in your history - Please restore from here", "OK");
return;
}
// Symptom is not in history, carry on as normal
Do the following to achieve what you are trying;
Comment the First line of code in your method i.e. CheckSymptomInHistory(AutoCompleteSymptomToAdd.Id);
In the ForEach Loop:
foreach (usersymptom item in UserSymptoms)
{
if (item.Symptomid == AutoCompleteSymptomToAdd.Id)
{
await DisplayAlert("Duplicate Symptom", "You already have recorded this symptom", "OK");
return;
}
else if(CheckSymptomInHistory(AutoCompleteSymptomToAdd.Id);)
{
await DisplayAlert("Historical Symptom", "This symptom is in your history - Please restore from here", "OK");
}
Where CheckSystemInHistory is as below:
private bool CheckSymptomInHistory(string id)
{
foreach (string item in SymptomHistoryIDs)
{
if (id == item)
{
return true;
}
}
return false;
}
i have a GET method to connect to a webservices in xamarin. The method works fine, but when my phone is not connected to internet, the application crash, i would like to know how to avoid this ? Thanks for your answers:
static public string GET(string url)
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
try
{
WebResponse response = request.GetResponse();
using (Stream responseStream = response.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.UTF8);
return reader.ReadToEnd();
}
}
catch (WebException ex)
{
WebResponse errorResponse = ex.Response;
using (Stream responseStream = errorResponse.GetResponseStream())
{
StreamReader reader = new StreamReader(responseStream, System.Text.Encoding.GetEncoding("utf-8"));
String errorText = reader.ReadToEnd();
// log errorText
}
throw;
}
}
HttpWebRequest throws an Exception if there is no internet connection.
You have handled the Exception using the catch block but there, you have also written throw which throws the exception again and if you haven't handled it into the calling method, your app will crash.
Either try removing throw from your catch block, or handle Exception again into a calling method.
Like
try
{
var result = Get("myUrl");
}
Catch(Exception ex)
{
//Handle it here too
}
I have a model property like below,
[Index("CourseCodeIndex", IsUnique = true)]
[MaxLength(15)]
public string Name { get; set; }
and if I use invalid data it works well but returns no error message. Is there any way to show a message on (view, like other required like messages)
#Html.ValidationMessageFor(model => model.Name)
if you want to show the error message, you need to declare it like:
[Required(ErrorMessage = "Compiletime error required")]
Also, try this.
[Unique(ErrorMessage = "This already exist !!")]
Make an instance of your context file
private datbaseContext db = new databaseContext();
add the code below to your controller action method
db.table.Add(model);
var user = db.table.Where(u => u.Name == model.Name).FirstOrDefault();
if (user != null)
{
ModelState.AddModelError("", model.Name + " Already Exists");
}
else
{
db.SaveChanges();
return RedirectToAction("Index", "model");
}
And the #Html.ValidationSummary(true) from your view will attach the error message