Answered by:
How to read data from database and store it in array

Question
-
User1006710852 posted
I am uisng asp.net 3.5 with c# as language. I want to read the database and store the data in either array or arraylist so that it can be called further in a dropdown list. How to proceed, pls give me some idea
Wednesday, February 23, 2011 5:20 AM
Answers
-
User-1373473781 posted
First make one class having similar attribute as you database table for eg: public class Student { public int ID { get; set; } public string Name { get; set; } public DateTime DateOfBirth { get; set; } } after that List<Student> student = new List<Student>(); SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Prac\TestAssembly\ForumsProjectCSharp\App_Data\Studetnt.mdf;Integrated Security=True;User Instance=True"); SqlCommand cmd = new SqlCommand("select * from Student", conn); SqlDataReader dr; try { conn.Open(); 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")) }); } dr.Close(); } catch (Exception exp) { throw; } finally { conn.Close(); }
Try this- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 23, 2011 5:45 AM
All replies
-
User-1373473781 posted
First make one class having similar attribute as you database table for eg: public class Student { public int ID { get; set; } public string Name { get; set; } public DateTime DateOfBirth { get; set; } } after that List<Student> student = new List<Student>(); SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=D:\Prac\TestAssembly\ForumsProjectCSharp\App_Data\Studetnt.mdf;Integrated Security=True;User Instance=True"); SqlCommand cmd = new SqlCommand("select * from Student", conn); SqlDataReader dr; try { conn.Open(); 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")) }); } dr.Close(); } catch (Exception exp) { throw; } finally { conn.Close(); }
Try this- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Wednesday, February 23, 2011 5:45 AM -
User1992938117 posted
it can be called further in a dropdown list.so why not you are binding dropdowllist directly:
SqlConnection con = new SqlConnection("Server=CRC;Database=ABCD;Trusted_Connection=True"); SqlCommand cmd = new SqlCommand("Select State from STATE"); SqlDataAdapter objAdapter=new SqlDataAdapter(); DataSet dset=new DataSet objAdapter.selectCommand=cmd; objAdapter.Fill(dset,"state"); con.close(); dropdownlist1.DataSource=dset.Tables[0]; dropdownlist1.DataTextField=dset.Tables[0].Columns[0].ToString() dropdownlist1.DataValueField=dset.Tables[0].Columns[0].ToString(); dropdownlist1.dataBind();
Wednesday, February 23, 2011 5:57 AM -
User542821068 posted
int[] _myArr = new int[NUMBER];
where number is the field which u have fetched from database
Wednesday, February 23, 2011 6:05 AM -
User-1706566334 posted
Hi,
using DataTable retreview the data from Table, then create array list, convert the datatable value to string using " , ", then using split u can load the array list, if u load the value in DropDown list mean y u using Array List, from Data Table to Drop Down List u can directly load. no need for array list.
EG Using Array:
DataTable DT=new DataTable();
string[] arr;
if(DT.Rows.Count>0)
{
string StrArray = string.Join(",", (from DataRow row in DT.Rows select (string)row["DataTableColumnName"]).ToArray());
arr = StrArray.Split(',');
}Wednesday, February 23, 2011 7:36 AM