Sivakumar
Sivakumar

Reputation: 15

Returning Dataset to controller in asp.net MVC

In Data access layer I have method to retrieve all the table values. How can I return it into the controller in asp.net MVC. can anyone help me?

Thanks in advance.

public static Dataset GetMembers()
{ 
    //sql steps to retrieve records and fill it in dataset 
} 

Controller code:

{
    var members = class.GetMembers()

    return View(members);
}

Upvotes: 0

Views: 4620

Answers (1)

David Fox
David Fox

Reputation: 10753

You'll need to modify your GetMembers method (or implement another one that returns IEnumerable<YourType>. In this example (since you don't provide any additional data), I'll return a list of MyType, which I'll assume looks like:

public class MyType
{
    public int ID { get; set; }
    public string Name { get; set; }
}

And then populate a List<MyType> inside GetMembers

public static IEnumerable<MyType> GetMembers()
{
List<MyType> list = new List<MyType>();

using(SqlConnection db = new SqlConnection(connectionString))
{

    DataSet ds = new DataSet();
    // get a DataSet as normal
    foreach (DataRow dr in ds.Tables[0].Rows)
    {
        list.Add(new MyType()
        {
            ID = Convert.ToInt32(dr["ID"].ToString()),
            Name = dr["Name"].ToString())
        });
    }
}

return list;
}

This is a basic example of how you could return IEnumerable (of which List<T> implements) from your method.

Upvotes: 1

Related Questions