tika
tika

Reputation: 3

Add ListBox items from Dataset

I have this C# code.

j = myAccountDataset.Tables["AccountsTables"].Rows.Count;

                for (i = 0; i <= (j - 1); i++ )
                {

                   listAccountList.Items.Add(myAccountDataset.Tables[0].Rows[i][1]);
                }

                this.listAccountList.SelectedIndex = 0;

the idea is to iterate inside the dataset and add the items to the list. but i am getting the following errors: Error 1 The best overloaded method match for 'System.Web.UI.WebControls.ListItemCollection.Add(string)' has some invalid arguments

Argument1: cannot convert from 'object' to 'string'

i must be doing something wrong. thye error is in the line: listAccountList.Items.Add(myAccountDataset.Tables[0].Rows[i][1]);

thank you.

Upvotes: 0

Views: 2728

Answers (2)

Jason Yost
Jason Yost

Reputation: 4937

A little more description

Your myAccountDataset.Tables[0].Rows[i][1] is a typeless object, the Add method is expecting a string, you will need to cast the object to a string. The simplest method of doing this is to add the .ToString() operator to your datarow object

myAccountDataset.Tables[0].Rows[i][1].Tostring()

Upvotes: 0

dugas
dugas

Reputation: 12463

The ListItemCollection's Add method only accepts two types - a string or a ListItem See the MSDN documentation here. You need to pass a string instead of an object:

listAccountList.Items.Add(myAccountDataset.Tables[0].Rows[i][1].ToString());

Upvotes: 1

Related Questions