By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,181 Members | 1,192 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,181 IT Pros & Developers. It's quick & easy.

Concatenate a datareader list into a string

P: n/a
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
Share this Question
Share on Google+
3 Replies


P: n/a
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

    P: n/a
    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

    P: n/a
    My fault... loop should be

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

    ;)

    Marcin
    Nov 16 '05 #4

    This discussion thread is closed

    Replies have been disabled for this discussion.