views:

745

answers:

3

Hi There, Well, first of all sorry about this question it must be pretty straight forward for you guys but I'm struggling myself on it, and I need to make it work :( Well I'm trying t o use DataSet on my application

and when I render it I got:

The type 'System.Data.DataSet' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Data

in my application System.Data is already being referenced from C:\Windows\Microsoft.NET\Framework\v2.0.50727\System.Data.dll

and I'm using on my using clauses as well

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;

This DataSet is a response from a Webservice So any Ideas on how to fix this problem?

PS. I don't know if it helps, but I'm using nHaml to render my view

Thanks a lot


UPDATE:

The only solution I found for now was to instead passing a DataSet to the view converter the DataSet to a

<List<List<String>>

and pass a loop through the entire DataSet like this

List<List<String>> myList = new List<List<String>>();

foreach (DataRow row in dsTrades.Tables[0].Rows)
{
    List<String> myListColumns = new List<String>();

    for (var index = 0; index < dsTrades.Tables[0].Columns.Count; index ++)
    {
        myListColumns.Add(row[index].ToString());
    }

    myList.Add(myListColumns);
}

// THIS SHOULD BE THE DATASET AND NOW 
// IT'S CONVERTED TO A LIST WITH STRINGS LIST INSIDE
viewModel.Trades = myList; 

return View(viewModel);

Actually this is completely crazy ins't it?

All this job could be easily done into the view if using DataSet directly I hope anyone can help me with a more simplistic way to do it

Thank you :)


UPDATE (SOLUTION)

Simon's answer was really effective and it worked on the first try after adding namespaces for System.Data and System.Xml but at the same time, Josh's answer present a very nice and cool way to work with DataSets, which on my opinion works much better and I think I'll go for it now.

Thanks for you help

+1  A: 

The only thing I could think of is that in the context of the page, the System.Data reference is not visible.

Try adding the namespace in your web.config:

<pages>
   <controls />
   <namespaces>
      <add namespace="System.Data"/>
   </namespaces>
</pages>

I know it's not really part of your question, but I would recommend building a class filled with properties representing the fields in your datatable. Using Linq, you can easily convert your rows into the class object and return a list of them. Here's some rough (and uncompiled) code.

[Serializable]
public class MyClass
{
   public string Property1 { get; set; }
   public string Property1 { get; set; }
}

You want it to be serializable so your web service can return it as xml or json (however you are returning it). The linq would look something like this:

var result = from r in dataSet.Table[0].Rows.AsEnumerable()
             select new MyClass() {
                Property1 = r["Field1"].ToString()
                Property2 = r["Field2"].ToString()
             };

return result.ToList();

In my personal experience, DataSets tend to be resource hogs. Also, Linq will be more efficient than your for loops.

Hope this helps.

Josh
Wow Josh, this second example you show me really worked, and it's much cleaner and well organised, I really like things like this. The only thing is that I had to remove the AsEnumerable() method to make it work. But now works like a charm! :) thanks a lot for it, unfortunately the web.config example didn't work, but don't bother about it. thanks again
ludicco
+2  A: 

try adding an explicit reference to System.Data in your nhaml configuration

<?xml version="1.0"?>

<configuration>
...
    <configSections>
            <section name="nhaml" type="NHaml.Configuration.NHamlConfigurationSection, NHaml"/>
    </configSections>
...
<nhaml autoRecompile="true">
            <assemblies>
                    <clear/>
                    ...
                    <add assembly="System.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"/>
            </assemblies>
            <namespaces>
                    <clear/>
                    ...
                    <add namespace="System.Data"/>
            </namespaces>
    </nhaml>

obviously replacing "..." with your other references and config

Simon
Perfect Simon, this worked very well, after adding this I had also to add directions for System.Xml but it was exactly the same procedure.This worked, and answered my question, but actually I really like the way Josh presented a new solution to pass DataSets through, so Thumbs Up for you both, thanks for you help!
ludicco
A: 

Delete the Reffrence(system.Data) ..and Add the same reffrence again .. it might be work..

San