c# loop

    webBrowser1.Navigate(new Uri(urls[0]));
                webBrowser1.Dock = DockStyle.Fill;
                webBrowser2.Navigate(new Uri(urls[1]));
                webBrowser2.Dock = DockStyle.Fill;
                webBrowser3.Navigate(new Uri(urls[2]));
                webBrowser3.Dock = DockStyle.Fill;
                webBrowser4.Navigate(new Uri(urls[3]));
                webBrowser4.Dock = DockStyle.Fill;

it like all of  url in web control is the same it is the last one from query how can i fix it to each row that it can read from db
{
            string MyConString = "SERVER=192.168.0.78;" +
                 "DATABASE=webboard;" +
                 "UID=aimja;" +
                 "PASSWORD=aimjawork;" +
                 "charset=utf8;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;
            command.CommandText = "SELECT  urlwebboard FROM `listweb` WHERE `urlwebboard` IS NOT NULL AND ( `webbordkind` = '¿¿¿¿¿¿¿¿¿¿¿¿' ) and `nourl`= 'n' order by province, amphore limit 4 ";
            connection.Open();
            Reader = command.ExecuteReader();

            
            string[] urls = new string[4];
            string thisrow = "";
            string sumthisrow = "";
            string urlname ;
            while (Reader.Read())
            {
                thisrow = "";
                sumthisrow = "";
                for (int i = 0; i < Reader.FieldCount; i++)
                {
                    thisrow += Reader.GetValue(i).ToString();
                   

                

                    System.IO.File.AppendAllText(@"C:\file.txt", thisrow + " " + Environment.NewLine);
                    sumthisrow =  thisrow;

                }

               


            }
            for (int i = 0; i < 4; i++)
            {
                urls[i] = sumthisrow;
                webBrowser1.Navigate(new Uri(urls[0]));
                webBrowser1.Dock = DockStyle.Fill;
                webBrowser2.Navigate(new Uri(urls[1]));
                webBrowser2.Dock = DockStyle.Fill;
                webBrowser3.Navigate(new Uri(urls[2]));
                webBrowser3.Dock = DockStyle.Fill;
                webBrowser4.Navigate(new Uri(urls[3]));
                webBrowser4.Dock = DockStyle.Fill;

            }
            connection.Close();

Open in new window

teeraAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Dennis AriesCEO @ Arkro ITCommented:
In your last for-loop, you set
urls[i]

Open in new window

to sumthisrow, which is the last row you used in the for-loop that you used before that.

 
{
            string MyConString = "SERVER=192.168.0.78;" +
                 "DATABASE=webboard;" +
                 "UID=aimja;" +
                 "PASSWORD=aimjawork;" +
                 "charset=utf8;";
            MySqlConnection connection = new MySqlConnection(MyConString);
            MySqlCommand command = connection.CreateCommand();
            MySqlDataReader Reader;
            command.CommandText = "SELECT  urlwebboard FROM `listweb` WHERE `urlwebboard` IS NOT NULL AND ( `webbordkind` = '¿¿¿¿¿¿¿¿¿¿¿¿' ) and `nourl`= 'n' order by province, amphore limit 4 ";
            connection.Open();
            Reader = command.ExecuteReader();

            
            string[] urls = new string[4];
           
            string thisrow = "";
            string sumthisrow = "";
            string urlname ;
            while (Reader.Read())
            {
                thisrow = "";
                sumthisrow = "";
                for (int i = 0; i < Reader.FieldCount; i++)
                {
                    thisrow += Reader.GetValue(i).ToString();
                   

                

                    System.IO.File.AppendAllText(@"C:\file.txt", thisrow + " " + Environment.NewLine);
                    sumthisrow =  thisrow;
                    // Assign the value to the url-array. I added a check there in case there are more than 4 records.
                    if (i>=0 && i<=4) {
                      urls[i] = thisrow;
                    }
                }

               


            }
            for (int i = 0; i < 4; i++)
            {
                // urls[i] = sumthisrow;   <-- this one was causing the problem
                webBrowser1.Navigate(new Uri(urls[0]));
                webBrowser1.Dock = DockStyle.Fill;
                webBrowser2.Navigate(new Uri(urls[1]));
                webBrowser2.Dock = DockStyle.Fill;
                webBrowser3.Navigate(new Uri(urls[2]));
                webBrowser3.Dock = DockStyle.Fill;
                webBrowser4.Navigate(new Uri(urls[3]));
                webBrowser4.Dock = DockStyle.Fill;

            }
            connection.Close();
}

Open in new window

lazyberezovskyCommented:
teera, consider following to improve your code

1) move connection strings to configuration file. E.g.
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <connectionStrings>
        <add name="MyConString" connectionString="SERVER=192.168.0.78;DATABASE=webboard;UID=aimja;PASSWORD=aimjawork;charset=utf8;"/>
    </connectionStrings>
</configuration>
This will allow to change connections string easily. Also you can add encryption for private data (if you need to)

2) Use "using" syntax for working with disposable objects like database connections or files - it will free unmanaged resources automatically, even in case of exception.

3) Divide & Conquer - split your code to small methods which describe their intension by name

Example:

List<string> urls = GetUrls();
SaveUrlsToFile(urls);
OpenUrls(urls);

private List<string> GetUrls()
{
    List<string> urls = new List<string>();
    // Get connection string from App.config file
    string myConString = ConfigurationManager.ConnectionStrings["MyConString"].ConnectionString;

    using (MySqlConnection connection = new MySqlConnection(myConString))
    {
        MySqlCommand command = connection.CreateCommand();
        command.CommandText = "SELECT  urlwebboard FROM `listweb` WHERE `urlwebboard` IS NOT NULL AND ( `webbordkind` = '¿¿¿¿¿¿¿¿¿¿¿¿' ) and `nourl`= 'n' order by province, amphore limit 4 ";
        connection.Open();

        MySqlDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            string url = String.Empty;

            for (int i = 0; i < reader.FieldCount; i++)
                url += reader.GetValue(i).ToString();

            urls.Add(url);
        }
    } // Connection closed automatically here

    return urls;
}

private void SaveUrlsToFile(IEnumerable<string> urls)
{
    const string filePath = @"C:\file.txt";
    using (StreamWriter writer = new StreamWriter(filePath))
    {
        foreach (string url in urls)
            writer.WriteLine(url);
    }
}

private void OpenUrls(List<string> urls)
{
    if (urls.Count < 4)
        return;

    OpenUrl(webBrowser1, urls[0]);
    OpenUrl(webBrowser2, urls[1]);
    OpenUrl(webBrowser3, urls[2]);
    OpenUrl(webBrowser4, urls[3]);
}

private void OpenUrl(WebBrowser webBrowser, string url)
{
    webBrowser.Navigate(new Uri(url));
    webBrowser.Dock = DockStyle.Fill;
}

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.