473,387 Members | 1,516 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Concatenate a datareader list into a string

Tom
Hi

I want to retrieve database data and concatenate them to a string. Here is my code

[code

string sql = "Select phone From users where userid=1 or userid=2 or userid=3 or userid=4

SqlDataReader dr = null

dr = SqlHelper.ExecuteReader(DBConnection.ConnString, CommandType.Text, sql)

ArrayList list = new ArrayList(0)
d

list.Add(dr["phone"].ToString()); //runtime error her
} while (dr.NextResult())

string[] array = (string[])list.ToArray(typeof(string));
string result = String.Join(", ", array)

[/code

I want to concatenate the phone number in DB to a string like
result = "123456,234567,345678,456789

How should I modify the code

Thanks
Nov 16 '05 #1
3 8771
Hi Tom,
Hi,

I want to retrieve database data and concatenate them to a string. Here is my code:

Expand|Select|Wrap|Line Numbers
  1.  string sql = "Select phone From users where userid=1 or userid=2 or userid=3 or userid=4;
  2.  SqlDataReader dr = null;
  3.  dr = SqlHelper.ExecuteReader(DBConnection.ConnString, CommandType.Text, sql);
  4.  ArrayList list = new ArrayList(0);
  •  
  • object phoneObj;
  •  
  • //>     do
  •  
  • while( dr.NextResult() )
  •      {
  • //         list.Add(dr["phone"].ToString()); //runtime error here
  •  
  • phoneObj=dr["phone"];
  •  
  • // write phones that aren't empty
  • if( phoneObj!=DBNull.Value ) {
  • list.Add(phoneObj.ToString().Trim());
  • // Trim() only if your numbers should not
  • // have empty spaces
  • }
  •  
  • //>     } while (dr.NextResult());
  •  
  • }
  •  string[] array = (string[])list.ToArray(typeof(string));
  • //> string result = String.Join(", ", array);
  •  
  • string result = String.Join(",", array);
  • // lifting change
  •  

  • I want to concatenate the phone number in DB to a string like:
    result = "123456,234567,345678,456789"

    How should I modify the code?

    Thanks

    If It doesn't do what you want then feedback on group.

    Regards

    Marcin
    Nov 16 '05 #2
    Tom wrote:
    Hi,

    I want to retrieve database data and concatenate them to a string. Here is my code:

    Expand|Select|Wrap|Line Numbers
    1.  string sql = "Select phone From users where userid=1 or userid=2 or userid=3 or userid=4;
    2.  SqlDataReader dr = null;
    3.  dr = SqlHelper.ExecuteReader(DBConnection.ConnString, CommandType.Text, sql);
    4.  ArrayList list = new ArrayList(0);
    5.      do
    6.      {
  •  
  • I think you should check if dr["phone"] == null before calling ToString():
  • if (dr["phone"] != null)
  •          list.Add(dr["phone"].ToString()); //runtime error here
  •      } while (dr.NextResult());
  •  string[] array = (string[])list.ToArray(typeof(string));
  •  string result = String.Join(", ", array);
  •  

  • I want to concatenate the phone number in DB to a string like:
    result = "123456,234567,345678,456789"

    How should I modify the code?

    Thanks

    WBR, Dmitry Kostenko.
    Nov 16 '05 #3
    My fault... loop should be

    do {
    ....
    }
    while(...)

    ;)

    Marcin
    Nov 16 '05 #4

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    8
    by: Dixie | last post by:
    I have the results of a query to send to a mailmerge with Word 2000. The query produces say 6 to 8 records, where only 1 of the fields is different from record to record. I can only have one...
    3
    by: Tom | last post by:
    Hi I want to retrieve database data and concatenate them to a string. Here is my code .ToString()); //runtime error her } while (dr.NextResult()) string array =...
    2
    by: Jake S | last post by:
    Hi all, Is it possibele to set the datasource of a dropdown list to a datareader? When I try to the only column I receive is a column populated by the datasource name repeated the amount of...
    5
    by: Rob Wire | last post by:
    For the code below, how could I add an item in the drop down lists for both company and location to be an "All" selection that would send to the stored proc. spRptAttachments a value of "%" so...
    1
    by: Brent | last post by:
    I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public...
    1
    by: Ivan Weiss | last post by:
    Hey all, I have the following code to populate a ListView control from my Access database. The listview is displaying a list of saved projects that the user will be able to open, edit, or delete...
    2
    by: Andrew Robinson | last post by:
    I am working on a data access layer using a pattern that I see more and more. Define a class that is a data container that is then added to a generic List<>. I am then using this List and support...
    10
    by: jimmy | last post by:
    Hi again, sorry for posting two questions so close together but im working on a school project which is due in soon and running into some difficulties implementing the database parts. I have the...
    13
    by: sinbad | last post by:
    hi, how to concatenate a "hash defined" constant value to another "hash defined" constant string. For example #define ABC 100 #define MYSTR "The value of ABC is" Now i need a string that...
    0
    by: taylorcarr | last post by:
    A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
    0
    by: aa123db | last post by:
    Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
    0
    by: ryjfgjl | last post by:
    If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
    0
    by: ryjfgjl | last post by:
    In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
    0
    by: emmanuelkatto | last post by:
    Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
    0
    BarryA
    by: BarryA | last post by:
    What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
    1
    by: nemocccc | last post by:
    hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
    0
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
    0
    jinu1996
    by: jinu1996 | last post by:
    In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.