Click here to Skip to main content
15,915,873 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi
How to assign reader value to string collection

StringCollection dodetails = new StringCollection();
dodetails = reader.ToString();


I am getting the below error

Cannot implicitly convert type 'string' to 'System.Collections.Specialized.StringCollection'

Please check

Thanks
Posted
Comments
sriman.ch 24-Nov-11 23:41pm    
you are trying to convert reader into a string not a string collection by using ToString(); then obviously it will throw you an error. Try type casting the reader yar....
Lancy.net 24-Nov-11 23:43pm    
Thanks
Sergey Alexandrovich Kryukov 25-Nov-11 1:54am    
Amazing! Why would you even expect something like that? And what would be the purpose? A string is a string, not a collection of strings.
--SA

Datareader is a "stream-based" data object and the data can be access only sequentially. So, there is no way to directly assign the data reader to a string collection.

Below is one of the ways to accomplish what you wish -

C#
if (reader.HasRows)
{
    while (reader.Read())
    {
         dodetails.Add(reader.GetString(column_number))
    }
}


HTH!

- Dinesh
 
Share this answer
 
Comments
Lancy.net 25-Nov-11 0:50am    
Thanks
C#
StringCollection dodetails = new StringCollection();
//Checking whether reader contains any value oe not.
if (reader.HasRows)
  {
      int i=0;
           while (reader.Read())
           {
               dodetails.Add(reader.GetValue(i));
              //Storing all Datarader value into collection
           }
   }
 
Share this answer
 
Comments
Lancy.net 25-Nov-11 0:50am    
Thanks

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