Web Service Help for a Noob : Returning an XML String

Discussion in 'ASP .Net Web Services' started by Nastero, Mar 29, 2005.

  1. Nastero

    Nastero Guest

    Hi All,

    I'm trying to learn how to access data in a database via a webservice
    and I'm experiencing a little bit of trouble at the moment.

    Being a relative noobie to the entire .NET environment and languages,
    I'm having difficulty pinpointing what the issue is and how I might go
    about fixing it.

    Also, when I actually did get it to return the XML String, this was the
    return value :

    <?xml version="1.0" encoding="utf-8" ?>
    <string xmlns="http://localhost/CatfishService/"><dsPlayerList
    xmlns="http://www.tempuri.org/dsPlayerList.xsd" /></string>

    How do I change the xmlns from "http://www.tempuri.org" to localhost
    (or whatever URL is applicable at the time)?

    I'll include the code at the end of the message for your review. I
    only ask that, if you see the problem and know the answer, you relay
    your advice in small words :).

    ====================== My Service Code ======================
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Data.SqlClient;
    using System.Diagnostics;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Xml.Serialization;

    namespace CatfishService
    {
    /// <summary>
    /// Summary description for Service1.
    /// </summary>
    ///
    [WebService(Description="This is a Service Designed to Manage the
    Catfish DB",Namespace="http://localhost/CatfishService/")]

    public class Service1 : System.Web.Services.WebService
    {
    public Service1()
    {
    //CODEGEN: This call is required by the ASP.NET Web Services
    Designer
    InitializeComponent();
    }

    private SqlConnection SQLConnCatfishDB;
    private SqlDataAdapter sdaManageRoster;
    private SqlCommand sicAddToRoster;
    private SqlCommand scGetPlayerList;
    private CatfishService.dsPlayerList dsPlayerList;

    [WebMethod()]
    [return: XmlElement Namespace="http://localhost/CatfishService/",
    ElementName="PlayerList")]

    public string GetPlayerList(int player_id, int is_active)
    {
    //SQLConnCatfishDB.Open(); (I commented this out because it
    would not process anything after it, it would just open up a
    webpage to nowhere).

    sdaManageRoster.SelectCommand.Prepare();
    sdaManageRoster.SelectCommand.Parameters["@player_id"].Value =
    player_id;
    sdaManageRoster.SelectCommand.Parameters["@is_active"].Value =
    is_active;

    //sdaManageRoster.Fill(dsPlayerList); (I commented this out
    for the same reason above, not really certain why it does this).
    return dsPlayerList.GetXml();
    }

    Thank in advance.

    Cheers,

    Daniel Hobert
    Nastero, Mar 29, 2005
    #1
    1. Advertising

  2. Nastero

    jerome Guest

    I'm sure there are more than one way to address this issue. One
    possibility would be to store your DB passwords and/or full connection
    strings in a separate file that you can read at runtime. The file
    should be adequately protected so nobody can hack into to retrieve your
    sensitive DB credentials.

    Daniel Hobert wrote:
    > Hi again,
    >
    > I was browsing through microsoft.public.dotnet.framework.webservices

    hoping
    > for an answer when I noticed a snippet of code in a semi-related

    thread that
    > I thought might help shed some light onto why I was unable to

    retrieve the
    > information necessary and why the page was incapable of being

    displayed.
    >
    > I added the code below to my webMethod that is returning a string of

    XML
    > data and suddenly I was able to get to the heart of my issue.
    >
    > So here goes.
    >
    > string xmlPlayerList;
    > try
    > {
    > sdaManageRoster.SelectCommand.Connection.Open();
    > sdaManageRoster.SelectCommand.Prepare();
    > sdaManageRoster.SelectCommand.Parameters["@player_id"].Value =
    > player_id;
    > sdaManageRoster.SelectCommand.Parameters["@is_active"].Value =
    > is_active;
    >
    > sdaManageRoster.Fill(dsPlayerList);
    > xmlPlayerList = dsPlayerList.GetXml();
    > }
    > catch(Exception ex)
    > {
    > xmlPlayerList = ex.ToString();
    > }
    >
    > sdaManageRoster.SelectCommand.Connection.Close();
    >
    > return xmlPlayerList;
    >
    > What this did for me was return the exact text of the exception I

    was
    > experiencing (but not seeing). The gist of it is that the default

    user was
    > not able to execute a stored procedure in the master database and so

    I was
    > unable to open the Connection in the first place. Once I put in the
    > necessary login info and made sure it was saved, my list of players

    was
    > successfully returned and displayed on the page.
    >
    > This brings me to a new issue though, apparently by saving the login

    and
    > password I am exposing it in the code of the page and thus am leaving

    the
    > database open to be logged into by anyone, I suppose. Does anyone

    know of a
    > better way to handle the opening of a connection where I won't be

    left
    > exposing the DB Login Information in such a way?
    >
    > Thanks again in advance.
    >
    > Cheers,
    >
    > Daniel Hobert
    >
    >
    > "Nastero" <> wrote in message
    > news:...
    > > Hi All,
    > >
    > > I'm trying to learn how to access data in a database via a

    webservice
    > > and I'm experiencing a little bit of trouble at the moment.
    > >
    > > Being a relative noobie to the entire .NET environment and

    languages,
    > > I'm having difficulty pinpointing what the issue is and how I might

    go
    > > about fixing it.
    > >
    > > Also, when I actually did get it to return the XML String, this was

    the
    > > return value :
    > >
    > > <?xml version="1.0" encoding="utf-8" ?>
    > > <string xmlns="http://localhost/CatfishService/"><dsPlayerList
    > > xmlns="http://www.tempuri.org/dsPlayerList.xsd" /></string>
    > >
    > > How do I change the xmlns from "http://www.tempuri.org" to

    localhost
    > > (or whatever URL is applicable at the time)?
    > >
    > > I'll include the code at the end of the message for your review. I
    > > only ask that, if you see the problem and know the answer, you

    relay
    > > your advice in small words :).
    > >
    > > ====================== My Service Code ======================
    > > using System;
    > > using System.Collections;
    > > using System.ComponentModel;
    > > using System.Data;
    > > using System.Data.SqlClient;
    > > using System.Diagnostics;
    > > using System.Web;
    > > using System.Web.Services;
    > > using System.Web.Services.Protocols;
    > > using System.Xml.Serialization;
    > >
    > > namespace CatfishService
    > > {
    > > /// <summary>
    > > /// Summary description for Service1.
    > > /// </summary>
    > > ///
    > > [WebService(Description="This is a Service Designed to Manage the
    > > Catfish DB",Namespace="http://localhost/CatfishService/")]
    > >
    > > public class Service1 : System.Web.Services.WebService
    > > {
    > > public Service1()
    > > {
    > > //CODEGEN: This call is required by the ASP.NET Web

    Services
    > > Designer
    > > InitializeComponent();
    > > }
    > >
    > > private SqlConnection SQLConnCatfishDB;
    > > private SqlDataAdapter sdaManageRoster;
    > > private SqlCommand sicAddToRoster;
    > > private SqlCommand scGetPlayerList;
    > > private CatfishService.dsPlayerList dsPlayerList;
    > >
    > > [WebMethod()]
    > > [return: XmlElement

    Namespace="http://localhost/CatfishService/",
    > > ElementName="PlayerList")]
    > >
    > > public string GetPlayerList(int player_id, int is_active)
    > > {
    > > //SQLConnCatfishDB.Open(); (I commented this out because

    it
    > > would not process anything after it, it would just open up a
    > > webpage to nowhere).
    > >
    > > sdaManageRoster.SelectCommand.Prepare();
    > > sdaManageRoster.SelectCommand.Parameters["@player_id"].Value =
    > > player_id;
    > >

    sdaManageRoster.SelectCommand.Parameters["@is_active"].Value =
    > > is_active;
    > >
    > > //sdaManageRoster.Fill(dsPlayerList); (I commented this

    out
    > > for the same reason above, not really certain why it does this).
    > > return dsPlayerList.GetXml();
    > > }
    > >
    > > Thank in advance.
    > >
    > > Cheers,
    > >
    > > Daniel Hobert
    > >
    jerome, Mar 31, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sumaira Ahmad
    Replies:
    2
    Views:
    409
    bruce barker
    Nov 2, 2004
  2. ToChina

    Noob question about returning HTML

    ToChina, Jun 8, 2005, in forum: ASP .Net
    Replies:
    2
    Views:
    334
    ToChina
    Jun 8, 2005
  3. Curly
    Replies:
    0
    Views:
    190
    Curly
    Aug 19, 2003
  4. akosz

    Returning XML from a web service

    akosz, Jun 7, 2004, in forum: ASP .Net Web Services
    Replies:
    1
    Views:
    221
    Dino Chiesa [Microsoft]
    Jun 7, 2004
  5. Leo Violette
    Replies:
    0
    Views:
    1,003
    Leo Violette
    Apr 17, 2009
Loading...

Share This Page