Concatenate a datareader list into a string

G

Guest

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
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

Hi 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);[/QUOTE]

object phoneObj;

//> 	do

while( dr.NextResult() )
[QUOTE]
{[/QUOTE]
// 		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());

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

string result = String.Join(",", array);
// lifting change
[QUOTE]

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
 
D

Dmitry Kostenko

Tom said:
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);
	do
	{[/QUOTE]

I think you should check if dr["phone"] == null before calling ToString():
if (dr["phone"] != null)
[QUOTE]
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.
 
?

=?ISO-8859-2?Q?Marcin_Grz=EAbski?=

My fault... loop should be

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

;)

Marcin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top