显示来自C#代码的警报

时间:2018-07-05 12:31:57

标签: c# ajax webforms

这是一个WebMethod,它从lvl字符串的前端获取值。 稍后,如果数据库中已经存在该值,则通过getDuplicate过程检查该字符串。如果该值存在,则不会激活插入过程InsertObject,并且如果数据库中没有这样的值,则第一个过程将返回null,并且插入过程将起作用。

如果插入成功或失败,我需要的所有代码中的所有功能都是从C#部分发出的某种警报消息。 我尝试了很多示例,但找不到任何解决方案:/ 有人可以帮忙吗?

[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;

    {
}

更新: 将值发送到方法的Ajax:

$(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 个答案:

答案 0 :(得分:1)

更改web方法返回类型

[WebMethod(EnableSession = true)]
public static int GetCollection(string lvl)
{
int success=0;
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();
            success= 1;
        }
        else
        {

        }
    }


    catch 
    {


    }
    finally
    {
        connection.Close();

    }

return success;

}

}

在Jquery代码中做一些小的更改

       $(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) {
                     if(parseInt(response.d)>0)
                        alert("Saved successfully.");
                     else
                        alert("Not Saved!");
                     console.log(response);
                     location.reload(true);

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

             });

         });

     });