Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8452929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:37:56+00:00 2026-06-10T11:37:56+00:00

I need to send an error message from controller, in case that,an error happens

  • 0

I need to send an error message from controller, in case that,an error happens in ajax function.
Here is my Controller code:

//check if the day is already approved
string check1 = "SELECT approve_status_id FROM table_approvals WHERE approve_date=" + date + " AND user_id=" + Session["userID"];
MySqlCommand status_cmd = new MySqlCommand(check1, con);
MySqlDataReader rdrStatus = status_cmd.ExecuteReader();
while (rdrStatus.Read())
{
    if (rdrStatus.GetString(0) == "5")
    {
        valid = false;
        error = "You cannot edit already approved dates!";
        response.Message = error;
     return View(Json(new { success = false,error},JsonRequestBehavior.AllowGet));
    }
}

my ajax function:

$.ajax({
    url: '@Url.Action("SaveLine", "AddLine")',
    type: 'post',
    data: { ids: IDs },
    dataType: 'json',
    traditional: true,
    success: function () {
        window.location.href = '@Url.Action("Index", "AddLine")';
    },
    error: function () {
        $.getJSON('/AddLine/SaveLine', function (json) {
            alert('error');
            $("#ajaxPostMessage").html(json.Message);
        });
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T11:37:58+00:00Added an answer on June 10, 2026 at 11:37 am

    Wrap everything in try catch. You can simple call the JSON method which will create a JSON response and return that to the client code. Also , you do not need JsonRequestBehavior.AllowGet to return JSON when it is a HttpPost Action method. You need that for GET only.($.getJSON, $.get ..)

    try
    {
       //code for getting data to reader    
         while (rdrStatus.Read())
         {
            if (rdrStatus.GetString(0) == "5")
            {
              valid = false;
              var errorMsg = "You cannot edit already approved dates!";             
              return Json( new { success = false,Error=errorMsg });           
            }
         }
     }
     catch(Exception ex)
     {
       //log Error
         return Json(new { success = false,Error="Some Error!"});
     }
    

    This will return a JSON like this to the client

    {  "success ": "True",    "Error": "SomeError"  }
    

    Now in your client code, you can check the JSON And do whatever you want.

    success: function (data) {
        if(data.success=="True")
        {
           window.location.href = '@Url.Action("Index", "AddLine")';
        }
        else
        {
          alert(data.Error);
        }
    },
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to pass an error message from a grails controller to a grails
We need send h264 from flash to Wowza and after to vlc by RTSP
I need send certain attributes(say, human readable user name) from server to client after
I've a custom form (created with form API) that need send an uploaded file
I need to send an HTML report with images from an application. The report
I need to send some information on a VxWorks message queue. The information to
I need to send some arguments from the iPhone to a php in the
I need to send emails from my web application (on account creation, password reset,
I want to be able to send a friendly error message to my user
i'm trying to send some radio buttons from a form with ajax, the problem

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.