Click here to Skip to main content
15,880,967 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to store data value of query (select emp_name from employee) in a array and idsplay all data in list box ?
Posted
Updated 28-Jun-12 20:30pm
v2

C#
DataTable dt = new DataTable();
using (SqlConnection con = new SqlConnection(connectionString))
    {
    con.Open();
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT emp_name FROM employee", con))
        {
        da.Fill(dt);
        }
    }
MyListBox.DataSource = dt;
 
Share this answer
 
Comments
Janardan Pandey 29-Jun-12 2:33am    
i want to store data of sql statment in an array. U store it in data table.
OriginalGriff 29-Jun-12 2:39am    
Only as a transport mechanism to get it into your list box - it doesn't matter at that point what it is in.
To use an array only would be a pain - you would need to find the number of entries in advance in order to assign enough elements before you started filling them.
_Amy 29-Jun-12 2:43am    
Yes. You are right. If we can do it directly, why we need array? Anyways, I have given one answer below. That can convert the datatable to a multidimensional array..
Hi,
Use this.. This statement will store the data in "results" array.
C#
DataTable docData= new DataTable();
using (SqlConnection con = new SqlConnection(connectionString))
{
    con.Open();
    using (SqlDataAdapter da = new SqlDataAdapter("SELECT emp_name FROM employee", con))
    {
        da.Fill(docData);
    }
}
//storing the datatable into multidimensional array
string[,] results = new string[doows.Count, docData.Columns.Count];

for (int rowCount = 0; rowCount < docData.Rows.Count; rowCount++)
{
  for (int colCount = 0; colCount < docData.Columns.Count; colCount++)
  {
    results[rowCount, colCount] = docData.Rows[rowCount][colCount].ToString();
  }
}

All the best.
--Amit
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900