2016-02-29 51 views
-5

当我试图插入产品中使用的代码我的DB到:无法隐式转换类型双为int,显式转换存在(是否缺少强制转换?)

private Product CreateProduct() 
{ 
    Product product = new Product(); 

    product.Name = txtName.Text; // getting error here (1) 
    product.Price = Convert.ToDouble(txtPrice.Text); //Getting error here (2) 
    product.TypeId = Convert.ToInt32(ddlType.SelectedValue); 
    product.Description = txtDescription.Text; 
    product.Image = ddlImage.SelectedValue; 

    return product; 
} 

我无法修复这个错误。请让我知道我哪里错了。

解决!

+0

请包括更多细节。例如,您应该包含错误消息和导致错误发生的数据。 – ryanyuyu

回答

0

看起来你的product.Priceint类型,你想给它double

+0

错误二是固定的,在(1)处得到另一个错误,不能隐式地将类型'字符串'转换为'byte []' –

+0

只是检查产品属性的类型和你给它们的值的类型 – VDN

0

product.Priceint。你不能隐式地将double转换为int,因为精度会有所下降。鉴于该物业名称为Price,您应该将Product.Price更改为double(或甚至是十进制)。

+0

错误(2 )现在是固定的,并且我在(1)错误处得到另一个错误:不能隐式地将类型'字符串'转换为'byte []'。 –

+0

否Product.Name不是字节它是'文本'。 –

+0

使用我用来解决第一个问题的相同过程,以确保您的类型匹配。你必须学会​​调试你的代码。 – Jonesopolis

相关问题