views:

336

answers:

2

I am new to .NET and C#. I created a Web service, and I am able to view it from a Web page. When I try to call it from a Windows Application, I get the Exception 401 : unauthorized. Code compiles OK, but throws exception when running. This is the code from the Windows App. :

namespace BookStore
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // Create a new instance of a service
            localhost.Service1 datasvc = new localhost.Service1();

            // Create instance of dataset, using WebService method GetTitleAuthors.
            DataSet myData = datasvc.GetTitleAuthors();

            // Set DataGrid's datasource to the myData DataSet.
            dataGridView1.DataSource = myData;

            //Expand all rows.            
            //dataGridView1.Expand(-1);

            //Expand DataTable
            //dataGridView1.NavigateTo(0, "Authors"); 
        }
    }
}

PS : I am using Windows Authentication in the website that hosts the web service.

+1  A: 

I believe there is a property on the generated proxy to the effect of UseDefaultCredentials try setting that to true.

datasvc.UseDefaultCredentials = true;

Although it's been a while I think this will force the service to pass windows credentials.

Quintin Robinson
This worked OK. Only issue I have is that the form opens with no data. I took this example from a book, that uses VS 2005, with a Datagrid, but I am using a DataGridView in VS 2008.
I had to comment 2 lines that would expand the DataGridView and select the Authors col. ://dataGridView1.Expand(-1);//dataGridView1.NavigateTo(0, "Authors");If I uncomment these lines, compiler errors generate (seems like Expand, and NavigateTo methods are not part of DataGridView component).
Well I'm glad that the solution worked for you. The other issue is because the datagridview is not the same as the old datagrid. For full column select you need to change the grids SelectionMode property to ColumnHeaderSelect however you cannot use auto generated columns in this mode.
Quintin Robinson
Is there a control like DataGridView, but that aut. allows for deletes, updates, and adding records? I mean, for the DataGridView, you have to code for theses tasks. Something like in MS Access where you design a form and all functionality is built-in. Seems like .NET always makes you work more.
The distinction there being that MS Access actually is a database with slight scripting/ui ability layered onto it. Where as .NET is rich platform to develop on that can connect to multiple back end stores and offers flexibility at the cost of designing your own interfaces to such back ends.
Quintin Robinson
+1  A: 

I don't know what type your Service1 object inherits so I can't say what properties or methods you have associated with it, but whenever I know you can make calls to you web service with using

HttpWebRequest req = (HttpWebRequest)WebRequest.Create(URL);

And then either using

req.UseDefaultCredentials = true;

or

req.Credentials = new NetworkCredential(userName, password);
thetreat