473,320 Members | 2,162 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,320 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 8744
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: DolphinDB | last post by:
    Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
    0
    by: ryjfgjl | last post by:
    ExcelToDatabase: batch import excel into database automatically...
    0
    isladogs
    by: isladogs | last post by:
    The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
    1
    isladogs
    by: isladogs | last post by:
    The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
    0
    by: Vimpel783 | last post by:
    Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
    0
    by: ArrayDB | last post by:
    The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
    1
    by: PapaRatzi | last post by:
    Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
    0
    by: CloudSolutions | last post by:
    Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
    0
    by: Defcon1945 | last post by:
    I'm trying to learn Python using Pycharm but import shutil doesn't work

    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.