473,473 Members | 2,073 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 8785
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: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
    0
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    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...
    0
    agi2029
    by: agi2029 | last post by:
    Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
    0
    by: conductexam | last post by:
    I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
    0
    by: TSSRALBI | last post by:
    Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
    0
    by: adsilva | last post by:
    A Windows Forms form does not have the event Unload, like VB6. What one acts like?
    0
    by: 6302768590 | last post by:
    Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
    1
    muto222
    php
    by: muto222 | last post by:
    How can i add a mobile payment intergratation into php mysql website.

    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.