无法将类型'string'隐式转换为'int'?

时间:2013-06-16 04:19:56

标签: c# type-conversion

我在组合框中选择任何可用的名称,它应该反映在文本框中。 我是初学者请帮我解决一下

public class product
{
    public int proid { set; get; }
    public string prodname { set; get; }
    public int unitprice { set; get; }

}
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)        
{
    SqlConnection con = new SqlConnection(@"server=xxxx-PC; database= sample; integrated security= true");
    con.Open();
    SqlCommand cmd = new SqlCommand("select * from tblproduct ", con);
    SqlDataReader dr = cmd.ExecuteReader();
    product prod = new product();
    while(dr.read())
    {
        prod.proid = dr[0].ToString();
        prod.prodname = dr[1].ToString();
        prod.unitprice = dr[2].ToString();
        textBox2.Text = proid;
        textBox3.Text = prodname;
        textBox4.Text = unitprice;
    } 
}

有错误:

Error   :   The name 'prodname' does not exist in the current context
Error   :   Cannot implicitly convert type 'string' to 'int'`improved formatting`

5 个答案:

答案 0 :(得分:1)

问题在于您尝试将String值设置为Int32属性而不进行任何显式转换或转换,并且未定义隐式转换:

// ...
public int proid { set; get; }
// ...

// ...
prod.prodid = dr[0].ToString();
// ...

根据dr[0]的实际类型,您可以使用GetInt32()和类似方法代替indexer.ToString()

prod.proid = dr.GetInt32(0);

但是,如果该列在SQL中可以为空,则需要使用IsDbNull()测试NULL并提供替换值:

prod.proid = dr.IsDbNull(0) ? default(int) : dr.GetInt32(0);

如果null'属性更改为int?,则默认值只能class才能与SQL匹配:

public int? proid { set; get; }

或者,分别指定演员或转换:

prod.proid = Convert.ToInt32(dr[0].ToString());

但是,由于目标似乎是更新文本框,因此您可以使用product跳过绕行,将String值与String属性相匹配:

textBox2.Text = dr[0].ToString();

答案 1 :(得分:0)

您必须将String显式转换为int:

int i;

bool succeeded = int.TryParse(string, out i); 

答案 2 :(得分:0)

您宣布产品为

public class product
    {
        public int proid { set; get; }    //Integer
        public string prodname { set; get; }
        public int unitprice { set; get; }
    }

并将字符串指定为prod.proid = dr[0].ToString();

其次,textBox2.Text = proid;语句在某些情况下有错误。例如null或其他格式。你可以使用TryParse(),如下所示:

int productId = 0;
 Int.TryParse(string, out productId)
 textBox2.Text =productId.ToString() ; 

答案 3 :(得分:0)

一旦获得代码编译,您可能遇到的另一个问题是使用SqlDataReader:您必须在尝试访问dr [0]之前调用dr.Read()

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqldatareader.read.aspx

答案 4 :(得分:-1)

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)

 {
            SqlConnection con = new SqlConnection(@"server=xxxx-PC; database= sample;       integrated security= true");
            con.Open();
            SqlCommand cmd = new SqlCommand("select * from tblproduct ", con);
            SqlDataReader dr = cmd.ExecuteReader();
            product prod = new product();

            // need to convert to integer, coz prodid is int
            prod.proid = Convert.ToInt32(dr[0].ToString());
            prod.prodname = dr[1].ToString();
            prod.unitprice = Convert.ToInt32(dr[2].ToString());

            // need to convert to string, textbox gets only string type
            // prod... => object_name.member_name
            textBox2.Text = prod.proid.ToString();

            // or u can u do: ..= prod.prodid+""; => this convert to str automatically             
            textBox3.Text = prod.prodname.ToString();
            textBox4.Text = prod.unitprice.ToString();
}