Mar 2, 2010

Populate list from untyped DataSet

A table in a dataset is used to populate a list box with employee name from the employee table. With the dataset the name of the table and field must be known at design time. if they are misspelled or mistyped an error will be generated only at runtime.

Define the dataset.

private DataSet dsEmp;


//Call this on your button click event
PopulateListFromDS();


private void PopulateListFromDS()

{

string s;

int i;

//Clear items in ListBox

lstResults.Items.Clear();



for(i=0; i <=dsEmp.Tables["EmplyeeDS"].Rows.Count - 1; i++)

{

// Check to see if row is flagged deleted.

if (dsEmp.Tables["EmplyeeDS"].Rows.RowState != DataRowState.Deleted)

{

// Get the product name for each record.

s = dsEmp.Tables["EmplyeeDS"].Rows["EmpName"].ToString();

// Add product name to the list box

lstResults.Items.Add(s);

}

}

}

No comments:

Post a Comment