简体   繁体   中英

Reader loop doesn't work with parameter in string

I'm trying to display account balance from database in different currencies. Based on comboBox index the currencyValue changes. The problem is that with the parameter currencyValue in query string program never enters the while (reader.Read()) loop. Only with parameter. When I just use magical number for example 4.30 in string instead, it works perfectly fine, but with parameter which value is set to 4.30 nothing happens (only works with 1).

private void btnWybierz_Click(object sender, EventArgs e)
    {
        string fullname = cmbKonto.Text;

        string query = string.Format("SELECT CAST(balance / '{0}' AS DECIMAL(10, 2)) " +
                                            "FROM dbo.Accounts " +
                                            "WHERE (firstname + ' ' + lastname)='{1}'", currencyValue, fullname);

        using (SqlConnection connection = ConnectToDB.ConnectDB())
        {
            using (SqlCommand command = new SqlCommand(query, connection))
            {
                using (var reader = command.ExecuteReader())
                {
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            saldo = reader[0];
                            tbSaldo.Text = saldo.ToString();
                        }
                    }
                }
            }
        }
    }

This problem may be solved by writing decimal places as 4/30.

This problem may be solved writing Replace '{0}' with {0}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM