尝试从javascript中调用插入值到Visual Studio中的sql server时获取内部服务器错误500

时间:2021-08-31 15:17:05

I have a code in javascript which calls a webmethod in c-sharp ,the javascript code and web method are given below

我在javascript中有一个代码,用c-sharp调用web方法,下面给出了javascript代码和web方法

javascript code:

var _data = {

            '_mStart': document.getElementById("St_Period").value,
            '_mEnd': document.getElementById("En_Period").value
        };

                  $.ajax({
                      type: "POST",
                      url: "maps.aspx/myFunc",
                      data: _data,
                      contentType: "application/json; charset=utf-8",
                      dataType: "json",
                      success: function(msg) {
                        alert("success!")
          }

        });

WEB method:

 [WebMethod]
    public static void myFunc(DateTime? _mStart, DateTime? _mEnd)
    {

        try
        {

            SqlConnection con=new SqlConnection("server=SWAPPS_LAP\\SQLEXPRESS;Initial Catalog=moogle;Integrated Security=True;MultipleActiveresultSets=true");
            SqlCommand cmd = new SqlCommand();
            con.Open();
            cmd = new SqlCommand("insert into MEDIA_BOOKING(ST_PERIOD,END_PERIOD,ENTERED_BY,ENTERED_ON) values(@st,@end,@by,@on)", con);
            cmd.Parameters.AddWithValue("@st", _mStart);
            cmd.Parameters.AddWithValue("@end", _mEnd);

            cmd.Parameters.AddWithValue("@on", DateTime.Now);
            cmd.ExecuteNonQuery();
            con.Close();
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

but when I run the code I get an error as

但是当我运行代码时,我得到一个错误

POST http://localhost:50060/moogle/maps.aspx/myFunc 500 (Internal Server Error) jquery.js:8102
jQuery.ajaxTransport.send jquery.js:8102
jQuery.extend.ajax jquery.js:7580
savebook maps.aspx:398
onclick

I have a hunch maybe there is something wrong with the paramenters ,maybe they are not in the proper format?

我有预感可能参数有问题,也许它们的格式不正确?

1 个解决方案

#1


0  

You are not providing a value for the @by parameter. Try adding a breakpoint in the catch statement to see what the error is. If you don't get to that stage the try posting the request body so that people can see what the request is and answer with more idea of the details.

您没有为@by参数提供值。尝试在catch语句中添加断点以查看错误。如果您没有进入该阶段,请尝试发布请求正文,以便人们可以查看请求的内容并回答更多详细信息。

#1


0  

You are not providing a value for the @by parameter. Try adding a breakpoint in the catch statement to see what the error is. If you don't get to that stage the try posting the request body so that people can see what the request is and answer with more idea of the details.

您没有为@by参数提供值。尝试在catch语句中添加断点以查看错误。如果您没有进入该阶段,请尝试发布请求正文,以便人们可以查看请求的内容并回答更多详细信息。