Alex
Alex

Reputation: 9720

how read with datareader from datatable

I have a function that returns a DataTable

DataTable dt = GetAllObject(ogj_id);

Now I want to fill one MultiCheckCombo, below is the link where I got this MultiCheckCombo

MultiCheckCombo Reference

The example of how to fill this MultiCheckCombo from link above is only with dataReader

OdbcConnection con = "get YOUR connection string";
con.Open();
OdbcCommand cmd = new OdbcCommand();
cmd.Connection = con;
cmd.CommandText = "select text,id from ...........";
OdbcDataReader dr = cmd.ExecuteReader();
MultiCheckCombo1.ClearAll();
dr.Read(); 
MultiCheckCombo1.AddItems(dr, "text", "id");

Query - Now my question sounds like this: How to convert DataTable in dataReader to fill this MultiCheckCombo?

Upvotes: 1

Views: 5976

Answers (2)

D Stanley
D Stanley

Reputation: 152521

Find a different control. That one is hard-coded to using an OdbcDataReader. You don't use a DataReader to read data from a DataTable - you iterate through the DataRows.

As a workaround, you could overload AddItems to accept a DataTable:

public void AddItems(DataTable dt, string textField, string valueField)
{
    ClearAll();
    int i = 0;
    foreach (DataRow dr in dt.Rows)
    {
        chkList.Items.Add(dr[textField].ToString());
        chkList.Items[i].Value = dr[valueField].ToString(); 
        i++;               
    }
}

Upvotes: 2

Jeff
Jeff

Reputation: 14279

You can't convert a data reader to a data table. You'll have to loop over the contents of the table and build each item manually. I am not familiar with this particular control, but if it follows normal conventions, it should probably look something like this:

foreach(DataRow row in dt.Rows)
{
    MultiCheckCombo1.Items.Add(new ListItem(dt["text"], dt["id"]));
}

Upvotes: 1

Related Questions