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 3 8744
Hi Tom, Hi,
I want to retrieve database data and concatenate them to a string. Here is my 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);
-
-
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
Tom wrote: Hi,
I want to retrieve database data and concatenate them to a string. Here is my 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
- {
-
-
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.
My fault... loop should be
do {
....
}
while(...)
;)
Marcin This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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 =...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: ryjfgjl |
last post by:
ExcelToDatabase: batch import excel into database automatically...
|
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...
|
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...
|
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...
|
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...
|
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)...
|
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...
|
by: Defcon1945 |
last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
| |