简体   繁体   中英

Alert from back-end

This is a WebMethod that takes value from front-end in the lvl string. Later that string is checked through getDuplicate procedure if there is already that value in the database. If the value exists then the insert procedure InsertObject is not activated and if there is no such value in the database first procedure returns null and the insert procedure will work.

Everything work well in the code all I need is some kind of an alert message from the C# part of the code if the insert is a success, and if it fails. I tried so many examples and I can't find any solution :/ Can someone please help ?

[WebMethod(EnableSession = true)]
public static void GetCollection(string lvl)
{

    string conn = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
    using (SqlConnection connection = new SqlConnection(conn))

    try
    {
        connection.Open();
        SqlCommand cmdCount = new SqlCommand("getDuplicate", connection);
        cmdCount.CommandType = CommandType.StoredProcedure;
        cmdCount.Parameters.AddWithValue("@ObjekatName", lvl);
        var count = (string)cmdCount.ExecuteScalar();

        if (count == null)
        {
            SqlCommand cmdProc = new SqlCommand("InsertObjekat", connection);
            cmdProc.CommandType = CommandType.StoredProcedure;
            cmdProc.Parameters.AddWithValue("@ObjekatName", lvl);                   
            cmdProc.ExecuteNonQuery();
            //successful alert
        }
        else
        {
          //fail alert
        }
    }
    catch 
    {

    }
    finally
    {
        connection.Close();
    }
    return;
}

Update: Ajax that sends values to the method:

$(function () {

     $('#myButton').on('click', function () {

         var lvl = $('#MainContent_txtProductConstruction').val()

         $.ajax({
             type: "POST",
             url: "NewProductConstruction.aspx/GetCollection",

             data: JSON.stringify({'lvl': lvl }),

             contentType: "application/json; charset=utf-8",
             dataType: "json",

             success: function (response) {
                 alert("Saved successfully.");
                 console.log(response);
                 location.reload(true);

             },
             error: function (response) {
                 alert("Not Saved!");
                 console.log(response);
                 location.reload(true);
             }
         });

     });

 });

You can return a json object. Change the return type of method as string.Include using Newtonsoft.Json; and then when you want to return, create an object like this:

[WebMethod]
public static string GetCollection(string lvl)
{
   bool isInserted = false;
   // set the value of isInserted
   // you can send code a numeric value or bool value according to your need
   var result = new {code = isInserted, message = isInserted ? "Succesfully inserted" : "Already Exists"};
   return JsonConvert.SerializeObject(result);
}

At the client side, check the response

success: function (response) {
          console.log(response);
          if(response != null){
            var data = $.parseJSON(response)
            alert(data.message)
          }
          location.reload(true);
         }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM