Web Service Help for a Noob : Returning an XML String

N

Nastero

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
 
J

jerome

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 said:
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 said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top