Reputation: 265
I'm trying to popular a list box in a Visual C# Form Application on Visual Studio 2012, however when I go to add the items to the list box, it claims the error:
Error 1 Operator '+' cannot be applied to operands of type 'object' and 'object'
How do I write code to add in 3 objects using the + operator?
I've never used a compact SQL database before, only through web development so I am new to Visual Studio Databases, so most probably a rookie error. Can anyone point me in the right direction?
populateListBox
public void populateListBox()
{
String query = "SELECT Bug_Code, Bug_Description, Bug_Author FROM tblBugs";
SqlCeCommand mySqlCommand = new SqlCeCommand(query, mySqlConnection);
try
{
mySqlConnection.Open();
SqlCeDataReader mySqlDataReader = mySqlCommand.ExecuteReader();
lbxBugged.Items.Clear();
while (mySqlDataReader.Read())
{
lbxBugged.Items.Add(mySqlDataReader["Bug_Code"] + mySqlDataReader["Bug_Description"] + mySqlDataReader["Bug_Author"]);
}
}
catch (SqlCeException)
{
MessageBox.Show("Error populating list box");
}
}
Upvotes: 0
Views: 2360
Reputation: 35318
You need to cast or convert the column's value into an actual type to make it usable, which is, in your case, strings for each of the columns. Try this:
lbxBugged.Items.Add(mySqlDataReader["Bug_Code"].ToString() +
mySqlDataReader["Bug_Description"].ToString() +
mySqlDataReader["Bug_Author"].ToString());
Upvotes: 2