Unable to cast object of type 'System.Int32' to type 'System.String' in DataReader.GetString()

后端 未结 4 511
Happy的楠姐
Happy的楠姐 2020-12-11 04:21

I was trying to add data from a database to acombobox.

        try
        {
            SqlCeCommand com = new SqlCeCommand(\"select * from Category_Master\         


        
4条回答
  •  长情又很酷
    2020-12-11 04:59

    Your column doesn't seem to have type int. To avoid things like this, you can use the columnnames instead of indexes.

    try
    {
        SqlCeCommand com = new SqlCeCommand("select * from Category_Master", con);
        SqlCeDataReader dr = com.ExecuteReader();
        while(dr.Read()){
            string name = dr["yourColumnName"].ToString();
            cmbProductCategory.Items.Add(name);
        }
    }
    catch(Exception ex)
    {
        System.Windows.Forms.MessageBox.Show(ex.Message, System.Windows.Forms.Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    

提交回复
热议问题