How can I put the values of database in array in ASP.NET MVC C# ?

How can I put the values of database in array in ASP.NET MVC C# ? 

Asked by:- DixonCh
: 2533 At:- 9/4/2017 3:17:55 PM

can you explain your question in detail? What exactly you are trying to do? 0
By : vikas_jk - at :- 9/4/2017 3:30:08 PM

1 Answers
profileImage Answered by:- vikas_jk

If I am getting it right you are trying to create a List of the same data-type, after fetching data from database, you can try this

First, create a class of data-type which you want, example

 public class Student
            public int ID { get; set; }
            public string Name { get; set; }
            public DateTime DateOfBirth { get; set; }


then get data from the database and save it in a List of Student type

 List<Student> student = new List<Student>();
            SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=Student.mdf;Integrated Security=True;User Instance=True");
            SqlCommand cmd = new SqlCommand("select * from Student", conn);
            SqlDataReader dr;
                dr = cmd.ExecuteReader();
                while (dr.Read())
                    student.Add(new Student()
                        ID = dr.GetInt32(dr.GetOrdinal("ID")),
                        Name = dr.GetString(dr.GetOrdinal("Name")),
                        DateOfBirth = dr.GetDateTime(dr.GetOrdinal("DateOfBirth"))
            catch (Exception exp)


That's it, let me know if this helps you

At:- 9/4/2017 3:38:13 PM
Thank you so much for your response. That's what I needed. 0
By : DixonCh - at :- 9/4/2017 4:39:55 PM
Btw, I have already mentioned the connection to the database in web.config file. Do I need to write the connection part again in the code as you had shown in the code? I'm using technique. 0
By : DixonCh - at :- 9/4/2017 4:47:04 PM
No, you don't need to write your connection string again, you can fetch it from web.config file using this code System.Configuration.ConfigurationManager.ConnectionStrings["connectionStringName"].ConnectionString; 0
By : vikas_jk - at :- 9/4/2017 6:12:04 PM

Login/Register to answer
Register directly by posting answer/details

Full Name *

Email *

By posting your answer you agree on privacy policy & terms of use