Web Services / Web References in ASP.NET using C#
-
I have no idea what I am wrong. Needless to say it is giving me a compiler error "Error 1 'clsWebServices' does not contain a definition for 'FindAddress' and no extension method 'FindAddress' accepting a first argument of type 'clsWebServices' could be found (are you missing a using directive or an assembly reference?)" What do I need to do to make this Web Method / Web Reference work properly? This is for a local Access .mdb file that resides under app_Data. This is what I have under App_Code for clsWebSerices.cs:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.OleDb;
using System.Web.Services;/// /// Summary description for clsWebServices
///public class clsWebServices : System.Web.Services.WebService {
public clsWebServices () { //Uncomment the following line if using designed components //InitializeComponent(); } public class Service1 : System.Web.Services.WebService { \[WebMethod\] public dsAddress FindAddress(string LastName, string Path) { string connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path; string commandText = "select \* from tblAddressBook where LastName like '" + LastName + "'"; dsAddress DS = default(dsAddress); using (var connection = new OleDbConnection(connectionString)) using (var command = new OleDbCommand(commandText, connection)) { // OleDbCommand uses positional, rather than named, parameters. // The parameter name doesn't matter; only the position. DS = new dsAddress(); var adapter = new OleDbDataAdapter(command); adapter.Fill(DS); } // Add your comments here return DS; } }
}
And for clsWebServices.asmx:
<%@ WebService Language="C#" CodeBehind="~/App_Code/clsWebServices.cs" Class="clsWebServices" %>
frmAddressBook.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmAddressBook.aspx.cs" Inherits="frmAddressBook" %>
<html xmlns="http://www.w3.org/1999/xhtml"
-
I have no idea what I am wrong. Needless to say it is giving me a compiler error "Error 1 'clsWebServices' does not contain a definition for 'FindAddress' and no extension method 'FindAddress' accepting a first argument of type 'clsWebServices' could be found (are you missing a using directive or an assembly reference?)" What do I need to do to make this Web Method / Web Reference work properly? This is for a local Access .mdb file that resides under app_Data. This is what I have under App_Code for clsWebSerices.cs:
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.Data.OleDb;
using System.Web.Services;/// /// Summary description for clsWebServices
///public class clsWebServices : System.Web.Services.WebService {
public clsWebServices () { //Uncomment the following line if using designed components //InitializeComponent(); } public class Service1 : System.Web.Services.WebService { \[WebMethod\] public dsAddress FindAddress(string LastName, string Path) { string connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path; string commandText = "select \* from tblAddressBook where LastName like '" + LastName + "'"; dsAddress DS = default(dsAddress); using (var connection = new OleDbConnection(connectionString)) using (var command = new OleDbCommand(commandText, connection)) { // OleDbCommand uses positional, rather than named, parameters. // The parameter name doesn't matter; only the position. DS = new dsAddress(); var adapter = new OleDbDataAdapter(command); adapter.Fill(DS); } // Add your comments here return DS; } }
}
And for clsWebServices.asmx:
<%@ WebService Language="C#" CodeBehind="~/App_Code/clsWebServices.cs" Class="clsWebServices" %>
frmAddressBook.aspx:
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="frmAddressBook.aspx.cs" Inherits="frmAddressBook" %>
<html xmlns="http://www.w3.org/1999/xhtml"
The error message is quite clear, and also correct. The class
clsWebServices
doesn't contain a method calledFindAddress
. That method is defined on a nested class calledService1
. Either move the method to the outer class, or change the class you're creating:clsWebServices.Service1 serviceObj = new clsWebServices.Service1();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
The error message is quite clear, and also correct. The class
clsWebServices
doesn't contain a method calledFindAddress
. That method is defined on a nested class calledService1
. Either move the method to the outer class, or change the class you're creating:clsWebServices.Service1 serviceObj = new clsWebServices.Service1();
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Richard thanks for the insight. I cleaned up things a bit to this: clsWebServices.aspx.cs
using System.Data.OleDb;
using System.Data;
using System.Web.Services;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;[WebService(Namespace = "http://localhost:51557/Week3Lab/clsWebServices.asmx/FindAddress")]
public class clsWebServices : System.Web.Services.WebService {public clsWebServices () { //InitializeComponent(); } \[WebMethod(Description = "This method call will get the LastName and return the Dataset.", EnableSession = false)\] public dsAddress FindAddress(string LastName, string Path) { string connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path; string commandText = "select \* from tblAddressBook where LastName like "+LastName ; dsAddress DS = new dsAddress(); using (var connection = new OleDbConnection(connectionString)) using (var command = new OleDbCommand(commandText, connection)) { var adapter = new OleDbDataAdapter(command); adapter.Fill(DS.tblAddressBook); } // Add your comments here return DS; }
}
frmAddressBook.aspx.cs
protected void btnFindLastName_Click(object sender, EventArgs e)
{
lblMessage.Text = "";
lblMessage.Text = txtFindLastName.Text;
try
{
clsWebServices serviceObj = new clsWebServices();
dsAddress dsFindLastName = new dsAddress();
string TempPath = Server.MapPath("~/App_Data/AddressBook.mdb");dsFindLastName = serviceObj.FindAddress(txtFindLastName.Text.ToString(), TempPath); if (dsFindLastName.tblAddressBook.Rows.Count > 0) { DataRow r = dsFindLastName.tblAddressBook.Rows\[0\]; txtFirstName.Text = r\["FirstName"\].ToString(); txtLastName.Text = r\["LastName"\].ToString(); txtEmail.Text = r\["Email"\].ToString(); txtPhoneNumber.Text = r\["PhoneNumber"\].ToString(); } else lblMessage.Text = "No records found!"; } catch (Exception ex) { lblMessage.Text = lblMessage.Text + ex.Message; } }
When I subm
-
Richard thanks for the insight. I cleaned up things a bit to this: clsWebServices.aspx.cs
using System.Data.OleDb;
using System.Data;
using System.Web.Services;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;[WebService(Namespace = "http://localhost:51557/Week3Lab/clsWebServices.asmx/FindAddress")]
public class clsWebServices : System.Web.Services.WebService {public clsWebServices () { //InitializeComponent(); } \[WebMethod(Description = "This method call will get the LastName and return the Dataset.", EnableSession = false)\] public dsAddress FindAddress(string LastName, string Path) { string connectionString = "PROVIDER=Microsoft.Jet.OLEDB.4.0;Data Source=" + Path; string commandText = "select \* from tblAddressBook where LastName like "+LastName ; dsAddress DS = new dsAddress(); using (var connection = new OleDbConnection(connectionString)) using (var command = new OleDbCommand(commandText, connection)) { var adapter = new OleDbDataAdapter(command); adapter.Fill(DS.tblAddressBook); } // Add your comments here return DS; }
}
frmAddressBook.aspx.cs
protected void btnFindLastName_Click(object sender, EventArgs e)
{
lblMessage.Text = "";
lblMessage.Text = txtFindLastName.Text;
try
{
clsWebServices serviceObj = new clsWebServices();
dsAddress dsFindLastName = new dsAddress();
string TempPath = Server.MapPath("~/App_Data/AddressBook.mdb");dsFindLastName = serviceObj.FindAddress(txtFindLastName.Text.ToString(), TempPath); if (dsFindLastName.tblAddressBook.Rows.Count > 0) { DataRow r = dsFindLastName.tblAddressBook.Rows\[0\]; txtFirstName.Text = r\["FirstName"\].ToString(); txtLastName.Text = r\["LastName"\].ToString(); txtEmail.Text = r\["Email"\].ToString(); txtPhoneNumber.Text = r\["PhoneNumber"\].ToString(); } else lblMessage.Text = "No records found!"; } catch (Exception ex) { lblMessage.Text = lblMessage.Text + ex.Message; } }
When I subm
WickedFooker wrote:
When I submit the FORM it has this in the submit:
<form id="form1" runat="server" action="clsWebServices.asmx/FindAddress" method="post">
Why? If you want to call the service from the code-behind, you need to post back to the same page. Your form is trying to post directly to the service instead. I haven't tried it, but posting to the service might work if you use client-side controls so that the names match the parameter names precisely. You can't use the server controls, because ASP.NET will mangle the names to make them unique. However, even if it works, the result isn't going to be something you want to display to the user. If you want to call the service from the browser without posting back, you'll need to use javascript: http://msdn.microsoft.com/en-us/library/bb398995%28v=vs.90%29.aspx[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
-
WickedFooker wrote:
When I submit the FORM it has this in the submit:
<form id="form1" runat="server" action="clsWebServices.asmx/FindAddress" method="post">
Why? If you want to call the service from the code-behind, you need to post back to the same page. Your form is trying to post directly to the service instead. I haven't tried it, but posting to the service might work if you use client-side controls so that the names match the parameter names precisely. You can't use the server controls, because ASP.NET will mangle the names to make them unique. However, even if it works, the result isn't going to be something you want to display to the user. If you want to call the service from the browser without posting back, you'll need to use javascript: http://msdn.microsoft.com/en-us/library/bb398995%28v=vs.90%29.aspx[^]
"These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer
Okay. I got it working 100%. I was totally over-thinking things. I made some minor changes to part of the code and took out the form submit junk I had. Thanks again for your help.