2013-03-12 2 views
0

messageID를 반환하고 싶지만 올바르게 작동하지 않습니다. 단일 문자열 값을 반환하려면 어떻게해야합니까?C#의 WebMethod에서 단일 문자열 값 반환

[WebMethod] 
    public string messageComeGetID(string from, string to) 
    { 
     try 
     { 
      sqlConn.Open(); 
     } 
     catch (Exception e) 
     { 
      return null; 
     } 
     // select messageID from tblMessage where [from] = '2' AND [to] = '4' gibi. 
     SqlCommand cmd3 = new SqlCommand("select messageID from tblMessage where [from][email protected] AND [to][email protected]", sqlConn); 

     cmd3.Parameters.AddWithValue("@from", from); 
     cmd3.Parameters.AddWithValue("@to", to); 

     cmd3.Connection = sqlConn; 
     object value = cmd3.ExecuteScalar(); 
     string messageID = Convert.ToString(value); 
     cmd3.ExecuteNonQuery(); 
     cmd3.Dispose(); 
     sqlConn.Close(); 

     return messageID; 
    } 
+2

입니다. 왜 그것이 효과가 없다고 생각합니까? – Sjoerd

+0

ı 같은 SQL에서와 열이 내 SQL 테이블에 있지만 다른 ID가 있습니다. 나는 질의의 마지막 ID를 얻으려고하지만 위의 코드는 항상 첫 번째 ID를 제공한다. –

+0

ı LAST 및 TOP 명령으로 처리하십시오. 감사. –

답변

0

나는이 WebService1.asmx

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Script.Serialization; 
using System.Web.Script.Services; 
using System.Web.Services; 

namespace StackOverflow_Solve.Services 
{ 
    /// <summary> 
    /// Summary description for WebService1 
    /// </summary> 
    [WebService(Namespace = "http://tempuri.org/")] 
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] 
    [System.ComponentModel.ToolboxItem(false)] 
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService] 
    public class WebService1 : System.Web.Services.WebService 
    { 

     [WebMethod] 
     public string HelloWorld() 
     { 
      return "Hello World"; 
     } 
     [WebMethod] 
     [ScriptMethod(ResponseFormat = ResponseFormat.Json)] 
     public string GetMessage() // Home.CS 
     { 
      //return "GOGO"; 
      Context.Response.Output.Write("Hello World"); 
      Context.Response.End(); 
      return string.Empty; 
     } 
    } 
} 

완전한 이행과 같이 구현 될 수 이것은 당신이 하나의 문자열 값을 반환하는 방법을 정확히

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml"> 
<head> 
    <title></title> 
    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> 
</head> 
<body> 
<head> //HTML and Ajax 
<title></title> 

<script> 
    function GetMessage() { 
     //Load jQuery($) to Use 
     $(function() { 
      $.get("WebService1.asmx/GetMessage", function (data) { 
       console.log(data); 
       $("p").html(data); 
      }); 
     }); 

    } 
</script> 
</head> 
<body> 
    <input type="button" onclick="GetMessage()" value="Get Message" /> 
    <p></p> 
</body> 
</body> 
</html>