Search code examples
c#asp.netdatatabledatareader

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?


Solution

  • 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++;               
        }
    }