-4

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

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`
4

5 回答 5

1

问题是您试图在没有任何显式转换或转换的情况下String为属性设置值Int32,并且没有定义隐式转换:

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

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

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

prod.proid = dr.GetInt32(0);

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

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

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

public int? proid { set; get; }

或者,单独指定强制转换或转换:

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

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

textBox2.Text = dr[0].ToString();
于 2013-06-16T04:30:10.340 回答
0

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

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

于 2013-06-16T04:33:12.243 回答
0

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

int i;

bool succeeded = int.TryParse(string, out i); 
于 2013-06-16T04:26:20.433 回答
0

您将 Product 声明为

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() ; 
于 2013-06-16T04:27:59.037 回答
-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();
} 
于 2013-06-16T04:31:33.897 回答