E
E
EcoTry2021-08-26 10:39:08
C++ / C#
EcoTry, 2021-08-26 10:39:08

Cannot convert from string to int. But all data is string. Why?

The program opens a connection to the database, takes a data of type nvarchar (50) from there and writes them to string, but writes that it cannot be converted from string to int.

private async void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string connectionString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Project\SaveMePassword\SaveMePassword\Database1.mdf;Integrated Security=True";
            SqlConnection con = new SqlConnection(connectionString);
            SqlCommand cmd;
            cmd = con.CreateCommand();
            cmd.CommandText = "SELECT * FROM [Data] WHERE NameSite='" + listBox1.Text + "';";
            try
            {
                con.Open();
                SqlDataReader sdr = cmd.ExecuteReader();

                // Загружаем данные
                while (sdr.Read())
                {
                    // Здесь ошибка
                     ______________________________________
                    string sSite = sdr.GetString("Site");
                    string sUser = sdr.GetString("User");
                    string sPassworSite = sdr.GetString("PasswordSite");
                    ________________________________________
                    _BoxSite.Text = sSite;
                    _BoxLogin.Text = sUser;
                    _BoxPassword.Text = sPassworSite;
                }

                // Закрываем соединение
                con.Close();
            }
            catch (Exception)
            {
                MessageBox.Show("Ошибка");
            }
        }

Mistake

Ошибка CS1503 Аргумент 1: не удается преобразовать из "string" в "int".

Answer the question

In order to leave comments, you need to log in

1 answer(s)
F
freeExec, 2021-08-26
@EcoTry

So you read what arguments are neededGetString

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question