Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a DataSet to a View

I am sending a standard Sql select statement to my Sql box via the SqlDataAdapter, then populating a DataSet object.

I can access the rows in the resulting DataSet, but how can I convert the DataSet into a List which can be returned to the MVC View. i.e. I'm assuming a List object is the best way to handle this.

Here's my controller c# code:

public class QAController : Controller
{

    private readonly static string connString = ConfigurationManager.ConnectionStrings["RegrDBConnection"].ToString();
    private readonly static SqlConnection sqlConn = new SqlConnection(connString);
    private readonly static SqlCommand sqlComm = new SqlCommand();

    public ActionResult Index()
    {
        DbRegressionExec();
        return View();
    }
    public static void DbRegressionExec()
    {
        // SELECT TABLE CONTENTS FROM SQL !!
        RegressDB_TableList regresDB = new RegressDB_TableList();
        string sqlStr = "select * from [RegressionResults].[dbo].[Diff_MasterList] order by TableName";

        // POPULATE DATASET OBJECT
        DataSet ds = new DataSet();
        SqlDataAdapter da = new SqlDataAdapter(sqlStr, sqlConn);
        da.SelectCommand.CommandType = CommandType.Text;
        sqlConn.Open();
        try
        {
            da.Fill(ds, "RegresDB");
        }
        catch (Exception e)
        {
            throw;
        }
        finally
        {
            sqlConn.Close();
        }

       // I can iterate thru rows here, but HOW DO CONVERT TO A LIST OBJECT ????

        int numRows = ds.Tables["RegresDB"].Rows.Count;
        for (int i = 0; i < numRows; i++)
        {
            string tblName = ds.Tables["RegresDB"].Rows[i].Field<string>("TableName");
        }

        //List<RegressDB_TableList> masterList = regresDB.RegresTableList.ToList(); //not working !!
        //var masterList = regresDB.TableName.ToList(); //

    }

}

and a simple class I may need to make this happen:

namespace RegressionMvc.Models
{
  public class RegresDB_TableName
  {
     public string TableName { get; set; }
  }
  public class RegressDB_TableList
  {
     public List<RegresDB_TableName> RegresTableList { get; set; }
  }

}

In the end, I'm trying to figure out the best way to handle DataSet results from Sql Server and how to make them back to an MVC View.

I can probably go with jQuery and Json, meaning just convert the data fields to Json and return to JQuery, but I'm sure there are several ways to handle Sql based result sets.

Thanks in advance for your advice....

Best, Bob

like image 250
bob.mazzo Avatar asked Oct 19 '12 21:10

bob.mazzo


1 Answers

In your controller put the code like this

[HttpGet]
public ActionResult View(Modelclass viewmodel)
{
    List<Modelclass> employees = new List<Modelclass>();
    DataSet ds = viewmodel.GetAllAuthors();
    var empList = ds.Tables[0].AsEnumerable().Select(dataRow => new Modelclass{
       AuthorId = dataRow.Field<int>("AuthorId"),
        Fname = dataRow.Field<string>("FName"),
       Lname = dataRow.Field<string>("Lname")
    });
    var list = empList.ToList();



    return View(list);
}

And in view

@{
var gd = new WebGrid(Model, canPage: true, rowsPerPage: 5, selectionFieldName: "selectedRow",ajaxUpdateContainerId: "gridContent");
    gd.Pager(WebGridPagerModes.NextPrevious);}
@gd.GetHtml(tableStyle: "table",

        columns: gd.Columns(
                 gd.Column("AuthorId", "AuthorId"),
                 gd.Column("Fname", " Fname"),
                 gd.Column("Lname", "Lname", style: "description")

 )) 
like image 100
Deepu T Avatar answered Oct 19 '22 11:10

Deepu T