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 8232889
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:58:15+00:00 2026-06-07T17:58:15+00:00

I have a problem,i have to retrieve the data from database and show it

  • 0

I have a problem,i have to retrieve the data from database and show it cshtml in a grid format….i have the data in view,but its not showing the i am geting an error this is how my code looks

               BugTracker Model

      namespace Gridview_BugTracker.Models
         {
        public class BugTracker_DataHelper
           {
           public static List<BugTracker_DataHelper> GetList{get;set;}        
           public string projectName { get; set; }           
           public string Description { get; set; }
           public  string status { get; set; }           
            }

          ------------------------------
         BugTracker Controller


         public ActionResult Index()
    {
        Gridview_BugTracker.Models.BugTracker_DataHelper model = new BugTracker_DataHelper();
        SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS");
        conn.Open();
        SqlCommand dCmd = new SqlCommand("Select * from Projects", conn);
        SqlDataAdapter da = new SqlDataAdapter(dCmd);
        DataSet ds = new DataSet();
        da.Fill(ds);
        conn.Close();
        for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
        {
            model.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
            model.Description = ds.Tables[0].Rows[i]["Description"].ToString();
            model.status = ds.Tables[0].Rows[i]["Status"].ToString();
        }


        return View(model);
    }

        Index.Cshtml

       @model  IEnumerable<Gridview_BugTracker.Models.BugTracker_DataHelper>

       @{
       ViewBag.Title = "Index";
       }

      <h2>Index</h2>


       <p>
        @Html.ActionLink("Create New", "Create")
       </p>
      <table>
        <tr>
           <th>
              ProjectName
          </th>
       <th>
             Description     
       </th>
       <th>
           Status
       </th>    
    </tr>

   @foreach (var item in Model)
    {
       <tr>
         <td>
            @Html.DisplayFor(modelItem => item.projectName)
        </td>
      <td>
        @Html.DisplayFor(modelItem => item.Description)
      </td>
    <td>
        @Html.DisplayFor(modelItem => item.status)
    </td>

    <td>
        @Html.ActionLink("Edit", "Edit", new { id = item.projectName }) |
        @Html.ActionLink("Details", "Details", new { id = item.Description }) |
        @Html.ActionLink("Delete", "Delete", new { id = item.status })
       </td>
  </tr>
  }

I am getting this error when i execute the program

            error
     ----------------
       The model item passed into the dictionary is of type 'Gridview_BugTracker.Models.BugTracker_DataHelper',
but this dictionary requires a model item of type
'System.Collections.Generic.IEnumerable`1[Gridview_BugTracker.Models.BugTracker_DataHelper]'. 

so can any one help where am i doing wrong or do i have to add any thing more…..

  • 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-07T17:58:16+00:00Added an answer on June 7, 2026 at 5:58 pm

    Because you are returning only one instance of BugTracker_DataHelpe object where the View expects a List of BugTracker_DataHelper objects. So return a list of objects to the View.

    I would move the database acces code to a seprate method so that i can call it from multiple places when needed

    public List<BugTracker_DataHelper> GetAllBugs()
    {
        var modelList = new List<BugTracker_DataHelper>();
        using(SqlConnection conn = new SqlConnection(@"Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=BugTracker;Data Source=SSDEV6\SQLEXPRESS"))
        {
          conn.Open();
          SqlCommand dCmd = new SqlCommand("Select * from Projects", conn);
          SqlDataAdapter da = new SqlDataAdapter(dCmd);
          DataSet ds = new DataSet();
          da.Fill(ds);         
          for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
          {
            var model = new BugTracker_DataHelper();
            model.projectName = ds.Tables[0].Rows[i]["projectName"].ToString();
            model.Description = ds.Tables[0].Rows[i]["Description"].ToString();
            model.status = ds.Tables[0].Rows[i]["Status"].ToString();
            modelList.Add(model);
         }
        }
        return modelList;
    }
    

    Now call this method in your Action

    public ActionResult Index()
    {
       var bugList=GetAllBugs();
       return View(bugList);    
    }
    

    Few more suggestions also

    1) Add proper exception handling to the code

    2) Change the Select * to Select specific column name format.

    3) Check for null before accessing and calling functions(ToString() in this example) on the DataRow cell values.

    4) If you are simply reading the list of items, Use DataReader instead of DataSet

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

Sidebar

Related Questions

I have problem SIMILAR to preventing form data reposting, but not quite the same
I have my own TableModel implementation designed to show data from a SQL database.
I have a problem with getting jquery to retrieve results from a WCF service.
I have next problem, when I'm trying to retrieve value from xforms:select elements I
I try to retrieve HTML data from my database and display them in a
Basically I have a datagridview which I retrieve from database and 3 of the
I am using LINQ to retrieve data from the Database, the variable name relative
I am trying to index data from database. I am doing that successfully. But
The project I have been given is to store and retrieve unstructured data from
I have a background worker that performs loading of data from the database 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.