HTTP status 401: Unauthorized when accessing web service method via winform in same VS2005 solution.

H

hazz

I don't get it. I have a VS2005 solution with a web service project and a
windows project. The web service when tested on its own works fine. But when
I add it as a web reference and refer to it my windows form code behind I
receive the following;
[System.Net.WebException] {"The request failed with HTTP status 401:
Unauthorized."}
when trying to execute
this.dataGridView1.DataSource = ws.ReturnStudentDS();

not sure if it is relevant but the web.config in the web service project has
<authentication mode="Windows"/>


Here is the webservice.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

}
[WebMethod]
public DataSet ReturnStudentDS() {
SqlConnection myConnection = new
SqlConnection("server=(local);database=Smartdb ;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from
Student", myConnection);
DataSet ds = new DataSet();
return ds;
}
}


Here is the winform accessing the webmethod.
public Form1()
{
InitializeComponent();
try
{

returnStudentDS.Service ws = new returnStudentDS.Service();
this.dataGridView1.DataSource = ws.ReturnStudentDS();
<-------------------- line where exception is thrown
}
catch (Exception e)

{
Console.WriteLine("{0} Exception caught.", e);

}

Thanks,
-hazz
 
A

Alvin Bruney - ASP.NET MVP

is the user executing the code authenticated?
It seems like it is not. You can try removing authentication and seeing if
you can hit the webservice.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
 
H

hazz

I am running it all on my local machine Alvin.
Stupid question on my part. What do you mean executing the code
authenticated. How do I remove authentication?
setting it to None in the web config? I tried that.

Thanks, -hazz

Alvin Bruney - ASP.NET MVP said:
is the user executing the code authenticated?
It seems like it is not. You can try removing authentication and seeing if
you can hit the webservice.

--
Regards,
Alvin Bruney [MVP ASP.NET]

[Shameless Author plug]
The Microsoft Office Web Components Black Book with .NET
Now Available @ www.lulu.com/owc
Forth-coming VSTO.NET - Wrox/Wiley 2006
-------------------------------------------------------



hazz said:
I don't get it. I have a VS2005 solution with a web service project and a
windows project. The web service when tested on its own works fine. But when
I add it as a web reference and refer to it my windows form code behind I
receive the following;
[System.Net.WebException] {"The request failed with HTTP status 401:
Unauthorized."}
when trying to execute
this.dataGridView1.DataSource = ws.ReturnStudentDS();

not sure if it is relevant but the web.config in the web service project has
<authentication mode="Windows"/>


Here is the webservice.
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
public class Service : System.Web.Services.WebService
{
public Service () {

}
[WebMethod]
public DataSet ReturnStudentDS() {
SqlConnection myConnection = new
SqlConnection("server=(local);database=Smartdb ;Trusted_Connection=yes");
SqlDataAdapter myCommand = new SqlDataAdapter("select * from
Student", myConnection);
DataSet ds = new DataSet();
return ds;
}
}


Here is the winform accessing the webmethod.
public Form1()
{
InitializeComponent();
try
{

returnStudentDS.Service ws = new returnStudentDS.Service();
this.dataGridView1.DataSource = ws.ReturnStudentDS();
<-------------------- line where exception is thrown
}
catch (Exception e)

{
Console.WriteLine("{0} Exception caught.", e);

}

Thanks,
-hazz
 

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

Top