Web Service
- namespace WebApp1.Services
- {
- ///
- /// Summary description for WebAppService
- ///
- [WebService(Namespace = "http://localhost/")]
- [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.
- [ScriptService]
- public class WebAppService : WebService
- {
- [WebMethod]
- [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
- public static List<string> GetBankNames(string pre)
- {
- List<string> listBanks = new List<string>();
- DataTable dtData = new DataTable();
- string conString = ConfigurationManager.ConnectionStrings["dbConnection"].ConnectionString;
- SqlConnection sqlCon = new SqlConnection(conString);
- sqlCon.Open();
- SqlCommand sqlCmd = new SqlCommand("Select BankId, BankName From BankMaster Where BankName Like '%' + @SearchText + '%'", sqlCon);
- sqlCmd.Parameters.AddWithValue("@SearchText", pre);
- SqlDataAdapter sqlDa = new SqlDataAdapter(sqlCmd);
- sqlDa.Fill(dtData);
- sqlCon.Close();
- if (dtData.Rows.Count > 0)
- {
- for (int i = 0; i < dtData.Rows.Count; i++)
- {
- listBanks.Add(string.Format("{0}-{1}", dtData.Rows[i]["BankName"], dtData.Rows[i]["BankId"]));
- }
- }
- return listBanks;
- }
- }
- }
Script
When ever i call the web service always get error as "Unknown web method GetBankNames".
Shweta LodhaPosted Dec 20, 2019, 5:13 PM
Amit MohantyPosted Dec 22, 2019, 10:20 PM
If you have a web method as part of your Page class, it must be static.
If you've put a web method in an .asmx file to use across your application, it must not be static.