2016-11-24 69 views
1

这种方法有什么不对吗?我试图添加此方法在Web服务(.asmx)文件中。但它显示我错误。添加完这个之后,即使在.asmx文件中显示相同错误的其他方法也是如此。C# - 添加方法后显示错误

[WebMethod] 
    public string UpdateEvent(EventsClass es) 
    { 
     string cs = ConfigurationManager.ConnectionStrings["DBCS"].ToString(); 
     using (SqlConnection con = new SqlConnection(cs)) 
     { 
      string qr = "UPDATE Events SET Title = '" + es.Title + 
       "', DateFrom = '" + es.DateFrom + 
       "', DateTo = '" + es.DateTo + 
       "', Location = '" + es.Location + 
       "', DateTo = '" + es.DateTo + 
       "', Status = '" + es.Status + 
       "', Description = '" + es.Description + 
       "', UpdateDate = '" + es.UpdateDate + 
       "' WHERE Id = '" + es.Id + "'"; 

      SqlCommand cmd = new SqlCommand(qr, con); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 
     return "1"; 
    } 

错误消息是这样的:在AJAX POST方法

System.IndexOutOfRangeException: Index was outside the bounds of the array. 
    at System.Web.Services.Protocols.HttpServerType..ctor(Type type) 
    at System.Web.Services.Protocols.HttpServerProtocol.Initialize() 
    at System.Web.Services.Protocols.ServerProtocolFactory.Create(Type type, HttpContext context, HttpRequest request, HttpResponse response, Boolean& abortProcessing) 

错误消息返回:

Failed to load resource: the server responded with a status of 500 (Internal Server Error) 
function(){return f&&(c&&!b&&(h=f.length-1,g.push(c)),function d(b){n.each(b,function(b,c){n.isFunction(c)?a.unique&&j.has(c)||f.push(c):c&&c.length&&"string"!==n.type(c)&&d(c)})}(arguments),c&&!b&&i… 
+0

一个建议:使用一个StringBuilder,在你的查询命令中使用AddFormat,它更容易构建代码并干净地读取......;) –

回答

3

那是因为你没有DBCS配置键Web.config,你会得到例外:

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ToString(); 

从不以这种方式生成SQL查询。改为始终使用SqlParameter。 使用参数有助于防止SQL注入攻击

+0

其他方法也有相同的连接字符串。并且Web.config具有DBCS配置密钥。 – Jaber

+0

@Jaber你跟踪过你的代码吗?在哪一行你有'IndexOutOfRangeException'异常? – esiprogrammer