Jennifer Douglas
Jennifer Douglas

Reputation: 55

Error: Argument 1: cannot convert from 'int?[]' to 'object[]'

I'm trying to create a report with Visual Studio 2012, MVC 4, C# and the built in reporting system. I'm using a stored procedure to union and join about 13 tables I know this is me just having a brain dead moment, but as I'm trying to populate an array from a stored procedure into a ddl, I'm having issues with the following error:

Argument 1: cannot convert from 'int?[]' to 'object[]'

Here is the code:

public frm100percentQA()
{
        InitializeComponent();
        this.comboBox2.Visible = true;
        this.comboBox2.Items.Clear();
        List<Int32?> users = (from c in new NHISLINQ.NHISLINQDataContext().sp100PercentlQualityAssurance() where c.UserID != '0' select c.UserID).ToList();
        this.comboBox2.Items.Add("<---Select UserID--->");
        this.comboBox2.Items.Add("Select All");
        this.comboBox2.Items.AddRange(users.ToArray());
        this.comboBox2.SelectedIndex = 0;
}

I know it's probably a fairly simple solution, but I'm drawing a blank at this point. Any help?

Upvotes: 3

Views: 5013

Answers (2)

DROP TABLE users
DROP TABLE users

Reputation: 1955

Probably something to do with this line this.comboBox2.Items.AddRange(users.ToArray()); could you do this without using the null able int32? type? If so you should use a generic object and it should work.

Upvotes: 0

Kenneth
Kenneth

Reputation: 28747

The problem is that you can't add an Int-array to an Object-array. Although you might think this should be possible, it's not. This is called Covariance.

You should first cast all values to object before you add them to the object-array. You can do that using Linq in one line:

this.comboBox2.Items.AddRange(users.Cast<object>().ToArray());

Upvotes: 4

Related Questions