Click here to Skip to main content
15,905,914 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hi to all!

here i wrote a code to copy all column value in a string but its copying only first column value

C#
if (rdr.HasRows)
               {
                   StringBuilder sb = new StringBuilder();
                   string a;
                   while (rdr.Read())
                   {
                       sb.Append(((string)rdr["modname_vc"]).ToString());
                       sb.Append("\n");
                       a = sb.ToString();
                       for (int i = 0; i < module_name_menustrip.Length; i++)
                       {
                           module_name_menustrip[i] = a;
                           MessageBox.Show(module_name_menustrip[i]);
                           sb.Length = 0;
                           count1++;

                       }

                   }



can u please tell me whats the error
Posted
Updated 30-Jul-11 22:59pm
v2

Why don't you do this at SQL Server itself? Look here on how to concat column values in SQL.

Get Column values as comma seperated string [^]
 
Share this answer
 
Comments
kami124 31-Jul-11 5:55am    
ok, its clear now i have to add the string in menustrip
I'm not exactly sure what you are trying to do here, your code doesn't make a lot of sense.
1) You have a loop, through all of the rows returned from your database.
2) You read the row value, and add it to the StringBuilder, along with a newline.
3) You then convert it to a string, and enter a new loop.
4) In this loop, you load all the elements of an array with the string, and kill the StringBuilder, by resetting the length.

Why have the StringBuilder at all? You code is the equivalent of:
C#
if (rdr.HasRows)
{
    while (rdr.Read())
    {
        for (int i = 0; i < module_name_menustrip.Length; i++)
        {
            module_name_menustrip[i] = ((string)rdr["modname_vc"]).ToString() + "\n";
            MessageBox.Show(module_name_menustrip[i]);
            count1++;
        }
    }
 
Share this answer
 
Comments
kami124 31-Jul-11 5:16am    
i got that actually i was adding the column into string its done now
i want to add these string items into an menu strip
here is my new code

if (rdr.HasRows)
{
StringBuilder sb = new StringBuilder();
string a;
int i = 0;
while (rdr.Read())
{
sb.Append(((string)rdr["modname_vc"]).ToString());
sb.Append("\n");
a = sb.ToString();
module_name_menustrip[i] = a;
menuStrip1.Items.Add(module_name_menustrip[i]);
sb.Length = 0;
count1++;
i++;
}

}

but i cannot view it on menu strip
You are only concatenating one column value, which is modname_vc. What you need to do is to loop to all the columns of your data reader and get their values.
 
Share this answer
 
Comments
kami124 31-Jul-11 5:21am    
no, i need only one column modname_vc
and these values i want to add on toolstrip

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