Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Typed DataSet and Web Service
Message
From
17/04/2008 12:36:26
 
 
To
17/04/2008 01:27:05
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
C# 2.0
OS:
Windows XP SP2
Database:
MS SQL Server
Miscellaneous
Thread ID:
01311278
Message ID:
01311484
Views:
25
>>Ok, so I tried this message in the 'Web Services' category, and go no responses at all. I'm hoping that's because nobody follows that category. This seems to the category where most problems are sorted out, so I figured I'd try it here. Following is the message I posted in the Web Service section. I'm hoping somebody can help me out here.
>>
>>---------------------
>>
>>I've been following Kevin M's thread #1309187 as it evolved, hoping it would give me the answer I want, but as luck would have it...
>>
>>Anyway, let me preface this by saying that Yankee Stadium couldn't hold what I don't know about .Net.
>>
>>Now, the question is, why can't I get a typed dataset to return actual data from a web service?
>>
>>I'm using the AdventureWorks database in MS SqlServer (SqlExpress).
>>
>>'dsDept' is a typed dataset created through VS 2005. It uses a stored proc to bring back the data. The stored proc is simply
>>
>>
Select * From HumanResources.Department where DepartmentID = @id
.
>>
>>"catsnjazz" is the name of my computer.
>>
>>When I execute the stored proc from VS 2005's Server Explorer, it brings back the correct data.
>>
>>Here is the code for the web service stripped right down to it's knickers. I took out the business objects etc, so that it would be as simple as possible to show the problem.
>>
>>Following the web service code, is the code that calls the web service (also stripped right down). It consists of a form with a button that when clicked starts the process.
>>
>>So, any idea what I'm doing wrong? Don't worry about hurting my feelings if it's something really dumb. And let me point out that if I don't use a typed dataset - if I create a normal dataset, command, datatable, and adapter in my code and return the dataset, it works like I would expect and returns the data requested. With the typed dataset, all I get back is the schema with no data.
>>
>>
// Web Service
>>
>>using System;
>>using System.Data;
>>using System.Web;
>>using System.Collections;
>>using System.Web.Services;
>>using System.Web.Services.Protocols;
>>using System.ComponentModel;
>>
>>namespace wsAwDept
>>{
>>  /// <summary>
>>  /// Summary description for svcAwDept
>>  /// </summary>
>>  [WebService(Namespace = "http://catsnjazz/")]
>>  [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
>>  [ToolboxItem(false)]
>>  public class svcAwDept : System.Web.Services.WebService
>>  {
>>
>>    [WebMethod(Description = "Returns a dataset according to desired id")]
>>    public DataSet GetDeptById(short id)
>>    {
>>      // create dataset (ds), datatable (dt) and table adapter (adapter)
>>      dsDept ds = new dsDept();
>>      dsDept.DepartmentDataTable dt = new dsDept.DepartmentDataTable(ds.Tables[0]);
>>      dsDeptTableAdapters.DepartmentTableAdapter adapter =
>>        new dsDeptTableAdapters.DepartmentTableAdapter();
>>
>>      // call adapter method to fill datatable according to requested id
>>      adapter.FillByID(dt, id);
>>
>>      return ds;
>>    }
>>  }
>>}
>>
// Calling program
>>
>>using System;
>>using System.Collections.Generic;
>>using System.ComponentModel;
>>using System.Data;
>>using System.Drawing;
>>using System.Text;
>>using System.Windows.Forms;
>>
>>namespace awdatatest
>>{
>>  public partial class Form1 : Form
>>  {
>>    // create service, dataset and datatablereader
>>    catsnjazz.svcAwDept ws = new catsnjazz.svcAwDept();
>>    DataSet departmentDS = new DataSet();
>>    DataTableReader dr;
>>
>>    public Form1()
>>    {
>>      InitializeComponent();
>>    }
>>
>>    private void btnStart_Click(object sender, EventArgs e)
>>    {
>>      // create a dataset using service and id of 11
>>      departmentDS = ws.GetDeptById(11);
>>      // set the datatablereader to the table in the dataset
>>      dr = departmentDS.CreateDataReader(departmentDS.Tables[0]);
>>
>>      // see if we have the right table
>>      MessageBox.Show(dr.GetName(0) + "\r\n" +
>>                      dr.GetName(1) + "\r\n" +
>>                      dr.GetName(2) + "\r\n" +
>>                      dr.GetName(3));
>>
>>      if (dr.HasRows)
>>      {
>>        // move to 1st record and display the data
>>        dr.Read();
>>        this.showData(dr);
>>      }
>>      else
>>      {
>>        // no data in the dataset - WHY NOT??
>>        MessageBox.Show("No rows returned");
>>      }
>>    }
>>
>>    // display the data in the dataset
>>    private void showData(DataTableReader dr)
>>    {
>>      MessageBox.Show(dr.GetValue(0).ToString() + "\r\n" +
>>                      dr.GetValue(1).ToString() + "\r\n" +
>>                      dr.GetValue(2).ToString() + "\r\n" +
>>                      dr.GetValue(3).ToString());
>>    }
>>  }
>>}
>
>I see one thing.
>
>When you do this:
>
dsDept.DepartmentDataTable dt = new dsDept.DepartmentDataTable(ds.Tables[0]);
>
>You are creating a "new" table outside of your dataset, so your dataset will be empty if you fill that table. (The table will have data, but you are returning the dataset, not the table.)
>
>
dsDept.DepartmentDataTable dt = ds.DepartmentDataTable;
>
>or
>
>
adapter.FillByID(ds.DepartmentDataTable, id);
>
>HTH

Neither of those is acceptable to the system. When I build I get an error that dsDept.DepartmentDataTable is a 'type' and is not valid in the context.

However, you did put me onto something. I found that the following does work. If I replace
dsDept.DepartmentDataTable dt = new dsDept.DepartmentDataTable(ds.Tables[0]);
with the following 2 lines, everything works as it should, so thanks for the help in putting me on the right track. I was going crazy with this one.
dsDept.DepartmentDataTable dt;
dt = (dsDept.DepartmentDataTable) ds.Tables[0];
I suppose they could be combined into one line, but this looks clearer to me.

The 2nd line can also be (probably better)
dt = (dsDept.DepartmentDataTable) ds.Tables["Department"];
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform