Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Web Development
  3. ASP.NET
  4. Web Services / Web References in ASP.NET using C#

Web Services / Web References in ASP.NET using C#

Scheduled Pinned Locked Moved ASP.NET
csharphtmlasp-netwcfhelp
5 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • W Offline
    W Offline
    WickedFooker
    wrote on last edited by
    #1

    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"

    Richard DeemingR 1 Reply Last reply
    0
    • W WickedFooker

      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"

      Richard DeemingR Offline
      Richard DeemingR Offline
      Richard Deeming
      wrote on last edited by
      #2

      The error message is quite clear, and also correct. The class clsWebServices doesn't contain a method called FindAddress. That method is defined on a nested class called Service1. 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

      "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

      W 1 Reply Last reply
      0
      • Richard DeemingR Richard Deeming

        The error message is quite clear, and also correct. The class clsWebServices doesn't contain a method called FindAddress. That method is defined on a nested class called Service1. 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

        W Offline
        W Offline
        WickedFooker
        wrote on last edited by
        #3

        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 DeemingR 1 Reply Last reply
        0
        • W WickedFooker

          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 DeemingR Offline
          Richard DeemingR Offline
          Richard Deeming
          wrote on last edited by
          #4

          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

          "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

          W 1 Reply Last reply
          0
          • Richard DeemingR Richard Deeming

            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

            W Offline
            W Offline
            WickedFooker
            wrote on last edited by
            #5

            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.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups