Click here to Skip to main content
15,891,657 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to save these records in a string to use it in other function. Here is the code I currently have:
C#
StringBuilder sb = new StringBuilder();
while (rdr.Read())
{
  sb.Append(((int)rdr["fnid_int"]).ToString());
  sb.Append("\n");
}
Posted
Updated 4-Jul-11 23:36pm
v2

string result = sb.ToString(); should do the trick.
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 5-Jul-11 5:42am    
Correct, but OP might not understand it as -- do use see ToString already in the answer? -- it is misused. My 5 anyway.
Please see my answer.
--SA
StringBuilder overrides the ToString() method to return a string, so you would return sb.ToString(); There are some areas that your code could be tightened up though; just like this:
C#
StringBuilder sb = new StringBuilder();
while (rdr.Read())
{
  sb.AppendFormat("{0}\n", rdr["fnid-int"]);
}
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 5-Jul-11 5:41am    
I think OP's problem is misuse of the ToString.
Please see my answer.
--SA
Your call to ToString does nothing as you discard the function result. Here is what to do: work with StringBuilder until you're completely done, at the very end convert result to string by string result = sb.ToString().

—SA
 
Share this answer
 
Comments
Pete O'Hanlon 5-Jul-11 5:50am    
It's a context thing. We, effectively, both said the same thing - hence the reason I showed the updated version of his StringBuilder code - I assumed he was capable of adding the result in himself. +5 anyway.
Sergey Alexandrovich Kryukov 5-Jul-11 5:56am    
Thank you, Pete. I did not pay attention you explained ToString, but the OP's problem is different: weak understanding if simple assignment and function return (and most likely a lot more), so the assignment should be explained. I'll vote 5 for your answer.
--SA
Abhinav S 5-Jul-11 7:06am    
Well somewhat similar to my answer - my 5.
:)
Sergey Alexandrovich Kryukov 5-Jul-11 12:54pm    
Thank you, Abhinav.
--SA
string value=sb.ToString();



Pass this "value" into your function!!!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900