469,920 Members | 2,355 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,920 developers. It's quick & easy.

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

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
Nov 17 '05 #1
2 6936
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" <hazz@sonic_net> wrote in message
news:ev*************@TK2MSFTNGP09.phx.gbl...
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

Nov 17 '05 #2
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" <www.lulu.com/owc> wrote in message
news:u9**************@TK2MSFTNGP14.phx.gbl...
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" <hazz@sonic_net> wrote in message
news:ev*************@TK2MSFTNGP09.phx.gbl...
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


Nov 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by Sparkplug | last post: by
2 posts views Thread by Iain Adams | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.