I want to retrieve the data from MS.Access through list but when run the application it show me all entries but just file name like Student.StudentInformation I don’t know why and then When i select the first entry i show me right data in textBox?
I query is:
public ICollection<StudentInformation> GetStudents()
{
OleDbConnection con = new OleDbConnection(connectionString);
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.Text;
cmd.CommandText = "SELECT * FROM Students";
ObservableCollection<StudentInformation> students = new ObservableCollection<Student>();
try
{
con.Open();
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Student aStudent = new Student(Convert.ToInt32(reader["StudentID"]),
reader["StudentName"].ToString(),
reader["StudentEmail"].ToString());
students.Add(aStudent);
}
reader.Close();
return students;
}
finally
{
con.Close();
}
}
In Student Information Class i did get set and made two constructor.One is default constructor and second pass the values.
and I am showing in list box:
private ICollection<StudentInformation> students;
private void BtnGetStudent_Click(object sender, RoutedEventArgs e)
{
students = StudentDb.GetStudents();
Studentlst.ItemsSource = students;
}
It show me list like Student.StudentInformation
How to fix it?
This seems to be a presentation problem. You should explain the WPF (you are using WPF, right?) how to display a
StudentInformation.As a simple workaround, you can overload the function
ToString()forStudentInformation, but the better solution would be writing aDataTemplatefor it. (Do you know how to do it?)A note aside: accessing the database in the UI thread is not a very good idea, this would block your UI for the duration of the query.