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

  • SEARCH
  • Home
  • 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 3626762
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T23:48:11+00:00 2026-05-18T23:48:11+00:00

I have code, which returns me a row out of a database, con =

  • 0

I have code, which returns me a row out of a database,

            con = new System.Data.SqlClient.SqlConnection();
            dsl = new DataSet();
            con.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=C:\\tbl.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True";

            con.Open();

            string sql = "SELECT * From tbl_fb";
            da = new System.Data.SqlClient.SqlDataAdapter(sql, con);
            da.Fill(dsl, "fb");
            DataRow dRow = dsl.Tables["fb"].Rows[0];
            ViewData["a"] = dRow.ItemArray.GetValue(1).ToString();

            ViewData["b"] = "afagjma";
            con.Close();

Is there a way, that I could use a loop to get all rows from table. The number of rows are unknown.
I know I can do it with a loop, by using the variable (i) instead of numbers. But then I would need to use the ViewData array, which is problem for me.

Example: ViewData[“a”][i];

  • 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-05-18T23:48:12+00:00Added an answer on May 18, 2026 at 11:48 pm

    You should set up a class that defines what data you’re returning from your database.

    For instance:

    public class Customer  
    {
       public int Id { get; set;} 
       public string Name { get; set;} 
    } 
    

    And then you would create a List<Customer>(), and set this as the Model for your MVC Page.

    For instance:

    var data = new new List<Customer>();    
    ViewData.Model = data; 
    

    And load your data into your list:

    foreach(var row in dsl.Tables["fb"].Rows)
    {
       var customer = new Customer(); 
       customer.Id = row.Field<int?>("Id"); 
       customer.Name = row.Field<string>("Name");
       data.Add(customer);
    }
    

    And then, on your MVC Page, set the <%@ Page %> header to have the Inherits attribute like so:

    <%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<IEnumerable<Customer>>" %>
    

    Now finally you can bind directly to the Model within the page:

    <% foreach (var item in Model) { %>
      Customer Id: <%: item.Id %>
      Name: <%: item.Name %>
    <% } %>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some code which returns InnerXML for a XMLNode. The node can contain
I have have some code which adds new cells to a table and fills
I have some JS code which generates the following object, return { type: some
I have the following code which works fine. However, I only want to return
I have a bit of PHP code which I need to return an even
I have code which needs to do something like this There is a list
I have code which looks like: private static DirectiveNode CreateInstance(Type nodeType, DirectiveInfo info) {
I have code which as been working against an older Active Directory server and
I already have code which lazy loads scripts on request. My issue now is
I have some code which collects points (consed integers) from a loop which looks

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.