ASP.NET MVC - System.Data.DataSet not referenced problem

﹥>﹥吖頭↗ 提交于 2019-12-14 02:43:54

问题


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:


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




回答2:


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.




回答3:


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



来源:https://stackoverflow.com/questions/1612104/asp-net-mvc-system-data-dataset-not-referenced-problem

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!