Click here to Skip to main content
15,894,955 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to copy SqlDataReader values to an Array. I have qno's on SqlDataReader. Can some one help me?

C#
protected void Page_Load(object sender, EventArgs e)
   {
       SqlCommand cmd = new SqlCommand("select qno from QUIZ1 where applicationname = @appname", con);
       cmd.Parameters.AddWithValue("@appname", "MFDL");
       con.Open();

       SqlDataReader sdr = cmd.ExecuteReader();
       while (sdr.Read())
       {
           string[] numb;
           numb = new string[sdr.FieldCount];
           for (int i = 0; i < sdr.FieldCount; i++)
           {
               numb[i] = sdr[i].ToString();

           }
           Label1.Text = numb[0].ToString();

       }
Posted
Updated 4-Jul-13 21:58pm
v3
Comments
sameer_dev 26-Mar-21 2:34am    
if we want to store two values in different arraylist

From your code, I guess you are trying to get all the columns of all the rows.

For that, take one ArrayList containing object Arrays, which are DataReader rows.
C#
ArrayList al = new ArrayList();

while(sdr.Read()) {
    Object[] numb = new Object[sdr.FieldCount];
    
    // Get the Row with all its column values..
    sdr.GetValues(values);
    
    // Add this Row to ArrayList.
    al.Add(values);
}

// Now the ArrayList is having all the Rows and their respective column values.
// Do whatever you want to do now. Below is the code to write all the values.

dr.Close();
con.Close();

// Writing all the Column values of all the Rows.
foreach(Object[] row in al) {
    foreach(object column in row) {
        Response.Write(column.ToString() + "<br>");
    }
}
 
Share this answer
 
v2
Comments
mskphani 5-Jul-13 4:37am    
Hi Tadit,

Instead of "Response.Write(column.ToString() + "<br>");" this statement i need to put that output in an array ?
Can you help me ?
Ok. Declare an List<string> and add values to it. Something like below.

List<string> columnValues = new List<string>();

foreach(Object[] row in al) {
foreach(object column in row) {
columnValues.Add(column.ToString());
}
}

Try and let me know.
mskphani 5-Jul-13 4:43am    
Thank you very much Tadit. I was able to do it. Thanks for helping
Most Welcome buddy... :) Thanks for accepting the answer. :)
clearly there is problem in code ...

C#
while (sdr.Read())
{
string[] numb;



everytime numb array will reinitialize .... so keep this array outside the loop then it should work fine
 
Share this answer
 
You select only qno, so there is not need of the for loop inside the while loop. Try this code
C#
SqlDataReader sdr = cmd.ExecuteReader();
List<string> numb = new List<string>();
while (sdr.Read())
{
numb.Add(sdr[0].ToString());
}
 
Share this answer
 
Comments
mskphani 5-Jul-13 3:39am    
Hi thanh_bkhn

I am retrieving q.no column which has 5 rows. I need to copy all those five rows into an array. How can i do it ?
thanh_bkhn 5-Jul-13 3:42am    
After the loop, numb itself is the array you need.
I can't pretend that I quite understand what it is you're trying to do, but I THINK that something like this is what you need:

C#
    SqlCommand cmd = new SqlCommand("select qno from QUIZ1 where applicationname = @appname", con);
    cmd.Parameters.AddWithValue("@appname", "MFDL");
    con.Open();

    SqlDataReader sdr = cmd.ExecuteReader();

    List<string> numbersList = new List<string>();

    while (sdr.Read())
    {
        numbersList.Add(Convert.ToString(sdr["qno"]));
    }

string[] numbersArray = numbersList.ToArray();


Mind you, if I were you, I'd work with the resultset as a genereic list instead of an array, but if that's what you really want, then it's up to you...
 
Share this answer
 
v3

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