Link to home
Create AccountLog in
Avatar of MrTV
MrTVFlag for Thailand

asked on

c# try too keep data to array

the code below I try to keep data to array but it like it keep only when MessageBox.Show(urls[0]); if it be urls[1] or urls[2]) it have no result while C:\file.txt have value 4 line
private void Form1_Load(object sender, EventArgs e)
        {
            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  url FROM `listweb` WHERE `url` 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 = "";
                for (int i = 0; i < Reader.FieldCount; i++)
                {
                    thisrow += Reader.GetValue(i).ToString();
                    urlname = Reader.GetValue(i).ToString();

                    urls[i] = thisrow;

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

                }
             
            }
            connection.Close();
            MessageBox.Show(urls[1]);
           
        }

Open in new window

SOLUTION
Avatar of cyberkiwi
cyberkiwi
Flag of New Zealand image

Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account

private void Form1_Load(object sender, EventArgs e)
        {
            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  url FROM `listweb` WHERE `url` IS NOT NULL AND ( `webbordkind` = '¿¿¿¿¿¿¿¿¿¿¿¿' ) and `nourl`= 'n' order by province, amphore limit 4 ";
            connection.Open();
            Reader = command.ExecuteReader();

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

                    urls[i] = thisrow;
                    iindex++;

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

                }
             
            }
            connection.Close();
            if (iindex > -1) {
                MessageBox.Show(urls[1]);
            }
        }

Open in new window

One minor correction in messagebox.show
private void Form1_Load(object sender, EventArgs e)
        {
            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  url FROM `listweb` WHERE `url` IS NOT NULL AND ( `webbordkind` = '¿¿¿¿¿¿¿¿¿¿¿¿' ) and `nourl`= 'n' order by province, amphore limit 4 ";
            connection.Open();
            Reader = command.ExecuteReader();

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

                    urls[i] = thisrow;
                    iindex++;

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

                }
             
            }
            connection.Close();
            if (iindex > -1) {
                MessageBox.Show(urls[iindex]);
            }
        }

Open in new window

ASKER CERTIFIED SOLUTION
Link to home
membership
Create an account to see this answer
Signing up is free. No credit card required.
Create Account