none
C# dataset table into array. RRS feed

  • Question

  • Hi, i need to populate an array any type of array with dataset table column, in my table there is "userId" and "userName" column ... i have this in dataset but i want to populate an array with "userName" in a listbox but not the "userId" ..
    any suggestions or sample code would be helpful
    thanks
    Sunday, May 4, 2008 3:47 AM

Answers

  • hi

     

    as per above, you can set it as datasource or you can iterate over the datatable to fill the box

    something like ( I dont have vs on this computer)

     

    Code Snippet

    // fill box by table

    //gives you text as name, value as id

    listbox.Items.Clear();

    foreach(DataRow row in DataSet.Tables["tablename"].Rows){

    listbox.Items.Add(new ListItem(row["name"].ToString(), row["id"].ToString()));

    }

     

    // fill box via array

    int count = DataSet.Tables["tablename"].Rows.Count;

    string[] names = new string[count];

    for(int i = 0; i < count ; i++){

    names[i] = DataSet.Tables["tablename"].Rows[i]["name"].ToString();

    }

    // afterwards add new listitems

     

     

     

    Hope this helps you out
    Sunday, May 4, 2008 8:15 AM

All replies

  • Do you need to? Can't you just set the DataTable as the DataSource, the DisplayMember to "userName" and the ValueMember to whatever you want ("userName" or "userId")? Why do you need it as an array?

     

    Sunday, May 4, 2008 8:08 AM
  • hi

     

    as per above, you can set it as datasource or you can iterate over the datatable to fill the box

    something like ( I dont have vs on this computer)

     

    Code Snippet

    // fill box by table

    //gives you text as name, value as id

    listbox.Items.Clear();

    foreach(DataRow row in DataSet.Tables["tablename"].Rows){

    listbox.Items.Add(new ListItem(row["name"].ToString(), row["id"].ToString()));

    }

     

    // fill box via array

    int count = DataSet.Tables["tablename"].Rows.Count;

    string[] names = new string[count];

    for(int i = 0; i < count ; i++){

    names[i] = DataSet.Tables["tablename"].Rows[i]["name"].ToString();

    }

    // afterwards add new listitems

     

     

     

    Hope this helps you out
    Sunday, May 4, 2008 8:15 AM
  • thanks  frederikm that was very hlpful .
     
    Wednesday, May 7, 2008 4:37 AM
  • i needed this in an array so that i could apply the mvc concept and make it more object oriented. because i cant call the dataset in view i must use business logic layer or the data layer to set my dataset table values into an array of some sort then assign the values of the array in some controls in the view.
    Wednesday, May 7, 2008 4:43 AM
  • Thank you very much frederikm.

    Thanks.

    Friday, September 23, 2011 11:25 AM