3

In my MVC application am retrieving data from Database. I want to display the retired data in tables. controller code:

 public ActionResult MyAccount()
    {

        var user = User.Identity.Name;

       string sThumbnails = "";

        DataSet dsTemplates = new DataSet();

        string qryTemplets = "select * from FileInfo where UserName= '" + user + "'";

        open_Connection();
        SqlDataAdapter daTemplate = new SqlDataAdapter(qryTemplets, conn);
        daTemplate.Fill(dsTemplates, "FileInfo");
        DataTable dtTemplates = new DataTable();
        dtTemplates = dsTemplates.Tables[0];

        foreach (DataRow row in dtTemplates.Rows)
        {

            sThumbnails = sThumbnails +  row["FileName"] +  row["Date"] +  row["Time"] +  row["Info"] ;
            ViewData["thumbs"] = sThumbnails;
        }
        close_Connection();
        return View();
    }

view code:

    <div id="formleft" style="border-style: solid; border-color: inherit; border-width: 4px; width:550px; height:500px; position:absolute; top: 345px; left: 348px;">
   <h2 class="heading" style="text-align: center;">Info</h2><%= ViewData["thumbs"] %> </div>

This code displays the data But i want to display it in tabular format.

How can i display the data in tabular format?

0

2 Answers 2

5

Don't use ViewData, use a Model built using a custom class to store that information. Something like:

public class TableInfo
{
    public string FileName { get; set; }
    public string Date { get; set; } //you might want to change this to DateTime
    public string Time { get; set; }  //this may need changing to TimeSpan or int possibly?
    public string Info { get; set; }
}

Then you can do:

List<TableInfo> tables = dtTemplates.Rows.AsEnumerable()
    .Select(t => new TableInfo
        {
            FileName = row["FileName"],
            Date = row["Date"],
            Time = row["Time"],
            Info = row["Info"]
        })
    .ToList();

    close_Connection();
    return View(tables);

Then in your view you can do:

@model List<TableInfo>

if (Model.Count > 0)
{
    <table>
        <tr>
            <td>File Name</td>
            <td>Date</td>
            <td>Time</td>
            <td>Info</td>
        </tr>
        @foreach (TableInfo item in Model)
        {
            <tr>
                <td>@item.FileName</td>
                <td>@item.Date</td>
                <td>@item.Time</td>
                <td>@item.Info</td>
            </tr>
        }
    </table>
}
Sign up to request clarification or add additional context in comments.

6 Comments

Am getting error for .Select(t => new FileInfo as: 'System.Data.DataRowCollection' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'System.Data.DataRowCollection' could be found (are you missing a using directive or an assembly reference?)
One more thing is FileInfo table from database contains record of all the users. I want to display the data of the logged in user only. Am developing this application in MVC2
ah you'll need to use Rows.AsEnumerable(). Make sure you add using System.Linq; too.
Can you provide me the full code.. I am still confused. I want it urgent. so please
@pihu Check my updated edit. You need to do Rows.AsEnumerable()
|
-2

Add follow code Before Html Code in your Model

<style> 
   table th {  border: 1px solid black; text-align:center }
   table td {  border: 1px solid black; text-align:center } 
  </style>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.