weixin_33694172 2018-07-06 06:35 采纳率: 0%
浏览 25

来自后端的警报

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);
             }
         });

     });

 });
  • 写回答

1条回答 默认 最新

  • weixin_33670713 2018-07-06 06:53
    关注

    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);
             }
    
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!