Baba mba Baba mba - 1 month ago 11
C# Question

Why do i get System.Data.DataRow?

I wanna show my rows in a listbox and the table names but when i run my code it says System.Data.DataRow can someone help me

my code is

foreach (object SelectedValue in ListEmployees.SelectedItems)
{
using (connection = new SqlConnection(connectionstring))
using (SqlDataAdapter adapter = new SqlDataAdapter("SELECT * FROM " + selectedCel, connection))
{
DataTable employees = new DataTable();
adapter.Fill(employees);

for (int i = 0; i < employees.Rows.Count; i++)
{
DataRow dr = employees.Rows[i];
ListViewItem itm = ListViewItem(dr.ToString());
listView1.Items.Add(itm);

}
}
}

Answer

DataRow.ToString() just prints out the full type name.

Presuming you want to show the first column:

ListViewItem itm = new ListViewItem(dr.Field<string>(0));
Comments