Wrox Programmer Forums
|
ASP.NET 3.5 Basics If you are new to ASP or ASP.NET programming with version 3.5, this is the forum to begin asking questions. Please also see the Visual Web Developer 2008 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 3.5 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 27th, 2008, 11:33 AM
Friend of Wrox
 
Join Date: Sep 2007
Posts: 169
Thanks: 7
Thanked 2 Times in 2 Posts
Default Question about webservices

Hi

I got a couple question about web services

I know in web services you can't return stuff like dataTableRows and dataTableColumns and that you can only return dataSets back and even that is not recommended.

I am wondering however can you still use them in as long as you don't return those thing in the end?

Like I tried to make a dataset file(dataset.xsd) and I then made a class to hold my logic. I just did one simple sql statement to just count the number of rows.

*note the variable names are not relevant. I just basically copied old code and change it around a bit.

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using WebService2.DataSet1TableAdapters;


namespace WebService2
{
    public class Class1
    {

        private DataTable1TableAdapter chartsAdapter = null;
        protected DataTable1TableAdapter ChartsAdapter
        {
            get
            {
                if (chartsAdapter == null)
                {
                    chartsAdapter = new DataTable1TableAdapter();
                }

                return chartsAdapter;

            }
        }

        public int grabCount()
        {
            // this is what my DataSet.xsd file holds for the sql
            // SELECT  COUNT(StudentID) AS Expr1
            // FROM  Students
            return Convert.ToInt32(ChartsAdapter.GetDataCount());
        }
    }
}
Then in my Web Service file(Service.asmx.cs)

Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebService2
{
    /// <summary>
    /// Summary description for Service1
    /// </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 Service1 : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }

        [WebMethod]
        public int test()
        {
            Class1 tes = new Class1();
            return tes.grabCount();
        }
    }
}
So as you can see I am just making a new object of Class and then trying to get the count back that returns a int.

However when I try to test my web service I get this error.

System.InvalidCastException: Unable to cast object of type 'DataTable1DataTable' to type 'System.IConvertible'.
   at System.Convert.ToInt32(Object value)
   at WebService2.Class1.grabCount() in I:\WebService2\Class1.cs:line 30
   at WebService2.Service1.test() in I:\WebService2\Service1.asmx.cs:line 30

So I am not sure what is going on.

So I am using an access database, Visual Studios 2008, Web Service Application.




 
Old November 28th, 2008, 12:58 AM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

That error says to me that your method
        ChartsAdapter.GetDataCount()
which is of course actually the method
        DataTable1TableAdapter.GetDataCount()
is returning a DataTable (more specifically, a DataTable1DataTable).

And of course you can't convert an entire table to an integer.

So I think you need to go look at that DataTable1TableAdapter class more carefully.





Similar Threads
Thread Thread Starter Forum Replies Last Post
Struts+webservices nishi_gupta Struts 1 October 26th, 2007 12:13 AM
checking webservices raju@wrocks C# 0 December 13th, 2006 03:13 AM
Tools for WebServices JosephLouis .NET Web Services 2 June 1st, 2006 02:23 AM
webservices dkautilya ASP.NET 1.0 and 1.1 Professional 0 November 18th, 2005 08:57 AM
java webservices rsgollakota Servlets 0 January 20th, 2004 06:55 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.