none
Webservice that retruns datatable RRS feed

Answers

  • How would you write a web service that returns an integer? You would calculate the integer and then return it.

    Do the same thing for the DataTable. "Calculate" it, then return it.

    Note that returning a DataTable or any other type which is specific to .NET may make your service less portable than it might be. For instance, Java or PHP might not be able to understand the DataTable.


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    • Marked as answer by TarekF Tuesday, December 18, 2012 5:39 AM
    Tuesday, December 18, 2012 2:08 AM
    Moderator

All replies

  • Hello,

    I have created a .net webservice connected to MS SQL  to retrieve all relevant data for a specific ID , the problem is that I am getting only the first record and not all records available in the table.
    How to modify the code in such a way I'll get all records, below is my code:

                

    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml.Linq;

    namespace timesheet_webservice
    {
        /// <summary>
        /// Summary description for Service1
        /// </summary>
        [WebService(Namespace = "http://tempuri.org/")]
        [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
        [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
        {

            //create new web method to get data

             [WebMethod]
            public string DBconnect(string supcode)
            {

                return connection.TSdata(supcode) ;
            }

        }

    }


    using System;
    using System.Data;
    using System.Configuration;
    using System.Linq;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Xml.Linq;
    using System.Data.SqlClient;

    namespace timesheet_webservice
    {
        public class connection
        {
                 
            public  static string TSdata(string supcode){
                string dbname = "db";
                string badgeno = "";
                 
                 SqlConnection con = new SqlConnection(@"Data Source = xxx; user id=xx; password=xx; Initial Catalog = " + dbname + ";");
                 SqlCommand cmd = new SqlCommand("select badgeno from tbl where code='" + supcode + "'", con);

                 con.Open();

                    // read data
                    SqlDataReader dr = cmd.ExecuteReader();

                    while (dr.HasRows)
                    {

                        while (dr.Read())
                        {
                            badgeno = dr["badgeno"].ToString();
                        }

                        dr.NextResult();
                    }
                   dr.Close();       
                  con.Close();
                  return badgeno;

            }
        }
    }

    Sunday, December 16, 2012 8:15 AM
  • How would you write a web service that returns an integer? You would calculate the integer and then return it.

    Do the same thing for the DataTable. "Calculate" it, then return it.

    Note that returning a DataTable or any other type which is specific to .NET may make your service less portable than it might be. For instance, Java or PHP might not be able to understand the DataTable.


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects

    • Marked as answer by TarekF Tuesday, December 18, 2012 5:39 AM
    Tuesday, December 18, 2012 2:08 AM
    Moderator