' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class AutoExtender
Inherits System.Web.Services.WebService
Dim cn As New SqlClient.SqlConnection()
Dim ds As New DataSet
Dim dt As New DataTable
Dim strCn As String
<WebMethod()> _
Public Function Getname(ByVal prefixText As String, ByVal count As Integer) As String()
'ADO.Net
strCn = "Server=localhost;UID=sa;PASSWORD=admin;database=Refer;Max Pool Size=400;Connect Timeout=600;"
cn.ConnectionString = strCn
Dim cmd As New SqlClient.SqlCommand
cmd.Connection = cn
cmd.CommandType = CommandType.Text
'Compare String From Textbox(prefixText)
'AND String From Column in DataBase(CompanyName)
'If String from DataBase is equal to String from TextBox(prefixText)
'then add it to return ItemList
'-----I defined a parameter instead of passing value
'directly to prevent SQL injection--------'
cmd.CommandText = "SELECT code,codename from icd10 Where codename like @myParameter"
cmd.Parameters.AddWithValue("@myParameter", "%" + prefixText + "%")
Try
cn.Open()
cmd.ExecuteNonQuery()
Dim da As New SqlDataAdapter(cmd)
da.Fill(ds)
Catch ex As Exception
Finally
cn.Close()
End Try
dt = ds.Tables(0)
'Then return List of string(txtItems) as result
Dim txtname As New List(Of String)
Dim dbValues As String
For Each row As DataRow In dt.Rows
''String From DataBase(dbValues)
dbValues = row("codename").ToString()
dbValues = dbValues.ToLower()
txtname.Add(dbValues)
Next
Return txtname.ToArray()
End Function
End Class
ของtest webService ดึงข้อมูลมาแต่ถ้าrunในหน้าหลักไม่ดึงข้อมููลมา
รบกวนช่วยด้วยให้หน่อยนะคะ
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
/// <summary>
/// Summary description for StaffAutoComplete
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
// 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 StaffAutoComplete : System.Web.Services.WebService
{
public StaffAutoComplete ()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public string[] GetCompletionList(string prefixText, int count)
{
Intranet2012DataContext intranet2012 = new Intranet2012DataContext();
//left join
string[] staff = (from s in intranet2012.Staff
join u in intranet2012.UserTable on s.ID equals u.StaffID into JoinedStaffUser
from u in JoinedStaffUser.DefaultIfEmpty()
where (s.Name.Trim() + " " + s.LastName.Trim()).StartsWith(prefixText) && u.StaffID == null
orderby s.Name
select new
{
StaffName = string.Format("{0} {1}", s.Name.Trim(), s.LastName.Trim())
}).Select(st => st.StaffName).Take(count).ToArray();
return staff;
}
}
' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
' <System.Web.Script.Services.ScriptService()> _
<System.Web.Services.WebService(Namespace:="http://tempuri.org/")> _
<System.Web.Services.WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _
<ToolboxItem(False)> _
Public Class AutoExtender
Inherits System.Web.Services.WebService
Dim cn As New SqlClient.SqlConnection()
Dim ds As New DataSet
Dim dt As New DataTable
Dim strCn As String