根据组合框选择显示文本框值

时间:2016-03-10 14:25:01

标签: c# sql string combobox textbox

在SQL中我有列price(nchar(10)), product_category(nvarchar(50)) 我想在从combobox中选择项目后显示从sql到某些文本框的价格。获取连续错误,当我尝试GETOrdinal("ColumnName")时,当我从组合框中选择时,它在文本框中只显示0 请带我离开这个。提前谢谢

private void cb_ocat_SelectedIndexChanged_1(object sender, EventArgs e)
{
    using (SqlConnection sqlConnection = new SqlConnection(@"Data Source=.;Initial Catalog=Pizza Mania;Integrated Security=True"))
    {
        using (SqlCommand sqlCmd2 = new SqlCommand("select Distinct(Price) from product where Product_category='" + cb_ocat.Text + "'", sqlConnection))
        {
            sqlConnection.Open();

            SqlDataReader sqlrdr = sqlCmd2.ExecuteReader();


            while (sqlrdr.Read())
            {
                String price = sqlrdr.GetInt32("Price").ToString();//getting error here that dbdatareader(int) has some invalid arguments
                txt_oprice.Text = price;
            }
            sqlConnection.Close();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

试试这个..

while (sqlrdr.Read())
                {
                    String price = sqlrdr["Price"].ToString();//getting error here that dbdatareader(int) has some invalid arguments
                    txt_oprice.Text = price;
                }