В общем и целом пишу курсач. Вот весь код формы: using CloudServer.Classes; using MySqlConnector; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Reflection.Emit; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CloudServer.Forms { public partial class FormVPS : Form { public FormVPS() { InitializeComponent(); randText(); } private void FormVPS_Load(object sender, EventArgs e) { randText(); clearfields(); pricevalue_disc(); pricevalue_2xRam(); } public void randText() { Random rand = new Random(); for (int i = hostname.randNames.Count; i > 0; i--) { int flag = rand.Next(hostname.randNames.Count); hostnameTextBox.Text = (hostname.randNames[flag]); hostnameTextBox.PlaceholderText = (hostname.randNames[flag]); hostname.randNames.RemoveAt(flag); } } private void pricevalue_disc() { DB db = new DB(); db.openConnection(); var RePriceval1 = $"select price from services where `services`.`service_name` = 'xeon discount'"; var RePriceval3 = $"select price from services where services.service_name = 'xeon 10gbps'"; var pr1 = new MySqlCommand(RePriceval1, db.getConnection()); var pr3 = new MySqlCommand(RePriceval3, db.getConnection()); MySqlDataReader reader1 = pr1.ExecuteReader(); //MySqlDataReader reader3 = pr3.ExecuteReader(); while (reader1.Read()) { xeon_disc_val.Text = reader1[0].ToString() + rub1lbl.Text; } //while (reader3.Read()) //{ // xeon10gbps_val.Text = reader3[2].ToString() + rub3lbl.Text; //} db.closeConnection(); } private void pricevalue_2xRam() { DB dB = new DB(); dB.openConnection(); var RePriceval2 = $"select price from services where services.service_name = 'xeon 2x ram'"; var pr2 = new MySqlCommand(RePriceval2, dB.getConnection()); MySqlDataReader reader2 = pr2.ExecuteReader(); while (reader2.Read()) { xeon2xRam_val.Text = reader2[1].ToString() + rub2lbl.Text; } dB.closeConnection(); } private void clearfields() { xeon_disc_val.Text = string.Empty; xeon2xRam_val.Text = string.Empty; xeon10gbps_val.Text = string.Empty; rub1lbl.Visible = false; rub2lbl.Visible = false; rub2lbl.Visible = false; rub3lbl.Visible = false; } } } CSHARP using CloudServer.Classes; using MySqlConnector; using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Globalization; using System.Linq; using System.Reflection.Emit; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace CloudServer.Forms { public partial class FormVPS : Form { public FormVPS() { InitializeComponent(); randText(); } private void FormVPS_Load(object sender, EventArgs e) { randText(); clearfields(); pricevalue_disc(); pricevalue_2xRam(); } public void randText() { Random rand = new Random(); for (int i = hostname.randNames.Count; i > 0; i--) { int flag = rand.Next(hostname.randNames.Count); hostnameTextBox.Text = (hostname.randNames[flag]); hostnameTextBox.PlaceholderText = (hostname.randNames[flag]); hostname.randNames.RemoveAt(flag); } } private void pricevalue_disc() { DB db = new DB(); db.openConnection(); var RePriceval1 = $"select price from services where `services`.`service_name` = 'xeon discount'"; var RePriceval3 = $"select price from services where services.service_name = 'xeon 10gbps'"; var pr1 = new MySqlCommand(RePriceval1, db.getConnection()); var pr3 = new MySqlCommand(RePriceval3, db.getConnection()); MySqlDataReader reader1 = pr1.ExecuteReader(); //MySqlDataReader reader3 = pr3.ExecuteReader(); while (reader1.Read()) { xeon_disc_val.Text = reader1[0].ToString() + rub1lbl.Text; } //while (reader3.Read()) //{ // xeon10gbps_val.Text = reader3[2].ToString() + rub3lbl.Text; //} db.closeConnection(); } private void pricevalue_2xRam() { DB dB = new DB(); dB.openConnection(); var RePriceval2 = $"select price from services where services.service_name = 'xeon 2x ram'"; var pr2 = new MySqlCommand(RePriceval2, dB.getConnection()); MySqlDataReader reader2 = pr2.ExecuteReader(); while (reader2.Read()) { xeon2xRam_val.Text = reader2[1].ToString() + rub2lbl.Text; } dB.closeConnection(); } private void clearfields() { xeon_disc_val.Text = string.Empty; xeon2xRam_val.Text = string.Empty; xeon10gbps_val.Text = string.Empty; rub1lbl.Visible = false; rub2lbl.Visible = false; rub2lbl.Visible = false; rub3lbl.Visible = false; } } } Класс для присоединения к бд: using MySqlConnector; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CloudServer { internal class DB { private MySqlConnection connection; public void openConnection() { connection = new MySqlConnection("server=localhost;port=3306;username=root;password=root;database=cloudserver"); if (connection.State == System.Data.ConnectionState.Closed) connection.Open(); } public void closeConnection() { if (connection.State == System.Data.ConnectionState.Open) connection.Close(); connection.Dispose(); } public MySqlConnection getConnection() { return connection; } } } CSHARP using MySqlConnector; using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace CloudServer { internal class DB { private MySqlConnection connection; public void openConnection() { connection = new MySqlConnection("server=localhost;port=3306;username=root;password=root;database=cloudserver"); if (connection.State == System.Data.ConnectionState.Closed) connection.Open(); } public void closeConnection() { if (connection.State == System.Data.ConnectionState.Open) connection.Close(); connection.Dispose(); } public MySqlConnection getConnection() { return connection; } } } При заходе на форму выдает данную ошибку: Ошибка
Скорее всего у тебя запрос не возвращает значений. Попробуй в субд его выполнить и посмотри, возвращается ли что-нибудь
cybernark, а, ты запросом достаешь только цену, а при записи в поле обращаешься ко второму элементу. Поменяй индекс с 1 на 0 у reader2[1]