Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9143939
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T10:08:52+00:00 2026-06-17T10:08:52+00:00

I’m terribly new to MVC and programming in general, and I’m having trouble loading

  • 0

I’m terribly new to MVC and programming in general, and I’m having trouble loading data from a SQL Server database into my MVC3 application. I couldn’t get it to work with a model, so I only have a controller and a view. The code looks like this:

Controller:

public ActionResult Datatable()
{
    string conn = System.Configuration.ConfigurationManager.ConnectionStrings["SampleDBConn"].ToString();
    SqlConnection connect = new SqlConnection(conn);
    connect.Open();

    SqlCommand RID = new SqlCommand("SELECT Request_ID FROM SampleDB.dbo.Transactions", connect);
    SqlCommand Tdate = new SqlCommand("SELECT Trans_Date FROM SampleDB.dbo.Transactions", connect);
    SqlCommand MID = new SqlCommand("SELECT Merchant_ID FROM SampleDB.dbo.Transactions", connect);
    SqlCommand Ttype = new SqlCommand("SELECT Trans_Type FROM SampleDB.dbo.Transactions", connect);
    SqlCommand Tamt = new SqlCommand("SELECT Total_Amt FROM SampleDB.dbo.Transactions", connect);

    DataTable dt = new DataTable("MyTable");
    dt.Columns.Add(new DataColumn("Request_ID", typeof(SqlCommand)));
    dt.Columns.Add(new DataColumn("Trans_Date", typeof(SqlCommand)));
    dt.Columns.Add(new DataColumn("Merchant_ID", typeof(SqlCommand)));
    dt.Columns.Add(new DataColumn("Trans_Type", typeof(SqlCommand)));
    dt.Columns.Add(new DataColumn("Total_Amt", typeof(SqlCommand)));

        DataRow row = dt.NewRow();
        row["Request_ID"] = RID;
        row["Trans_Date"] = Tdate;
        row["Merchant_ID"] = MID;
        row["Trans_Type"] = Ttype;
        row["Total_Amt"] = Tamt;
        dt.Rows.Add(row);

    connect.Close();
    return View(dt);
}

View:

    @model System.Data.DataTable

    @{
        ViewBag.Title = "Datatable";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

   < h2 > Datatable < /h2 >

   < div id="header" >< /div >

   < br / >

   < style type"text/css" >

         #DT1{display:inline}

   < /style >

   < table border="1" id="DT1" style="background-color: Lime" >

        < thead style="background-color:Olive" >
            < tr >

             @foreach (System.Data.DataColumn col in Model.Columns)
             {
                    < th >@col.Caption< /th >
             }
            </tr>

        < /thead >

        < tbody >

           @foreach (System.Data.DataRow row in Model.Rows)
           {
                <tr>
                    @foreach (var cell in row.ItemArray)
                    {
                       <td>@cell.ToString()</td>
                    }
               </tr>
             }
       </tbody>
    </table>

The resulting table displays each column like this:

Request_Id      
System.Data.SqlClient.SqlCommand

Where “Request_Id” is the Column header and “System.Data.SqlClient.SqlCommand” is where the data should be

I used this method from another stackoverflow solution:
Displaying standard DataTables in MVC

The end goal of my application is to dynamically query my database via checkboxes or drop-down lists to display charts, and I’ll also need to be able to export the data to an excel file. If anyone has any thoughts or suggestions on how I should approach this, it would be greatly appreciated.

I’ve been able to used a stored procedure to statically create a chart via this method:

    @{
        ViewBag.Title = "Chart1"; 
     }

    < h2 >Chart1< /h2 >
    @{
          var db = Database.Open("SampleDBConn");
          var dbdata = db.Query("August2012byMerch");

          var myChart = new Chart(width: 1100, height: 600, theme: ChartTheme.Green)
          .AddTitle("Merchant Totals by Transaction Type: August 2012")
          .AddSeries("Default",
           xValue: dbdata, xField: "Transaction Type",
           yValues: dbdata, yFields: "Total")
           .Write();
           db.Close();
       }

Thank you!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T10:08:53+00:00Added an answer on June 17, 2026 at 10:08 am

    I’ve found a solution that worked for me as far as the DataTable goes.

    From the advice from Kevin in the comments section, I used a POCO model with an Entity Framework Code-First design.
    Also research lead me to a great tutorial here -> http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-4

    I based my model off this tutorial, then modified it a little to meet my needs.

    Here is the model:

        namespace Example_1.Models
        {
            public class Transaction
            {
                [Key]
                public int TID { get; set; }
                public DateTime TDate { get; set; }   
                public decimal TAmt { get; set; }
                public string Trans_Type { get; set; }
                public int MID { get; set; }
            }
        }
    
        namespace Example_1.Models
        {
             public class Merchants
             {
                [Key]
                public int MID { get; set; }
                public string Merchant { get; set; }
                public List<Transaction> Transactions { get; set; }
             }
        }
        namespace Example_1.Models
        {
            public class SampleDBEntities : DbContext
            {
                public DbSet<Transaction> Transactions { get; set; }
                public DbSet<Merchants> Merchants { get; set; }
            }
        }
    

    The SampleDBEntities class must match the connection string in the web.config file. While the Transactions class and Merchants class must match the variables set up in my database.

    Here is the controller:

         public ActionResult Browse(string Merchant)
        {
            var mercmodel = sampledb.Merchants.Include("Transactions")
                .SingleOrDefault(g => g.Merchant == Merchant);
            return View(mercmodel);
        }
    

    The Merchant variable must match the path taken in your URL. For example my URL for this is
    “http://localhost:64269/Transaction/Browse?Merchant=ISCO_CA“

    Then in the view, I constructed a table where I passed my data into:

        <script src="@Url.Content("~/Scripts/sorttable.js")" type="text/javascript">   </script>
    
    
       @model Example_1.Models.Merchants
    
        @{
            ViewBag.Title = "Browse";
        }
    
    
        <h2>Browse</h2>
    
          <h3>Transactions: @Model.Merchant</h3>
         <table class="sortable">
         <tr>
         <th>ID</th>
         <th>Date</th>
         <th>Merchant</th>
         <th>Amount</th>
         </tr>
            @foreach (var transaction in Model.Transactions)
             {
                <tr>
                  <td>
                    @transaction.TID
                 </td>
                 <td>
                   @transaction.TDate
                 </td>
                 <td>
                   @transaction.Trans_Type
                 </td>
                 <td>
                   $@transaction.TAmt
                </td>
              </tr>
            }
         </table>
    

    This displays a beautiful table that looks like this:

    MyTable

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a view passing on information from a database: def serve_article(request, id): served_article
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
I am currently running into a problem where an element is coming back from
I am using jsonparser to parse data and images obtained from json response. When
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.